graphene/examples/starwars_django/schema.py

64 lines
1.3 KiB
Python
Raw Normal View History

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 (
getFaction,
getShip,
getShips,
2015-09-30 09:40:40 +03:00
getRebels,
getEmpire,
)
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):
return Ship(getShip(id))
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):
return Faction(getFaction(id))
class Query(graphene.ObjectType):
rebels = graphene.Field(Faction)
empire = graphene.Field(Faction)
node = relay.NodeField()
ships = relay.ConnectionField(Ship, description='All the ships.')
@resolve_only_args
def resolve_ships(self):
return [Ship(s) for s in getShips()]
2015-09-30 09:40:40 +03:00
@resolve_only_args
def resolve_rebels(self):
return Faction(getRebels())
@resolve_only_args
def resolve_empire(self):
return Faction(getEmpire())
schema.query = Query