graphql-ruby: Passing empty array in a variable returns an error

The following mutation works as expected:

mutation {
   AdjustRoles(input: {
     role_names: []
   }) {
     clientMutationId
     viewer {
       id  active_role_names
     }
   }
}

However the same mutation using variables:

mutation AdjustRolesMutation($role_names: [String]!) {
  AdjustRoles(input: {
    role_names: $role_names
  }) {
    clientMutationId
    viewer {
      id  active_role_names
    }
  }
}

Variables
{ "role_names": [] }

Causes the following error:

{
  "errors": [
    {
      "message": "Variable role_names of type [String]! was provided invalid value",
      "locations": [
        {
          "line": 1,
          "column": 30
        }
      ],
      "value": null,
      "problems": [
        {
          "path": [],
          "explanation": "Expected value to not be null"
        }
      ]
    }
  ]
}

Looks like the empty array is being treated as null.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 17 (8 by maintainers)

Most upvoted comments

So, the keys are hashes, but you need string keys:

# Prepare a new hash to hold string-keyed variables 
graphql_variables = {} 
# copy each pair into the  new hash, but convert each key to a string with .to_s
params[:variables].each do |k, v|
 graphql_variables[k.to_s] = v 
end 
# Make sure they're strings: 
p graphql_variables 

# then use them: 
Schema.execute(query_str, variables: graphql_variables, ...)