Graphql-js: Enums as Query/Mutation Params in Schema Language

Created on 1 Jan 2017  路  3Comments  路  Source: graphql/graphql-js

I'm attempting to create a mutation that updates the value of an enum and also being able to query by that enum using schema language. Here is the enum type in question :

const Role = `
  # Roles that grant users specific permissions.
  enum Role {
    # Users with access to admin-specific features.
    ADMIN
    # Users with access to contributor-specific features.
    CONTRIBUTOR
  }
`;

As well as the mutation:

const userMutations = `
    # Finds a user by email and changes their role to either ADMIN or CONTRIBUTOR.
    updateUserRole(
      email: String!
      role: Role!
    ): User
`;

This is what I get back when from either a query or mutation with a param with the Role type:

{
  "errors": [
    {
      "message": "Argument \"role\" has invalid value \"ADMIN\".\nExpected \"Role\", found not an object.",
      "locations": [
        {
          "line": 2,
          "column": 25
        }
      ]
    }
  ]
}

It works perfectly fine for returning values from queries. If I manually inject a user with an invalid role it will error when I query it. I'm sure this will end up being something obvious. Not sure if it is a limitation of the schema language, as I've found working examples building the enum programmatically. Any help is much appreciated.

Most helpful comment

How are you specifying the value for the role argument? Note that if you write the value inline in the mutation query (instead of using a variable) then it must be written without quotes, e.g. mutation Foo { updateUserRole(role: ADMIN, ...) ..., as opposed to role: "ADMIN".

All 3 comments

How are you specifying the value for the role argument? Note that if you write the value inline in the mutation query (instead of using a variable) then it must be written without quotes, e.g. mutation Foo { updateUserRole(role: ADMIN, ...) ..., as opposed to role: "ADMIN".

That was it! Thanks @josephsavona. Is that anywhere in the documentation?

I think the enum examples on graphql.org don't use quotes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafgraph picture rafgraph  路  4Comments

scf4 picture scf4  路  3Comments

sudheerj picture sudheerj  路  3Comments

galki picture galki  路  3Comments

matthewgertner picture matthewgertner  路  4Comments