graphene/examples/starwars/schema.py

61 lines
1.4 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
2016-06-06 12:03:57 +03:00
def resolve_friends(self, args, *_):
# 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,
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)