0

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?

rasperry
  • 45
  • 8

1 Answers1

1

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 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 Mar 24 '20 at 19:38
  • 1
    @rasperry `String` and `Int` are scalar types (not object types). Those can be contained in both input and output types. – Bergi Mar 24 '20 at 19:40