mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-23 01:56:54 +03:00
Merge pull request #365 from BossGrand/master
Added documentation for InputFields and InputObjectTypes usage
This commit is contained in:
commit
815f7528c5
|
@ -76,3 +76,69 @@ We should receive:
|
||||||
"ok": true
|
"ok": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputFields and InputObjectTypes
|
||||||
|
----------------------
|
||||||
|
InputFields are used in mutations to allow nested input data for mutations
|
||||||
|
|
||||||
|
To use an InputField you define an InputObjectType that specifies the structure of your input data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
class PersonInput(graphene.InputObjectType):
|
||||||
|
name = graphene.String()
|
||||||
|
age = graphene.Int()
|
||||||
|
|
||||||
|
class CreatePerson(graphene.Mutation):
|
||||||
|
class Input:
|
||||||
|
person_data = graphene.InputField(PersonInput)
|
||||||
|
|
||||||
|
person = graphene.Field(lambda: Person)
|
||||||
|
|
||||||
|
def mutate(self, args, context, info):
|
||||||
|
p_data = args.get('person_data')
|
||||||
|
|
||||||
|
name = p_data.get('name')
|
||||||
|
age = p_data.get('age')
|
||||||
|
|
||||||
|
person = Person(name=name, age=age)
|
||||||
|
return CreatePerson(person=person)
|
||||||
|
|
||||||
|
|
||||||
|
Note that **name** and **age** are part of **person_data** now
|
||||||
|
|
||||||
|
Using the above mutation your new query would look like this:
|
||||||
|
|
||||||
|
.. code:: graphql
|
||||||
|
|
||||||
|
mutation myFirstMutation {
|
||||||
|
createPerson(personData: {name:"Peter", age: 24}) {
|
||||||
|
person {
|
||||||
|
name,
|
||||||
|
age
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InputObjectTypes can also be fields of InputObjectTypes allowing you to have
|
||||||
|
as complex of input data as you need
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
class LatLngInput(graphene.InputObjectType):
|
||||||
|
lat = graphene.Float()
|
||||||
|
lng = graphene.Float()
|
||||||
|
|
||||||
|
#A location has a latlng associated to it
|
||||||
|
class LocationInput(graphene.InputObjectType):
|
||||||
|
name = graphene.String()
|
||||||
|
latlng = graphene.InputField(LatLngInputType)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user