Skip to content

Commit

Permalink
More graphql schema cleanup (#7)
Browse files Browse the repository at this point in the history
* Make top-level module docstring runnable, doctest code

* Update language around music providers

At the moment the language is:
- spotify is a music provider
- each listen has a 'song provider' field, which is a music provider
type
  • Loading branch information
zhammer authored Dec 16, 2018
1 parent c114a6a commit e892385
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions front/delivery/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@
This isn't super intuitive and at the moment is not very well documented.
We can have something like:
```py
class PersonNamedTuple(NamedTuple):
first_name: str
last_name: str
class PersonGraphQl(graphene.ObjectType):
first_name: graphene.String()
last_name: graphene.String()
full_name: graphene.String()
def resolve_full_name(root: PersonNamedTuple) -> str: # root is a PersonNamedTuple.
return self.first_name + ' ' + self.last_name
class Query(graphene.ObjectType):
person = graphene.Field(PersonGraphQl) # `person` is a PersonGraphQl type.
def resolve_person(root) -> PersonNamedTuple: # But its resolver returns a PersonNamedTuple.
return PersonNamedTuple('Michael', 'Jackson')
```
>>> import json
>>> from typing import NamedTuple
>>> import graphene
>>>
>>> class PersonNamedTuple(NamedTuple):
... first_name: str
... last_name: str
>>>
>>> class PersonGraphQl(graphene.ObjectType):
... first_name = graphene.String()
... last_name = graphene.String()
... full_name = graphene.String()
...
... def resolve_full_name(root: PersonNamedTuple, info) -> str: # root is a PersonNamedTuple.
... return root.first_name + ' ' + root.last_name
>>>
>>> class Query(graphene.ObjectType):
... person = graphene.Field(PersonGraphQl) # `person` is a PersonGraphQl type.
...
... # But its resolver returns a PersonNamedTuple.
... def resolve_person(root, info) -> PersonNamedTuple:
... return PersonNamedTuple('Michael', 'Jackson')
>>>
>>> result = graphene.Schema(query=Query).execute('''
... query person {
... person {
... firstName
... lastName
... fullName
... }
... }
... ''')
>>>
>>> print(json.dumps(result.data))
{"person": {"firstName": "Michael", "lastName": "Jackson", "fullName": "Michael Jackson"}}
It's a little funky, and even funkier for the relay pagination stuff.
Expand Down Expand Up @@ -51,7 +67,7 @@ class Meta:

id = graphene.ID(required=True)
name = graphene.String()
vendor = GraphQlMusicProvider()
song_provider = GraphQlMusicProvider()
artist_name = graphene.String()
album_name = graphene.String()
image_large_url = graphene.String()
Expand Down Expand Up @@ -188,7 +204,7 @@ class Meta:
name = 'ListenInput'

song_id = graphene.String(required=True)
music_provider = GraphQlMusicProvider(default_value=GraphQlMusicProvider.SPOTIFY.value)
song_provider = GraphQlMusicProvider(default_value=GraphQlMusicProvider.SPOTIFY.value)
listener_name = graphene.String(required=True)
note = graphene.String(required=False)
iana_timezone = graphene.String(required=True)
Expand All @@ -204,7 +220,7 @@ class Arguments:
def mutate(root, info: ResolveInfo, input: GraphQlListenInput) -> Listen:
listen = ListenInput(
song_id=input.song_id,
song_provider=MusicProvider(input.music_provider),
song_provider=MusicProvider(input.song_provider),
listener_name=input.listener_name,
note=input.note,
iana_timezone=input.iana_timezone
Expand Down

0 comments on commit e892385

Please sign in to comment.