2015-09-26 02:35:17 +03:00
|
|
|
import graphene
|
2015-10-31 23:46:43 +03:00
|
|
|
from graphene import relay, resolve_only_args
|
2015-09-26 02:35:17 +03:00
|
|
|
|
2015-10-31 23:46:43 +03:00
|
|
|
from .data import create_ship, get_empire, get_faction, get_rebels, get_ship
|
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):
|
|
|
|
'''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-30 10:36:31 +03:00
|
|
|
return get_ship(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
|
2015-10-30 10:36:31 +03:00
|
|
|
return [get_ship(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-30 10:36:31 +03:00
|
|
|
return get_faction(id)
|
|
|
|
|
|
|
|
|
|
|
|
class IntroduceShip(relay.ClientIDMutation):
|
2015-10-31 23:46:43 +03:00
|
|
|
|
2015-10-30 10:36:31 +03:00
|
|
|
class Input:
|
|
|
|
ship_name = graphene.StringField(required=True)
|
|
|
|
faction_id = graphene.StringField(required=True)
|
|
|
|
|
|
|
|
ship = graphene.Field(Ship)
|
|
|
|
faction = graphene.Field(Faction)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def mutate_and_get_payload(cls, input, info):
|
|
|
|
ship_name = input.get('ship_name')
|
|
|
|
faction_id = input.get('faction_id')
|
|
|
|
ship = create_ship(ship_name, faction_id)
|
|
|
|
faction = get_faction(faction_id)
|
|
|
|
return IntroduceShip(ship=ship, faction=faction)
|
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):
|
2015-10-30 10:36:31 +03:00
|
|
|
return get_rebels()
|
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-30 10:36:31 +03:00
|
|
|
return get_empire()
|
|
|
|
|
|
|
|
|
|
|
|
class Mutation(graphene.ObjectType):
|
|
|
|
introduce_ship = graphene.Field(IntroduceShip)
|
2015-09-26 02:35:17 +03:00
|
|
|
|
|
|
|
|
2015-09-29 09:29:10 +03:00
|
|
|
schema.query = Query
|
2015-10-30 10:36:31 +03:00
|
|
|
schema.mutation = Mutation
|