Added mutations in examples

This commit is contained in:
Syrus Akbary 2015-10-30 00:36:31 -07:00
parent a682ef8bc3
commit 97a81120b7
6 changed files with 246 additions and 47 deletions

View File

@ -85,31 +85,30 @@ def initialize():
executor.save() executor.save()
def createShip(shipName, factionId): def create_ship(ship_name, faction_id):
nextShip = len(data['Ship'].keys())+1 new_ship = Ship(
newShip = Ship( name=ship_name,
id=str(nextShip), faction_id=faction_id
name=shipName
) )
newShip.save() new_ship.save()
return newShip return new_ship
def getShip(_id): def get_ship(_id):
return Ship.objects.get(id=_id) return Ship.objects.get(id=_id)
def getShips(): def get_ships():
return Ship.objects.all() return Ship.objects.all()
def getFaction(_id): def get_faction(_id):
return Faction.objects.get(id=_id) return Faction.objects.get(id=_id)
def getRebels(): def get_rebels():
return getFaction(1) return get_faction(1)
def getEmpire(): def get_empire():
return getFaction(2) return get_faction(2)

View File

@ -7,11 +7,12 @@ from graphene.contrib.django import (
from .models import ( from .models import (
Ship as ShipModel, Faction as FactionModel, Character as CharacterModel) Ship as ShipModel, Faction as FactionModel, Character as CharacterModel)
from .data import ( from .data import (
getFaction, get_faction,
getShip, get_ship,
getShips, get_ships,
getRebels, get_rebels,
getEmpire, get_empire,
create_ship
) )
schema = graphene.Schema(name='Starwars Django Relay Schema') schema = graphene.Schema(name='Starwars Django Relay Schema')
@ -23,7 +24,7 @@ class Ship(DjangoNode):
@classmethod @classmethod
def get_node(cls, id): def get_node(cls, id):
return Ship(getShip(id)) return Ship(get_ship(id))
@schema.register @schema.register
@ -38,7 +39,24 @@ class Faction(DjangoNode):
@classmethod @classmethod
def get_node(cls, id): def get_node(cls, id):
return Faction(getFaction(id)) 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)
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
@ -49,15 +67,20 @@ class Query(graphene.ObjectType):
@resolve_only_args @resolve_only_args
def resolve_ships(self): def resolve_ships(self):
return [Ship(s) for s in getShips()] return [Ship(s) for s in get_ships()]
@resolve_only_args @resolve_only_args
def resolve_rebels(self): def resolve_rebels(self):
return Faction(getRebels()) return Faction(get_rebels())
@resolve_only_args @resolve_only_args
def resolve_empire(self): def resolve_empire(self):
return Faction(getEmpire()) return Faction(get_empire())
class Mutation(graphene.ObjectType):
introduce_ship = graphene.Field(IntroduceShip)
schema.query = Query schema.query = Query
schema.mutation = Mutation

View File

@ -0,0 +1,78 @@
import pytest
from ..schema import schema
from ..data import initialize
pytestmark = pytest.mark.django_db
def test_mutations():
initialize()
query = '''
mutation MyMutation {
introduceShip(input:{clientMutationId:"abc", shipName: "Peter", factionId: "1"}) {
ship {
id
name
}
faction {
name
ships {
edges {
node {
id
name
}
}
}
}
}
}
'''
expected = {
'introduceShip': {
'ship': {
'id': 'U2hpcDo5',
'name': 'Peter'
},
'faction': {
'name': 'Alliance to Restore the Republic',
'ships': {
'edges': [{
'node': {
'id': 'U2hpcDox',
'name': 'X-Wing'
}
}, {
'node': {
'id': 'U2hpcDoy',
'name': 'Y-Wing'
}
}, {
'node': {
'id': 'U2hpcDoz',
'name': 'A-Wing'
}
}, {
'node': {
'id': 'U2hpcDo0',
'name': 'Millenium Falcon'
}
}, {
'node': {
'id': 'U2hpcDo1',
'name': 'Home One'
}
}, {
'node': {
'id': 'U2hpcDo5',
'name': 'Peter'
}
}]
},
}
}
}
result = schema.execute(query)
assert not result.errors
assert result.data == expected

View File

@ -77,28 +77,29 @@ def setup():
} }
def createShip(shipName, factionId): def create_ship(ship_name, faction_id):
nextShip = len(data['Ship'].keys())+1 from .schema import Ship
newShip = Ship( next_ship = len(data['Ship'].keys()) + 1
id=str(nextShip), new_ship = Ship(
name=shipName id=str(next_ship),
name=ship_name
) )
data['Ship'][newShip.id] = newShip data['Ship'][new_ship.id] = new_ship
data['Faction'][factionId].ships.append(newShip.id) data['Faction'][faction_id].ships.append(new_ship.id)
return newShip return new_ship
def getShip(_id): def get_ship(_id):
return data['Ship'][_id] return data['Ship'][_id]
def getFaction(_id): def get_faction(_id):
return data['Faction'][_id] return data['Faction'][_id]
def getRebels(): def get_rebels():
return getFaction('1') return get_faction('1')
def getEmpire(): def get_empire():
return getFaction('2') return get_faction('2')

View File

@ -2,10 +2,11 @@ import graphene
from graphene import resolve_only_args, relay from graphene import resolve_only_args, relay
from .data import ( from .data import (
getFaction, get_faction,
getShip, get_ship,
getRebels, get_rebels,
getEmpire, get_empire,
create_ship,
) )
schema = graphene.Schema(name='Starwars Relay Schema') schema = graphene.Schema(name='Starwars Relay Schema')
@ -17,7 +18,7 @@ class Ship(relay.Node):
@classmethod @classmethod
def get_node(cls, id): def get_node(cls, id):
return getShip(id) return get_ship(id)
class Faction(relay.Node): class Faction(relay.Node):
@ -29,11 +30,28 @@ class Faction(relay.Node):
@resolve_only_args @resolve_only_args
def resolve_ships(self, **args): def resolve_ships(self, **args):
# Transform the instance ship_ids into real instances # Transform the instance ship_ids into real instances
return [getShip(ship_id) for ship_id in self.ships] return [get_ship(ship_id) for ship_id in self.ships]
@classmethod @classmethod
def get_node(cls, id): def get_node(cls, id):
return getFaction(id) return 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)
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
@ -43,11 +61,16 @@ class Query(graphene.ObjectType):
@resolve_only_args @resolve_only_args
def resolve_rebels(self): def resolve_rebels(self):
return getRebels() return get_rebels()
@resolve_only_args @resolve_only_args
def resolve_empire(self): def resolve_empire(self):
return getEmpire() return get_empire()
class Mutation(graphene.ObjectType):
introduce_ship = graphene.Field(IntroduceShip)
schema.query = Query schema.query = Query
schema.mutation = Mutation

View File

@ -0,0 +1,75 @@
from ..schema import schema
from ..data import setup
setup()
def test_mutations():
query = '''
mutation MyMutation {
introduceShip(input:{clientMutationId:"abc", shipName: "Peter", factionId: "1"}) {
ship {
id
name
}
faction {
name
ships {
edges {
node {
id
name
}
}
}
}
}
}
'''
expected = {
'introduceShip': {
'ship': {
'id': 'U2hpcDo5',
'name': 'Peter'
},
'faction': {
'name': 'Alliance to Restore the Republic',
'ships': {
'edges': [{
'node': {
'id': 'U2hpcDox',
'name': 'X-Wing'
}
}, {
'node': {
'id': 'U2hpcDoy',
'name': 'Y-Wing'
}
}, {
'node': {
'id': 'U2hpcDoz',
'name': 'A-Wing'
}
}, {
'node': {
'id': 'U2hpcDo0',
'name': 'Millenium Falcon'
}
}, {
'node': {
'id': 'U2hpcDo1',
'name': 'Home One'
}
}, {
'node': {
'id': 'U2hpcDo5',
'name': 'Peter'
}
}]
},
}
}
}
result = schema.execute(query)
assert not result.errors
assert result.data == expected