mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 09:36:44 +03:00
Improved mutation examples
This commit is contained in:
parent
7455063728
commit
f68682e153
|
@ -5,6 +5,10 @@ class GeoInput(graphene.InputObjectType):
|
||||||
lat = graphene.Float(required=True)
|
lat = graphene.Float(required=True)
|
||||||
lng = graphene.Float(required=True)
|
lng = graphene.Float(required=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def latlng(self):
|
||||||
|
return "({},{})".format(self.lat, self.lng)
|
||||||
|
|
||||||
|
|
||||||
class Address(graphene.ObjectType):
|
class Address(graphene.ObjectType):
|
||||||
latlng = graphene.String()
|
latlng = graphene.String()
|
||||||
|
@ -14,10 +18,25 @@ class Query(graphene.ObjectType):
|
||||||
address = graphene.Field(Address, geo=GeoInput(required=True))
|
address = graphene.Field(Address, geo=GeoInput(required=True))
|
||||||
|
|
||||||
def resolve_address(self, info, geo):
|
def resolve_address(self, info, geo):
|
||||||
return Address(latlng="({},{})".format(geo.get('lat'), geo.get('lng')))
|
return Address(latlng=geo.latlng)
|
||||||
|
|
||||||
|
|
||||||
schema = graphene.Schema(query=Query)
|
class CreateAddress(graphene.Mutation):
|
||||||
|
|
||||||
|
class Arguments:
|
||||||
|
geo = GeoInput(required=True)
|
||||||
|
|
||||||
|
Output = Address
|
||||||
|
|
||||||
|
def mutate(self, info, geo):
|
||||||
|
return Address(latlng=geo.latlng)
|
||||||
|
|
||||||
|
|
||||||
|
class Mutation(graphene.ObjectType):
|
||||||
|
create_address = CreateAddress.Field()
|
||||||
|
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query, mutation=Mutation)
|
||||||
query = '''
|
query = '''
|
||||||
query something{
|
query something{
|
||||||
address(geo: {lat:32.2, lng:12}) {
|
address(geo: {lat:32.2, lng:12}) {
|
||||||
|
@ -25,6 +44,13 @@ query = '''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
|
mutation = '''
|
||||||
|
mutation addAddress{
|
||||||
|
createAddress(geo: {lat:32.2, lng:12}) {
|
||||||
|
latlng
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
def test_query():
|
def test_query():
|
||||||
|
@ -37,6 +63,16 @@ def test_query():
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_mutation():
|
||||||
|
result = schema.execute(mutation)
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {
|
||||||
|
'createAddress': {
|
||||||
|
'latlng': "(32.2,12.0)",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
result = schema.execute(query)
|
result = schema.execute(query)
|
||||||
print(result.data['address']['latlng'])
|
print(result.data['address']['latlng'])
|
||||||
|
|
|
@ -54,7 +54,7 @@ def test_mutation_custom_output_type():
|
||||||
|
|
||||||
class CreateUser(Mutation):
|
class CreateUser(Mutation):
|
||||||
|
|
||||||
class Input:
|
class Arguments:
|
||||||
name = String()
|
name = String()
|
||||||
|
|
||||||
Output = User
|
Output = User
|
||||||
|
@ -73,7 +73,7 @@ def test_mutation_custom_output_type():
|
||||||
def test_mutation_execution():
|
def test_mutation_execution():
|
||||||
class CreateUser(Mutation):
|
class CreateUser(Mutation):
|
||||||
|
|
||||||
class Input:
|
class Arguments:
|
||||||
name = String()
|
name = String()
|
||||||
dynamic = Dynamic(lambda: String())
|
dynamic = Dynamic(lambda: String())
|
||||||
dynamic_none = Dynamic(lambda: None)
|
dynamic_none = Dynamic(lambda: None)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user