diff --git a/docs/types/mutations.rst b/docs/types/mutations.rst index cb2d780b..8de07f32 100644 --- a/docs/types/mutations.rst +++ b/docs/types/mutations.rst @@ -76,3 +76,69 @@ We should receive: "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 + } + ok + } + } + +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) + +