Search code examples
graphqlapollo-clientgraphcool

Create a mutation which takes multiple values of an ENUM


I've created a model that has a field which takes multiple values of an ENUM. How can I write a mutation which will allow me to add multiple values at once?

For example, I have a sign up mutation:

export const signUp = gql`
  mutation signUp(
    $email: String!,
    $name: String!,
    $password: String!,
    $attended: [USER_ATTENDED!]!,
    $role: [USER_ROLE!]!,
  ) {
    createUser(
      authProvider: {
        email: {
          email: $email,
          password: $password,
        },
      },
      name: $name,
      attended: $attended,
      role: $role,
    ) {
      id,
      createdAt,
      name,
      email,
    }
  }
`;

I would expect $attended and $role to accept multiple values.


Solution

  • You can pass multiple values by using brackets: []. In your case, the variables in GraphiQL need to like like this:

    {
      "email": "name@email.com",
      "name": "Name",
      "password": "secret",
      "attended": ["A", "B"],
      "role": ["ROLE_A", "ROLE_B"]
    }
    

    In Apollo, this should work:

    const variables = {
      email: "name@email.com",
      name": "Name",
      password: "secret",
      attended: ["A", "B"],
      role: ["ROLE_A", "ROLE_B"]
    }
    

    More information about GraphQL variables is available here and here.