graphene/examples/starwars/schema.py

63 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
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
@graphene.implements(Character)
class Human(graphene.ObjectType):
home_planet = graphene.String()
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
2016-06-04 21:21:33 +03:00
@graphene.implements(Character)
class Droid(graphene.ObjectType):
primary_function = graphene.String()
2015-09-24 12:11:50 +03:00
2016-06-04 21:21:33 +03:00
def resolve_friends(self, args, *_):
# The character friends is a list of strings
2016-06-04 22:16:18 +03:00
return [get_character(f) for f in self.friends]
2016-06-04 21:21:33 +03:00
2015-09-24 12:11:50 +03:00
class Query(graphene.ObjectType):
hero = graphene.Field(Character,
2016-06-05 00:05:31 +03:00
episode=Episode()
2015-10-03 08:17:51 +03:00
)
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
2016-06-04 21:21:33 +03:00
schema = graphene.Schema(query=Query)