2016-09-12 07:47:34 +03:00
|
|
|
|
Mutations
|
|
|
|
|
=========
|
|
|
|
|
|
|
|
|
|
Most APIs don’t just allow you to read data, they also allow you to
|
2016-09-15 10:38:53 +03:00
|
|
|
|
write.
|
|
|
|
|
|
|
|
|
|
In GraphQL, this is done using mutations. Just like queries,
|
2016-09-12 07:47:34 +03:00
|
|
|
|
Relay puts some additional requirements on mutations, but Graphene
|
|
|
|
|
nicely manages that for you. All you need to do is make your mutation a
|
|
|
|
|
subclass of ``relay.ClientIDMutation``.
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
class IntroduceShip(relay.ClientIDMutation):
|
|
|
|
|
|
|
|
|
|
class Input:
|
|
|
|
|
ship_name = graphene.String(required=True)
|
|
|
|
|
faction_id = graphene.String(required=True)
|
|
|
|
|
|
|
|
|
|
ship = graphene.Field(Ship)
|
|
|
|
|
faction = graphene.Field(Faction)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2017-07-28 06:06:48 +03:00
|
|
|
|
def mutate_and_get_payload(cls, root, info, **input):
|
2017-07-27 08:33:59 +03:00
|
|
|
|
ship_name = input.ship_name
|
|
|
|
|
faction_id = input.faction_id
|
2016-09-12 07:47:34 +03:00
|
|
|
|
ship = create_ship(ship_name, faction_id)
|
|
|
|
|
faction = get_faction(faction_id)
|
|
|
|
|
return IntroduceShip(ship=ship, faction=faction)
|
2017-02-06 22:40:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Accepting Files
|
2017-02-07 06:48:09 +03:00
|
|
|
|
---------------
|
2017-02-06 22:40:04 +03:00
|
|
|
|
|
2017-02-07 06:48:09 +03:00
|
|
|
|
Mutations can also accept files, that's how it will work with different integrations:
|
2017-02-06 22:40:04 +03:00
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
class UploadFile(graphene.ClientIDMutation):
|
|
|
|
|
class Input:
|
|
|
|
|
pass
|
|
|
|
|
# nothing needed for uploading file
|
|
|
|
|
|
|
|
|
|
# your return fields
|
|
|
|
|
success = graphene.String()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2017-07-28 06:06:48 +03:00
|
|
|
|
def mutate_and_get_payload(cls, root, info, **input):
|
2017-02-07 06:48:09 +03:00
|
|
|
|
# When using it in Django, context will be the request
|
2017-10-30 18:00:18 +03:00
|
|
|
|
files = info.context.FILES
|
2017-02-07 06:48:09 +03:00
|
|
|
|
# Or, if used in Flask, context will be the flask global request
|
|
|
|
|
# files = context.files
|
2017-02-06 22:40:04 +03:00
|
|
|
|
|
|
|
|
|
# do something with files
|
|
|
|
|
|
2017-02-07 04:19:02 +03:00
|
|
|
|
return UploadFile(success=True)
|