mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 12:16:58 +03:00
58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
import graphene
|
|
from graphene import resolve_only_args
|
|
from graphql.core.type import GraphQLEnumValue
|
|
|
|
from .data import getCharacter, getDroid, getHero, getHuman
|
|
|
|
Episode = graphene.Enum('Episode', dict(
|
|
NEWHOPE=GraphQLEnumValue(4),
|
|
EMPIRE=GraphQLEnumValue(5),
|
|
JEDI=GraphQLEnumValue(6)
|
|
))
|
|
|
|
|
|
class Character(graphene.Interface):
|
|
id = graphene.IDField()
|
|
name = graphene.StringField()
|
|
friends = graphene.ListField('self')
|
|
appears_in = graphene.ListField(Episode)
|
|
|
|
def resolve_friends(self, args, *_):
|
|
# The character friends is a list of strings
|
|
return [getCharacter(f) for f in self.friends]
|
|
|
|
|
|
class Human(Character):
|
|
home_planet = graphene.StringField()
|
|
|
|
|
|
class Droid(Character):
|
|
primary_function = graphene.StringField()
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
hero = graphene.Field(Character,
|
|
episode=graphene.Argument(Episode)
|
|
)
|
|
human = graphene.Field(Human,
|
|
id=graphene.Argument(graphene.String)
|
|
)
|
|
droid = graphene.Field(Droid,
|
|
id=graphene.Argument(graphene.String)
|
|
)
|
|
|
|
@resolve_only_args
|
|
def resolve_hero(self, episode=None):
|
|
return getHero(episode)
|
|
|
|
@resolve_only_args
|
|
def resolve_human(self, id):
|
|
return getHuman(id)
|
|
|
|
@resolve_only_args
|
|
def resolve_droid(self, id):
|
|
return getDroid(id)
|
|
|
|
|
|
Schema = graphene.Schema(query=Query)
|