Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More graphql schema cleanup #7

Merged
merged 2 commits into from
Dec 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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