2015-09-30 09:40:40 +03:00
|
|
|
import graphene
|
|
|
|
from graphene import resolve_only_args, relay
|
|
|
|
from graphene.contrib.django import (
|
|
|
|
DjangoObjectType,
|
|
|
|
DjangoNode
|
|
|
|
)
|
2015-10-11 00:53:46 +03:00
|
|
|
from .models import (
|
|
|
|
Ship as ShipModel, Faction as FactionModel, Character as CharacterModel)
|
2015-09-30 09:40:40 +03:00
|
|
|
from .data import (
|
2015-10-30 10:36:31 +03:00
|
|
|
get_faction,
|
|
|
|
get_ship,
|
|
|
|
get_ships,
|
|
|
|
get_rebels,
|
|
|
|
get_empire,
|
|
|
|
create_ship
|
2015-09-30 09:40:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(name='Starwars Django Relay Schema')
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-30 09:40:40 +03:00
|
|
|
class Ship(DjangoNode):
|
|
|
|
class Meta:
|
2015-10-03 08:17:51 +03:00
|
|
|
model = ShipModel
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_node(cls, id):
|
2015-10-30 10:36:31 +03:00
|
|
|
return Ship(get_ship(id))
|
2015-09-30 09:40:40 +03:00
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-11 00:53:46 +03:00
|
|
|
@schema.register
|
2015-10-27 09:54:51 +03:00
|
|
|
class Character(DjangoObjectType):
|
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
|
|
|
|
|
|
|
class Faction(DjangoNode):
|
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
|
|
|
|
def get_node(cls, id):
|
2015-10-30 10:36:31 +03:00
|
|
|
return Faction(get_faction(id))
|
|
|
|
|
|
|
|
|
|
|
|
class IntroduceShip(relay.ClientIDMutation):
|
|
|
|
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-30 09:40:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
rebels = graphene.Field(Faction)
|
|
|
|
empire = graphene.Field(Faction)
|
|
|
|
node = relay.NodeField()
|
2015-10-01 11:54:52 +03:00
|
|
|
ships = relay.ConnectionField(Ship, description='All the ships.')
|
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_ships(self):
|
2015-10-30 10:36:31 +03:00
|
|
|
return [Ship(s) for s in get_ships()]
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_rebels(self):
|
2015-10-30 10:36:31 +03:00
|
|
|
return Faction(get_rebels())
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_empire(self):
|
2015-10-30 10:36:31 +03:00
|
|
|
return Faction(get_empire())
|
|
|
|
|
|
|
|
|
|
|
|
class Mutation(graphene.ObjectType):
|
|
|
|
introduce_ship = graphene.Field(IntroduceShip)
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
schema.query = Query
|
2015-10-30 10:36:31 +03:00
|
|
|
schema.mutation = Mutation
|