2015-09-26 02:35:17 +03:00
|
|
|
import graphene
|
2015-09-29 12:29:38 +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-29 09:29:10 +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):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-26 09:25:10 +03:00
|
|
|
'''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):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-26 09:25:10 +03:00
|
|
|
'''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
|
|
|
|
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
|
|
|
|
|
|
|
|
2015-09-29 12:29:38 +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-29 09:29:10 +03:00
|
|
|
schema.query = Query
|