graphene/tests/starwars_relay/schema.py

51 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-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-09-28 06:37:47 +03:00
return Ship(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.')
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
def resolve_ships(self, **kwargs):
return [Ship(getShip(ship)) for ship in self.instance.ships]
2015-09-26 02:35:17 +03:00
2015-09-26 09:25:10 +03:00
@classmethod
def get_node(cls, id):
2015-09-28 06:37:47 +03:00
return Faction(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):
return Faction(getRebels())
2015-09-26 02:35:17 +03:00
@resolve_only_args
2015-09-26 09:25:10 +03:00
def resolve_empire(self):
return Faction(getEmpire())
2015-09-26 02:35:17 +03:00
schema.query = Query