Commit a4bc7d09 authored by ymir's avatar ymir
Browse files

graphQL mutation address

parent 234454f5
......@@ -18,11 +18,16 @@ class AddressDAO():
connection = connect(DB)
cursor = connection.cursor()
raisedExeption = None
# INFO : we can update input addresses in dictionary keys
newAddresses = list()
try:
results = cursor.execute(statement,parameters)
# insert data
cursor.execute(statement,parameters)
connection.commit()
ids = [id for id in range(results.lastrowid-results.rowcount+1,results.lastrowid+1)]
for index in range(len(ids)): addresses[index].id = ids[index]
# retrieve IDs
for originalAddress in addresses:
newAddress = AddressDAO.read(houseNumber=originalAddress.houseNumber, street=originalAddress.street, zipCode=originalAddress.zipCode, city=originalAddress.city)[0]
newAddresses.append(newAddress)
pass
except Exception as exeception:
raisedExeption = exeception
......@@ -30,7 +35,7 @@ class AddressDAO():
cursor.close()
connection.close()
if raisedExeption : raise raisedExeption
pass
return newAddresses
@staticmethod
def read(ids=None, houseNumber=None, street=None, zipCode=None, city=None, clientsIds=None, limit=None, offset=None):
statement = "SELECT A.HOUSE_NUMBER, A.STREET, A.ZIPCODE, A.CITY, A.ID FROM ADDRESS A"
......
......@@ -6,7 +6,7 @@ from dummypythongraphqlserver.graphQL.leaf.address import LeafAddressField
from dummypythongraphqlserver.graphQL.leaf.purchaseOrder import LeafPurchaseOrderField
from dummypythongraphqlserver.graphQL.leaf.item import LeafItemField
from dummypythongraphqlserver.graphQL.root.client import RootClientField, ClientInput, DeleteClients, CreateClients, UpdateClients
from dummypythongraphqlserver.graphQL.root.address import RootAddressField, AddressInput
from dummypythongraphqlserver.graphQL.root.address import RootAddressField, AddressInput, DeleteAddresses, CreateAddresses, UpdateAddresses
from dummypythongraphqlserver.graphQL.root.purchaseOrder import RootPurchaseOrderField, PurchaseOrderInput
from dummypythongraphqlserver.graphQL.root.item import RootItemField, ItemInput
# query
......@@ -52,6 +52,9 @@ class Mutation(ObjectType):
deleteClients = DeleteClients.Field()
createClients = CreateClients.Field()
updateClients = UpdateClients.Field()
deleteAddresses = DeleteAddresses.Field()
createAddresses = CreateAddresses.Field()
updateAddresses = UpdateAddresses.Field()
# schema
schema = Schema(query=Query,mutation=Mutation)
pass
# coding=utf-8
# import
from graphene import Int, String, Field, List, NonNull
from graphene import Int, String, Field, List, NonNull, Mutation, ID, InputObjectType, Boolean
from dummypythongraphqlserver.entities import Address
from dummypythongraphqlserver.graphQL.leaf.address import LeafAddressField
from dummypythongraphqlserver.graphQL.leaf.client import LeafClientField
from dummypythongraphqlserver.graphQL.node.purchaseOrder import NodePurchaseOrderField
from dummypythongraphqlserver.graphQL.root import CommonInput
from dummypythongraphqlserver.dataAccess.address import AddressDAO
# input
class AddressInput(CommonInput):
houseNumber = Int()
......@@ -29,4 +31,63 @@ class RootAddressField(LeafAddressField):
rootClientFields.append(rootClientField)
return rootClientFields
pass
# mutation
class DeleteAddressInput(InputObjectType):
ids = List(ID)
houseNumber = Int()
street = String()
zipCode = Int()
city = String()
class DeleteAddresses(Mutation):
class Arguments:
deleteAddressInput = DeleteAddressInput(required=True)
# INFO : at least 1 return field is needed by graphene framework
confirmation = Boolean(required=True)
def mutate(self, _, deleteAddressInput):
AddressDAO.delete(ids=deleteAddressInput.ids, houseNumber=deleteAddressInput.houseNumber, street=deleteAddressInput.street, zipCode=deleteAddressInput.zipCode, city=deleteAddressInput.city)
return DeleteAddresses(confirmation=True)
pass
class CreateAddressInput(InputObjectType):
houseNumber = Int()
street = String()
zipCode = Int()
city = String()
class CreateAddresses(Mutation):
class Arguments:
createAddressInput = List(NonNull(CreateAddressInput), required=True)
withAddress = Boolean()
addresses = Field(List(NonNull(LeafAddressField)), required=True)
'''INFO : keep all parameters, even the ones you do not use later
they will be used by graphen framework
i.e : withAddress, withClient(s), withPurchaseOrders, withOrderLines'''
def mutate(self, _, createAddressInput, withAddress=True):
# create each address
entitiesAddressInputs = list()
for lineAddressInput in createAddressInput:
entityAddress = Address(houseNumber=lineAddressInput.houseNumber, street=lineAddressInput.street, zipCode=lineAddressInput.zipCode, city=lineAddressInput.city)
entitiesAddressInputs.append(entityAddress)
pass
addressOutputs = AddressDAO.create(entitiesAddressInputs)
return CreateAddresses(addresses=addressOutputs)
pass
class UpdateAddressInput(InputObjectType):
id = ID(required=True)
houseNumber = Int()
street = String()
zipCode = Int()
city = String()
class UpdateAddresses(Mutation):
class Arguments:
updateAddressInput = List(NonNull(UpdateAddressInput), required=True)
confirmation = Boolean(required=True)
def mutate(self, _, updateAddressInput):
# update each address
entitiesAddressInputs = list()
for lineAddressInput in updateAddressInput:
entityAddress = Address(id=lineAddressInput.id, houseNumber=lineAddressInput.houseNumber, street=lineAddressInput.street, zipCode=lineAddressInput.zipCode, city=lineAddressInput.city)
entitiesAddressInputs.append(entityAddress)
pass
AddressDAO.update(entitiesAddressInputs)
return UpdateAddresses(confirmation=True)
pass
pass
......@@ -108,5 +108,70 @@ class testAddress(TestCase):
result = schema.execute(query)
self.assertEqual(result.errors[0].message,"""Unknown argument "addressInput_" on field "addresses" of type "Query". Did you mean "addressInput"?""")
pass
def testAddress09(self):
# clean
deleteMutation = """mutation {
deleteAddresses(deleteAddressInput:{houseNumber:1, street:"STREET#", zipCode:10, city:"CITY#"}) {confirmation}
}"""
schema.execute(deleteMutation)
# create
mutation = """mutation {
createAddresses (createAddressInput:[{houseNumber:10, street:"STREET#0", zipCode:100, city:"CITY#0"},{houseNumber:11, street:"STREET#1", zipCode:101, city:"CITY#1"}]) {
addresses {
id
}
}
}"""
result = schema.execute(mutation)
ids=list()
for address in result.data["createAddresses"]["addresses"] :
self.assertIsNotNone(address["id"])
ids.append(address["id"])
# update
mutation = """mutation {
updateAddresses (updateAddressInput:[{id:"""+ids[0]+""", houseNumber:12, street:"STREET#2", zipCode:102, city:"CITY#2"},{id:"""+ids[1]+""", houseNumber:103, street:"STREET#3", zipCode:103, city:"CITY#3"}]) {confirmation}
}"""
result = schema.execute(mutation)
self.assertTrue(result.data["updateAddresses"]["confirmation"])
# delete
result = schema.execute(deleteMutation)
self.assertTrue(result.data["deleteAddresses"]["confirmation"])
pass
def testAddress10(self):
# clean
deleteMutation = """mutation deleteMutation ($deleteAddressInput:DeleteAddressInput!) {
deleteAddresses(deleteAddressInput:$deleteAddressInput) {confirmation}
}"""
deleteVariables = {"deleteAddressInput": {"firstName":"first_name#", "lastName":"first_name#"}}
schema.execute(deleteMutation,variables=deleteVariables)
# create
mutation = """mutation createMutation ($createAddressInput:[CreateAddressInput!]!, $withAddress:Boolean!) {
createAddresses (createAddressInput:$createAddressInput, withAddress:$withAddress) {
addresses {
houseNumber,
street,
zipCode,
city
}
}
}"""
variables = {"createAddressInput": [{"firstName":"first_name#0", "lastName":"first_name#0", "addressId":1},{"firstName":"first_name#1", "lastName":"first_name#1", "addressId":2}], "withAddress": True}
result = schema.execute(mutation,variables=variables)
ids=list()
for address in result.data["createAddresses"]["addresses"] :
self.assertIsNotNone(address["id"])
self.assertIsNotNone(address["address"])
ids.append(address["id"])
# update
mutation = """mutation updateMutation ($updateAddressInput:[UpdateAddressInput!]!) {
updateAddresses (updateAddressInput:$updateAddressInput) {confirmation}
}"""
variables = {"updateAddressInput": [{"id":ids[0], "firstName":"first_name#2", "lastName":"first_name#2", "addressId":3},{"id":ids[1], "firstName":"first_name#3", "lastName":"first_name#3", "addressId":4}]}
result = schema.execute(mutation,variables=variables)
self.assertTrue(result.data["updateAddresses"]["confirmation"])
# delete
result = schema.execute(deleteMutation,variables=deleteVariables)
self.assertTrue(result.data["deleteAddresses"]["confirmation"])
pass
pass
pass
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment