@@ -10,19 +10,20 @@ This example defines a Mutation:
10
10
11
11
.. code :: python
12
12
13
- import graphene
13
+ import graphene
14
14
15
- class CreatePerson (graphene .Mutation ):
16
- class Input :
17
- name = graphene.String()
15
+ class CreatePerson(graphene.Mutation):
16
+ class Input:
17
+ name = graphene.String()
18
18
19
- ok = graphene.Boolean()
20
- person = graphene.Field(lambda : Person)
19
+ ok = graphene.Boolean()
20
+ person = graphene.Field(lambda: Person)
21
21
22
- def mutate (self , args , context , info ):
23
- person = Person(name = args.get(' name' ))
24
- ok = True
25
- return CreatePerson(person = person, ok = ok)
22
+ @staticmethod
23
+ def mutate(root, args, context, info):
24
+ person = Person(name=args.get('name'))
25
+ ok = True
26
+ return CreatePerson(person=person, ok=ok)
26
27
27
28
**person ** and **ok ** are the output fields of the Mutation when is
28
29
resolved.
@@ -42,11 +43,16 @@ So, we can finish our schema like this:
42
43
43
44
class Person (graphene .ObjectType ):
44
45
name = graphene.String()
46
+ age = graphene.Int()
45
47
46
48
class MyMutations (graphene .ObjectType ):
47
49
create_person = CreatePerson.Field()
48
50
49
- schema = graphene.Schema(mutation = MyMutations)
51
+ # We must define a query for our schema
52
+ class Query (graphene .ObjectType ):
53
+ person = graphene.Field(Person)
54
+
55
+ schema = graphene.Schema(query = Query, mutation = MyMutations)
50
56
51
57
Executing the Mutation
52
58
----------------------
@@ -96,11 +102,12 @@ To use an InputField you define an InputObjectType that specifies the structure
96
102
97
103
class CreatePerson (graphene .Mutation ):
98
104
class Input :
99
- person_data = graphene.InputField (PersonInput)
105
+ person_data = graphene.Argument (PersonInput)
100
106
101
107
person = graphene.Field(lambda : Person)
102
108
103
- def mutate (self , args , context , info ):
109
+ @ staticmethod
110
+ def mutate (root , args , context , info ):
104
111
p_data = args.get(' person_data' )
105
112
106
113
name = p_data.get(' name' )
0 commit comments