mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-23 18:13:54 +03:00
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
import graphene
|
|
from graphene import resolve_only_args
|
|
|
|
from .data import get_character, get_droid, get_hero, get_human
|
|
|
|
|
|
class Episode(graphene.Enum):
|
|
NEWHOPE = 4
|
|
EMPIRE = 5
|
|
JEDI = 6
|
|
|
|
|
|
class Character(graphene.Interface):
|
|
id = graphene.ID()
|
|
name = graphene.String()
|
|
friends = graphene.List(lambda: Character)
|
|
appears_in = graphene.List(Episode)
|
|
|
|
def resolve_friends(self, args, *_):
|
|
# The character friends is a list of strings
|
|
return [get_character(f) for f in self.friends]
|
|
|
|
|
|
class Human(graphene.ObjectType, Character):
|
|
home_planet = graphene.String()
|
|
|
|
|
|
class Droid(graphene.ObjectType, Character):
|
|
primary_function = graphene.String()
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
hero = graphene.Field(Character,
|
|
episode=Episode()
|
|
)
|
|
human = graphene.Field(Human,
|
|
id=graphene.String()
|
|
)
|
|
droid = graphene.Field(Droid,
|
|
id=graphene.String()
|
|
)
|
|
|
|
@resolve_only_args
|
|
def resolve_hero(self, episode=None):
|
|
return get_hero(episode)
|
|
|
|
@resolve_only_args
|
|
def resolve_human(self, id):
|
|
return get_human(id)
|
|
|
|
@resolve_only_args
|
|
def resolve_droid(self, id):
|
|
return get_droid(id)
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|