mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 17:46:57 +03:00
Improved Mutation docs. Fixed #402
This commit is contained in:
parent
1a52cf9a3d
commit
8b0cd488d4
|
@ -10,19 +10,20 @@ This example defines a Mutation:
|
||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
|
||||||
import graphene
|
import graphene
|
||||||
|
|
||||||
class CreatePerson(graphene.Mutation):
|
class CreatePerson(graphene.Mutation):
|
||||||
class Input:
|
class Input:
|
||||||
name = graphene.String()
|
name = graphene.String()
|
||||||
|
|
||||||
ok = graphene.Boolean()
|
ok = graphene.Boolean()
|
||||||
person = graphene.Field(lambda: Person)
|
person = graphene.Field(lambda: Person)
|
||||||
|
|
||||||
def mutate(self, args, context, info):
|
@staticmethod
|
||||||
person = Person(name=args.get('name'))
|
def mutate(root, args, context, info):
|
||||||
ok = True
|
person = Person(name=args.get('name'))
|
||||||
return CreatePerson(person=person, ok=ok)
|
ok = True
|
||||||
|
return CreatePerson(person=person, ok=ok)
|
||||||
|
|
||||||
**person** and **ok** are the output fields of the Mutation when is
|
**person** and **ok** are the output fields of the Mutation when is
|
||||||
resolved.
|
resolved.
|
||||||
|
@ -42,11 +43,16 @@ So, we can finish our schema like this:
|
||||||
|
|
||||||
class Person(graphene.ObjectType):
|
class Person(graphene.ObjectType):
|
||||||
name = graphene.String()
|
name = graphene.String()
|
||||||
|
age = graphene.Int()
|
||||||
|
|
||||||
class MyMutations(graphene.ObjectType):
|
class MyMutations(graphene.ObjectType):
|
||||||
create_person = CreatePerson.Field()
|
create_person = CreatePerson.Field()
|
||||||
|
|
||||||
schema = graphene.Schema(mutation=MyMutations)
|
# We must define a query for our schema
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
person = graphene.Field(Person)
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query, mutation=MyMutations)
|
||||||
|
|
||||||
Executing the Mutation
|
Executing the Mutation
|
||||||
----------------------
|
----------------------
|
||||||
|
@ -96,11 +102,12 @@ To use an InputField you define an InputObjectType that specifies the structure
|
||||||
|
|
||||||
class CreatePerson(graphene.Mutation):
|
class CreatePerson(graphene.Mutation):
|
||||||
class Input:
|
class Input:
|
||||||
person_data = graphene.InputField(PersonInput)
|
person_data = graphene.Argument(PersonInput)
|
||||||
|
|
||||||
person = graphene.Field(lambda: Person)
|
person = graphene.Field(lambda: Person)
|
||||||
|
|
||||||
def mutate(self, args, context, info):
|
@staticmethod
|
||||||
|
def mutate(root, args, context, info):
|
||||||
p_data = args.get('person_data')
|
p_data = args.get('person_data')
|
||||||
|
|
||||||
name = p_data.get('name')
|
name = p_data.get('name')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user