graphene/tests/starwars_relay/schema.py

66 lines
1.4 KiB
Python
Raw Normal View History

2015-09-26 02:35:17 +03:00
import graphene
2015-09-26 02:36:18 +03:00
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
2015-09-26 13:36:00 +03:00
schema = graphene.Schema()
2015-09-26 02:35:17 +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):
ship = getShip(id)
if ship:
return Ship(ship)
2015-09-26 02:35:17 +03:00
2015-09-26 13:36:00 +03:00
# class Meta:
# schema = schema
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):
faction = getFaction(id)
if faction:
return Faction(faction)
2015-09-26 02:35:17 +03:00
2015-09-26 13:36:00 +03:00
# class Meta:
# schema = schema
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
2015-09-26 13:36:00 +03:00
# class Meta:
# schema = schema
print '*CACA', schema._types
schema.query = Query
Schema = schema