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.
Oops 馃槵
@rmosolgo So this is a confirmed bug then?
Yes, I think so, it seems like [] should be a valid input for [String]!!
I have received the same mistake
小ause of error:
GraphQL::Field.define do
argument :firstName, !types.String
argument :lastName, !types.String
argument :email, !types.String
end
query:
mutation SignUp($firstName: String!, $lastName: String!, $email: String!) {
signUp(firstName: $firstName, lastName: $lastName, email: $email) {
firstName
}
}
and variables
{
"firstName": "name",
"lastName": "lastName",
"email": "[email protected]"
}
Problem in the file of a lib/graphql/query/variables.rb, line: 29
# variable_name == "firstName" <- String
# @provided_variables == {:firstName=>"name", :lastName=>"lastName", :email=>"[email protected]"}
provided_value = @provided_variables[variable_name] # result: nil
@AlexanderMint Use string keys for variables, eg
{
"firstName" => "name",
"lastName" => "lastName",
"email" => "[email protected]"
}
(I switched : for =>)
@rmosolgo But it isn't possible for inquiry json
Variables are invalid JSON: Unexpected token = in JSON at position 16.
If I am not mistaken, parameters will always be the String type.
{
"firstName": "name",
...
}
And variables names - Symbol
signUp(firstName: $firstName, ...) {
I don't quite understand what you mean, but "something": creates a symbol key, for example:
$ irb
irb(main):001:0> hash = { "some_key": 1 }
# => {:some_key=>1}
irb(main):002:0> hash.keys.first
# => :some_key
irb(main):003:0> hash.keys.first.class
# => Symbol
@rmosolgo I receive a mistake:
{
"errors": [
{
"message": "Variable firstName of type String! was provided invalid value",
"locations": [
{
"line": 1,
"column": 17
}
],
"value": null,
"problems": [
{
"path": [],
"explanation": "Expected value to not be null"
}
]
},
{
"message": "Variable lastName of type String! was provided invalid value",
"locations": [
{
"line": 1,
"column": 38
}
],
"value": null,
"problems": [
{
"path": [],
"explanation": "Expected value to not be null"
}
]
},
{
"message": "Variable email of type String! was provided invalid value",
"locations": [
{
"line": 1,
"column": 58
}
],
"value": null,
"problems": [
{
"path": [],
"explanation": "Expected value to not be null"
}
]
}
]
}
Query:
mutation SignUp($firstName: String!, $lastName: String!, $email: String!) {
signUp(firstName: $firstName, lastName: $lastName, email: $email) {
firstName
}
}
{
"firstName": "name",
"lastName": "lastName",
"email": "[email protected]"
}
Action:
Schema.execute(params[:query],
variables: params[:variables] || {},
context: { user_id: session[:user_id] }).to_json
schema.rb
Schema = GraphQL::Schema.define do
query Types::QueryType
mutation Types::MutationType
end
Screenshot: https://imgur.com/VuLLA0s
What do I do not so?
Oh, it looks OK! What do you get if you print out params[:variables], for example
p params[:variables]
# or
Rails.logger.info(params[:variables])
Can you tell if the keys are present in params[:variables]
?
@rmosolgo params[:variables]:
{:firstName=>"name", :lastName=>"lastName", :email=>"[email protected]"}
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, ...)
@rmosolgo Thanks, so everything works. Maybe you will make it in gem?
@blevine Are you still experiencing this issue?
I tried to add a test to find the bug but I couldn't find it, this test passes: https://github.com/rmosolgo/graphql-ruby/compare/fix-empty-list-input
Could you share the Ruby code where you prepare variables and pass them to execute ?
On further investigation I believe that this is not your bug. Looks like Rails is converting the empty array to a nil. See https://github.com/rails/rails/issues/13766 and https://github.com/rails/rails/pull/13188. I haven't been able to disable this behavior via the switch that was added in 4.1.8:
config.action_dispatch.perform_deep_munge = false
but that's my problem to track down. Closing this issue.
Thanks for following up, hope you can track it down!
Most helpful comment
So, the keys are hashes, but you need string keys: