graphene/examples/starwars/schema.py

53 lines
1.2 KiB
Python
Raw Normal View History

2015-09-24 12:11:50 +03:00
import graphene
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
2016-06-05 00:05:31 +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()
2016-06-04 21:21:33 +03:00
friends = graphene.List(lambda: Character)
2016-06-05 00:05:31 +03:00
appears_in = graphene.List(Episode)
2016-06-04 21:21:33 +03:00
2017-07-27 12:51:25 +03:00
def resolve_friends(self, info):
2016-06-06 12:03:57 +03:00
# The character friends is a list of strings
return [get_character(f) for f in self.friends]
2016-06-04 21:21:33 +03:00
class Human(graphene.ObjectType):
class Meta:
interfaces = (Character,)
2016-06-04 21:21:33 +03:00
home_planet = graphene.String()
2015-09-24 12:11:50 +03:00
2015-09-26 06:01:14 +03:00
class Droid(graphene.ObjectType):
class Meta:
interfaces = (Character,)
primary_function = graphene.String()
2015-09-24 12:11:50 +03:00
class Query(graphene.ObjectType):
hero = graphene.Field(Character, episode=Episode())
human = graphene.Field(Human, id=graphene.String())
droid = graphene.Field(Droid, id=graphene.String())
2015-09-24 12:11:50 +03:00
2017-07-27 12:51:25 +03:00
def resolve_hero(self, info, episode=None):
2015-11-03 06:28:06 +03:00
return get_hero(episode)
2015-09-24 12:11:50 +03:00
2017-07-27 12:51:25 +03:00
def resolve_human(self, info, id):
2015-11-03 06:28:06 +03:00
return get_human(id)
2015-09-24 12:11:50 +03:00
2017-07-27 12:51:25 +03:00
def resolve_droid(self, info, id):
2015-11-03 06:28:06 +03:00
return get_droid(id)
2015-09-24 12:11:50 +03:00
2016-06-04 21:21:33 +03:00
schema = graphene.Schema(query=Query)