graphene/examples/starwars/schema.py

58 lines
1.5 KiB
Python
Raw Normal View History

2015-09-24 12:11:50 +03:00
import graphene
from graphene import resolve_only_args
from graphql.core.type import GraphQLEnumValue
2015-09-24 12:11:50 +03:00
2015-11-03 06:28:06 +03:00
from .data import get_character, get_droid, get_hero, get_human
2015-09-24 12:11:50 +03:00
Episode = graphene.Enum('Episode', dict(
NEWHOPE=GraphQLEnumValue(4),
EMPIRE=GraphQLEnumValue(5),
JEDI=GraphQLEnumValue(6)
2015-09-24 12:11:50 +03:00
))
2015-09-26 06:01:14 +03:00
2015-09-24 12:11:50 +03:00
class Character(graphene.Interface):
id = graphene.IDField()
name = graphene.StringField()
2015-11-11 04:50:11 +03:00
friends = graphene.ListField('Character')
2015-10-27 09:54:51 +03:00
appears_in = graphene.ListField(Episode)
2015-09-24 12:11:50 +03:00
def resolve_friends(self, args, *_):
2015-10-27 09:54:51 +03:00
# The character friends is a list of strings
2015-11-03 06:28:06 +03:00
return [get_character(f) for f in self.friends]
2015-09-24 12:11:50 +03:00
2015-09-26 06:01:14 +03:00
2015-09-24 12:11:50 +03:00
class Human(Character):
2015-10-27 09:54:51 +03:00
home_planet = graphene.StringField()
2015-09-24 12:11:50 +03:00
class Droid(Character):
2015-10-27 09:54:51 +03:00
primary_function = graphene.StringField()
2015-09-24 12:11:50 +03:00
class Query(graphene.ObjectType):
hero = graphene.Field(Character,
2015-10-03 08:17:51 +03:00
episode=graphene.Argument(Episode)
)
2015-09-24 12:11:50 +03:00
human = graphene.Field(Human,
2015-10-03 08:17:51 +03:00
id=graphene.Argument(graphene.String)
)
2015-09-24 12:11:50 +03:00
droid = graphene.Field(Droid,
2015-10-03 08:17:51 +03:00
id=graphene.Argument(graphene.String)
)
2015-09-24 12:11:50 +03:00
@resolve_only_args
2015-10-07 08:53:43 +03:00
def resolve_hero(self, episode=None):
2015-11-03 06:28:06 +03:00
return get_hero(episode)
2015-09-24 12:11:50 +03:00
@resolve_only_args
def resolve_human(self, id):
2015-11-03 06:28:06 +03:00
return get_human(id)
2015-09-24 12:11:50 +03:00
@resolve_only_args
def resolve_droid(self, id):
2015-11-03 06:28:06 +03:00
return get_droid(id)
2015-09-24 12:11:50 +03:00
Schema = graphene.Schema(query=Query)