Мутации (Mutations)

Большинстов API позволяют Вам не просто считывать данные, но также их записывать.

В GraphQL это сделано при помощи мутаций. Подобно запросам, Relay добавляет некоторые дополнительные требования к мутациям, но Graphene прекрасно с этим справится. Все, что надо, это наследовать мутацию от relay.ClientIDMutation.

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
    def mutate_and_get_payload(cls, root, info, **input):
        ship_name = input.ship_name
        faction_id = input.faction_id
        ship = create_ship(ship_name, faction_id)
        faction = get_faction(faction_id)
        return IntroduceShip(ship=ship, faction=faction)

Прием файлов

Мутации также принимают файлы, вот как это работает:

class UploadFile(graphene.ClientIDMutation):
     class Input:
         pass
         # nothing needed for uploading file

     # your return fields
     success = graphene.String()

    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        # When using it in Django, context will be the request
        files = info.context.FILES
        # Or, if used in Flask, context will be the flask global request
        # files = context.files

        # do something with files

        return UploadFile(success=True)

results matching ""

    No results matching ""