graphene/examples/starwars/schema.py

57 lines
1.3 KiB
Python
Raw Normal View History

2015-09-24 12:11:50 +03:00
import graphene
from graphene import resolve_only_args
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
2016-02-02 00:07:30 +03:00
class Episode(graphene.Enum):
NEWHOPE = 4
EMPIRE = 5
JEDI = 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.ID()
name = graphene.String()
friends = graphene.List('Character')
appears_in = graphene.List(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):
home_planet = graphene.String()
2015-09-24 12:11:50 +03:00
class Droid(Character):
primary_function = graphene.String()
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,
id=graphene.String()
2015-10-03 08:17:51 +03:00
)
2015-09-24 12:11:50 +03:00
droid = graphene.Field(Droid,
id=graphene.String()
2015-10-03 08:17:51 +03:00
)
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)