2016-09-18 02:29:00 +03:00
|
|
|
import graphene
|
2016-09-18 03:09:56 +03:00
|
|
|
from graphene import Schema, relay, resolve_only_args
|
2016-09-20 08:15:10 +03:00
|
|
|
from graphene_django import DjangoConnectionField, DjangoObjectType
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2019-06-11 06:54:30 +03:00
|
|
|
from .data import create_ship, get_empire, get_faction, get_rebels, get_ship, get_ships
|
2016-09-18 03:09:56 +03:00
|
|
|
from .models import Character as CharacterModel
|
|
|
|
from .models import Faction as FactionModel
|
|
|
|
from .models import Ship as ShipModel
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Ship(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = ShipModel
|
2019-06-11 06:54:30 +03:00
|
|
|
interfaces = (relay.Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
@classmethod
|
2017-08-01 08:42:16 +03:00
|
|
|
def get_node(cls, info, id):
|
2016-09-18 02:29:00 +03:00
|
|
|
node = get_ship(id)
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
|
|
class Character(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = CharacterModel
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Faction(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = FactionModel
|
2019-06-11 06:54:30 +03:00
|
|
|
interfaces = (relay.Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
@classmethod
|
2017-08-01 08:42:16 +03:00
|
|
|
def get_node(cls, info, id):
|
2016-09-18 02:29:00 +03:00
|
|
|
return get_faction(id)
|
|
|
|
|
|
|
|
|
|
|
|
class IntroduceShip(relay.ClientIDMutation):
|
|
|
|
class Input:
|
|
|
|
ship_name = graphene.String(required=True)
|
|
|
|
faction_id = graphene.String(required=True)
|
|
|
|
|
|
|
|
ship = graphene.Field(Ship)
|
|
|
|
faction = graphene.Field(Faction)
|
|
|
|
|
|
|
|
@classmethod
|
2019-06-11 06:54:30 +03:00
|
|
|
def mutate_and_get_payload(
|
|
|
|
cls, root, info, ship_name, faction_id, client_mutation_id=None
|
|
|
|
):
|
2016-09-18 02:29:00 +03:00
|
|
|
ship = create_ship(ship_name, faction_id)
|
|
|
|
faction = get_faction(faction_id)
|
|
|
|
return IntroduceShip(ship=ship, faction=faction)
|
|
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
rebels = graphene.Field(Faction)
|
|
|
|
empire = graphene.Field(Faction)
|
|
|
|
node = relay.Node.Field()
|
2019-06-11 06:54:30 +03:00
|
|
|
ships = DjangoConnectionField(Ship, description="All the ships.")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_ships(self):
|
|
|
|
return get_ships()
|
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_rebels(self):
|
|
|
|
return get_rebels()
|
|
|
|
|
|
|
|
@resolve_only_args
|
|
|
|
def resolve_empire(self):
|
|
|
|
return get_empire()
|
|
|
|
|
|
|
|
|
|
|
|
class Mutation(graphene.ObjectType):
|
|
|
|
introduce_ship = IntroduceShip.Field()
|
|
|
|
|
|
|
|
|
|
|
|
# We register the Character Model because if not would be
|
|
|
|
# inaccessible for the schema
|
|
|
|
schema = Schema(query=Query, mutation=Mutation, types=[Ship, Character])
|