1

I want to use an array/list as variable for a GraphQL mutation like this:

Schema

type User {
  id: Id!
  email: String
  name: String
  bookmarks: [Bookmark]
}

type Mutation {
  updateUser(data: UserInput!): User
}

input UserInput {
  email: String
  name: String
  bookmarks: [Bookmark]
}

type Bookmark {
  vidId: String!
  timestamp: Int!
}

Mutation

mutation($email: String, $name: String, $bookmarks: [{vidId: String!, timestamp: Int!}]) {
  updateUser(data: {email: $email, name: $name, bookmarks: $bookmarks}) {
    email
    name
  }
}

However, the variable syntax in the mutation regarding the bookmarks array seems invalid. How can I pass in an array as a GraphQL variable?

4
  • Should that be input Bookmark instead of type Bookmark? Commented Mar 24, 2020 at 19:28
  • I'm not sure, Bookmark is also used elsewhere outside the input type. Does everything in the input type have to be recursively input types? Edit: I added another type to show this Commented Mar 24, 2020 at 19:34
  • Yes, I'm pretty certain that they do. Does your schema validate? Commented Mar 24, 2020 at 19:39
  • Thanks, I got it now Commented Mar 24, 2020 at 19:41

1 Answer 1

2

GraphQL does not support anonymous type declarations (that {vidId: String!, timestamp: Int!} literal), you must refer to a named type (or a list of it or a non-null of it). So just write

mutation($email: String, $name: String, $bookmarks: [BookmarkInput]) {
#                                                    ^^^^^^^^^^^^^
  updateUser(data: {email: $email, name: $name, bookmarks: $bookmarks}) {
    email
    name
  }
}

instead with a

input BookmarkInput {
  vidId: String!
  timestamp: Int!
}

defined in your schema.

Sign up to request clarification or add additional context in comments.

2 Comments

I see. Does this have to be an input type? I thought only the outmost layer has to be input type, because String, Int are not input types
@rasperry String and Int are scalar types (not object types). Those can be contained in both input and output types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.