2015-09-30 09:40:40 +03:00
|
|
|
import graphene
|
2016-06-19 00:33:04 +03:00
|
|
|
from graphene import relay, resolve_only_args, Schema
|
|
|
|
from graphene_django import DjangoNode, DjangoObjectType
|
2015-10-31 23:46:43 +03:00
|
|
|
|
|
|
|
from .data import (create_ship, get_empire, get_faction, get_rebels, get_ship,
|
|
|
|
get_ships)
|
2016-06-19 00:33:04 +03:00
|
|
|
from .models import (
|
|
|
|
Character as CharacterModel,
|
|
|
|
Faction as FactionModel,
|
|
|
|
Ship as ShipModel
|
|
|
|
)
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
|
2016-06-19 00:33:04 +03:00
|
|
|
class Ship(DjangoNode, DjangoObjectType):
|
2015-11-12 07:02:57 +03:00
|
|
|
|
2015-09-30 09:40:40 +03:00
|
|
|
class Meta:
|
2015-10-03 08:17:51 +03:00
|
|
|
model = ShipModel
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@classmethod
|
2016-06-19 00:33:04 +03:00
|
|
|
def get_node(cls, id, context, info):
|
|
|
|
node = get_ship(id)
|
|
|
|
print(node)
|
|
|
|
return node
|
2015-09-30 09:40:40 +03:00
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-27 09:54:51 +03:00
|
|
|
class Character(DjangoObjectType):
|
2015-11-12 07:02:57 +03:00
|
|
|
|
2015-10-11 00:53:46 +03:00
|
|
|
class Meta:
|
|
|
|
model = CharacterModel
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-11 00:53:46 +03:00
|
|
|
|
2016-06-19 00:33:04 +03:00
|
|
|
class Faction(DjangoNode, DjangoObjectType):
|
2015-11-12 07:02:57 +03:00
|
|
|
|
2015-09-30 09:40:40 +03:00
|
|
|
class Meta:
|
2015-10-03 08:17:51 +03:00
|
|
|
model = FactionModel
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@classmethod
|
2016-06-19 00:33:04 +03:00
|
|
|
def get_node(cls, id, context, info):
|
|
|
|
return get_faction(id)
|
2015-10-30 10:36:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
class IntroduceShip(relay.ClientIDMutation):
|
2015-11-12 07:02:57 +03:00
|
|
|
|
2015-10-30 10:36:31 +03:00
|
|
|
class Input:
|
2015-11-12 09:15:28 +03:00
|
|
|
ship_name = graphene.String(required=True)
|
|
|
|
faction_id = graphene.String(required=True)
|
2015-10-30 10:36:31 +03:00
|
|
|
|
|
|
|
ship = graphene.Field(Ship)
|
|
|
|
faction = graphene.Field(Faction)
|
|
|
|
|
|
|
|
@classmethod
|
2016-06-20 00:30:33 +03:00
|
|
|
def mutate_and_get_payload(cls, input, context, info):
|
|
|
|
ship_name = input.get('shipName')
|
|
|
|
faction_id = input.get('factionId')
|
2015-10-30 10:36:31 +03:00
|
|
|
ship = create_ship(ship_name, faction_id)
|
|
|
|
faction = get_faction(faction_id)
|
2016-06-19 00:33:04 +03:00
|
|
|
return IntroduceShip(ship=ship, faction=faction)
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
rebels = graphene.Field(Faction)
|
|
|
|
empire = graphene.Field(Faction)
|
2016-06-20 00:30:33 +03:00
|
|
|
node = DjangoNode.Field()
|
2015-10-01 11:54:52 +03:00
|
|
|
ships = relay.ConnectionField(Ship, description='All the ships.')
|
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_ships(self):
|
2015-12-04 05:24:19 +03:00
|
|
|
return get_ships()
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_rebels(self):
|
2015-12-04 05:24:19 +03:00
|
|
|
return get_rebels()
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_empire(self):
|
2015-12-04 05:24:19 +03:00
|
|
|
return get_empire()
|
2015-10-30 10:36:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Mutation(graphene.ObjectType):
|
2016-06-20 00:30:33 +03:00
|
|
|
introduce_ship = IntroduceShip.Field()
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
|
2015-11-03 06:36:49 +03:00
|
|
|
# We register the Character Model because if not would be
|
|
|
|
# inaccessible for the schema
|
2016-06-20 00:30:33 +03:00
|
|
|
schema = Schema(query=Query, mutation=Mutation, types=[Ship, Character])
|