@@ -76,3 +76,69 @@ We should receive:
76
76
"ok" : true
77
77
}
78
78
}
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