Skip to content

Commit 815f752

Browse files
authored
Merge pull request #365 from BossGrand/master
Added documentation for InputFields and InputObjectTypes usage
2 parents b1bffc4 + 85cad0e commit 815f752

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

docs/types/mutations.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,69 @@ We should receive:
7676
"ok": true
7777
}
7878
}
79+
80+
InputFields and InputObjectTypes
81+
----------------------
82+
InputFields are used in mutations to allow nested input data for mutations
83+
84+
To use an InputField you define an InputObjectType that specifies the structure of your input data
85+
86+
87+
88+
89+
.. code:: python
90+
91+
import graphene
92+
93+
class PersonInput(graphene.InputObjectType):
94+
name = graphene.String()
95+
age = graphene.Int()
96+
97+
class CreatePerson(graphene.Mutation):
98+
class Input:
99+
person_data = graphene.InputField(PersonInput)
100+
101+
person = graphene.Field(lambda: Person)
102+
103+
def mutate(self, args, context, info):
104+
p_data = args.get('person_data')
105+
106+
name = p_data.get('name')
107+
age = p_data.get('age')
108+
109+
person = Person(name=name, age=age)
110+
return CreatePerson(person=person)
111+
112+
113+
Note that **name** and **age** are part of **person_data** now
114+
115+
Using the above mutation your new query would look like this:
116+
117+
.. code:: graphql
118+
119+
mutation myFirstMutation {
120+
createPerson(personData: {name:"Peter", age: 24}) {
121+
person {
122+
name,
123+
age
124+
}
125+
}
126+
}
127+
128+
InputObjectTypes can also be fields of InputObjectTypes allowing you to have
129+
as complex of input data as you need
130+
131+
.. code:: python
132+
133+
import graphene
134+
135+
class LatLngInput(graphene.InputObjectType):
136+
lat = graphene.Float()
137+
lng = graphene.Float()
138+
139+
#A location has a latlng associated to it
140+
class LocationInput(graphene.InputObjectType):
141+
name = graphene.String()
142+
latlng = graphene.InputField(LatLngInputType)
143+
144+

0 commit comments

Comments
 (0)