graphene/examples/starwars_relay/schema.py

54 lines
1.2 KiB
Python
Raw Normal View History

2015-09-26 02:35:17 +03:00
import graphene
from graphene import resolve_only_args, relay
2015-09-26 02:35:17 +03:00
2015-09-26 02:36:18 +03:00
from .data import (
2015-09-26 09:25:10 +03:00
getFaction,
getShip,
getRebels,
getEmpire,
)
2015-09-26 02:35:17 +03:00
schema = graphene.Schema(name='Starwars Relay Schema')
2015-10-03 08:17:51 +03:00
2015-09-26 09:25:10 +03:00
class Ship(relay.Node):
'''A ship in the Star Wars saga'''
name = graphene.StringField(description='The name of the ship.')
2015-09-26 02:36:18 +03:00
2015-09-26 09:25:10 +03:00
@classmethod
def get_node(cls, id):
2015-10-27 09:54:51 +03:00
return getShip(id)
2015-09-26 02:35:17 +03:00
2015-09-26 02:36:18 +03:00
2015-09-26 09:25:10 +03:00
class Faction(relay.Node):
'''A faction in the Star Wars saga'''
name = graphene.StringField(description='The name of the faction.')
2015-10-03 08:17:51 +03:00
ships = relay.ConnectionField(
Ship, description='The ships used by the faction.')
2015-09-26 02:35:17 +03:00
2015-09-26 09:25:10 +03:00
@resolve_only_args
2015-10-27 09:54:51 +03:00
def resolve_ships(self, **args):
# Transform the instance ship_ids into real instances
return [getShip(ship_id) for ship_id in self.ships]
2015-09-26 02:35:17 +03:00
2015-09-26 09:25:10 +03:00
@classmethod
def get_node(cls, id):
2015-10-27 09:54:51 +03:00
return getFaction(id)
2015-09-26 02:35:17 +03:00
class Query(graphene.ObjectType):
2015-09-26 09:25:10 +03:00
rebels = graphene.Field(Faction)
empire = graphene.Field(Faction)
node = relay.NodeField()
2015-09-26 02:35:17 +03:00
@resolve_only_args
2015-09-26 09:25:10 +03:00
def resolve_rebels(self):
2015-10-27 09:54:51 +03:00
return getRebels()
2015-09-26 02:35:17 +03:00
@resolve_only_args
2015-09-26 09:25:10 +03:00
def resolve_empire(self):
2015-10-27 09:54:51 +03:00
return getEmpire()
2015-09-26 02:35:17 +03:00
schema.query = Query