9

I'm using Scaphold.io to build out the back end of a little app I'm working on, and have run into a problem trying to update a number of items in a single mutation request.

In Scaphold, when you create a new Type, mutations are created for common operations: create, update, and delete.

You can't redefine the schema for these mutations, or their input types. In the type of the model I'm working (card), I get these mutations out of the box:

createCard(input: CreateCardInput!): CreateCardPayload

updateCard(input: UpdateCardInput!): UpdateCardPayload

deleteCard(input: DeleteCardInput!): DeleteCardPayload

The updateCardInput type defines the following fields:

listId: ID
description: Text
name: String
order: Int
id: ID!
clientMutationId: String

What I can do: update a single card

With this, I know how to define a mutation that updates order for a single card:

mutation updateCardOrder($card: UpdateCardInput!) {
  updateCard(input: $card) {
    changedCard {
      id
      order
    }
  }
}

Variables:

{
  "card": {
    "id": "Q2FyZDo4MzA=",
    "order": "1"
  }
}

What I want to do: update multiple cards in a single mutation

I got some pointers to use a list of cards ([card]) as an input, but I can't figure out how to get that to work.

Ideally, something like:

mutation updateCardOrder($cards: UpdateCardInput!) {
  updateCard(input: $card) {
    changedCard {
      id
      order
    }
  }
}

And pass in multiple cards as a variable, for example:

{
  "cards": [
    {
      "id": "Q2FyZDo4Mjk=",
      "order": "1"
    },
    {
      "id": "Q2FyZDo4MzA=",
      "order": "2"
    }
  ]
}

When I try this, I get an error:

{
  "errors": [
    {
      "message": "Variable \"$cards\" of type \"[UpdateCardInput!]\" used in position expecting type \"UpdateCardInput!\".",
      "locations": [
        {
          "line": 1,
          "column": 32
        },
        {
          "line": 2,
          "column": 27
        }
      ],
      "name": "GraphQLError"
    }
  ]
}

So… what is the right way to think about doing this in GraphQL, given that I can't change the Input Types or mutations schema in Scaphold? Is it even possible?

Jonathan Heron
  • 191
  • 1
  • 5

1 Answers1

-3

Since you want to run multiple mutations in a single request, you can use GraphQL aliases to accomplish that. More information here: https://github.com/mugli/learning-graphql/blob/master/3.%20Querying%20with%20Field%20Aliases%20and%20Fragments.md

Essentially, what you can do is run a mutation like this:

mutation updateCardOrder($card1: UpdateCardInput!, $card2: UpdateCardInput!, $card3: UpdateCardInput!) {
  update1: updateCard(input: $card1) {
    changedCard {
      id
      order
    }
  }
  update2: updateCard(input: $card2) {
    changedCard {
      id
      order
    }
  }
  update3: updateCard(input: $card3) {
    changedCard {
      id
      order
    }
  }
}

And the variables would look something like this:

{
  "card1": {
    "id": "Q2FyZDo4Mjk=",
    "order": "1"
  },
  "card2": {
    "id": "Q2FyZDo4MzA=",
    "order": "2"
  },
  "card3": {
    "id": "Q2FyZDo4MzA=",
    "order": "3"
  }
}

Hope that helps!

vince
  • 1,904
  • 1
  • 12
  • 17
  • 4
    That sounds counter-productive, if you need 10 cards you would need 10 arguments.. We need something that is dynamic! – kroe Aug 29 '17 at 16:18
  • Upvoting. You can do this with a script like in this question https://stackoverflow.com/questions/41688883/how-to-update-all-records-in-a-collection-using-graphql – Alex Po Aug 02 '18 at 06:25