mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-30 23:47:55 +03:00 
			
		
		
		
	Added documentation for InputFields and InputObjectTypes usage
This commit is contained in:
		
							parent
							
								
									8128292b02
								
							
						
					
					
						commit
						b179d012d7
					
				|  | @ -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) | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user