Hello,
While using variables for posting to /graphql from RSpec, it is automatically converting the variable inputs to strings.
For e.g. I have this mutation
module Mutations
class UpdateCreditEntryMutation < Mutations::BaseMutation
class UpdateCreditEntryInput < Types::BaseInputObject
argument :contact_id, ID, required: false
argument :amount, Float, required: true
end
argument :id, ID, required: true
argument :input, UpdateCreditEntryInput, required: false
type Types::CreditEntryType
null false
def resolve(id:, input:)
...
end
end
end
And, in my Rspec tests, I am defining the query this way.
let(:gql) do
<<~GRAPHQL
mutation UpdateCreditEntryMutation($id: ID!, $input: UpdateCreditEntryInput!) {
updateCreditEntry(id: $id, input: $input) {
id
}
}
GRAPHQL
end
And then posting to GraphQL via this:
variables = { id: ce.id, input: { amount: 200.00, updatedAt: '2020-01-01' } }
post '/graphql', headers: { 'X-Token' => session.token }, params: {
query: gql,
variables: variables
}
But I get an error because the amount is supposed to be a float, but it gets automatically converted into a string.
This is the error:
{"errors"=>[{"message"=>"Variable input of type UpdateCreditEntryInput! was provided invalid value for amount (Could not coerce value \"200.0\" to Float)", "locations"=>[{"line"=>1, "column"=>46}], "extensions"=>{"value"=>{"amount"=>"200.0", "updatedAt"=>"2020-01-01"}, "problems"=>[{"path"=>["amount"], "explanation"=>"Could not coerce value \"200.0\" to Float"}]}}]}
If I change the amount to type string in my mutation input - everything will pass.
Here is is converting the amount into a string:
"extensions"=>{"value"=>{"amount"=>"200.0", "updatedAt"=>"2020-01-01"}
Wondering why it is converting the float into a string. Or maybe I am calling the inputs the wrong way? If so, will delete this issue.
Thanks.
Hi, it looks like you're passing graphql variables as query parameters (eg ?variables[amount]=200.00) is that right?
If so, then you have to recognize that they won't be parsed by Rails into anything but strings. Query parameters don't have any typing information, so Rails can't tell that you mean 200.0, not "200.00".
You could confirm this by debugging in your controller:
p ["Variables param:", params[:variables]]
# What do you see in the log for this?
To work around this, use a JSON request body instead of query parameters. For example:
post "/graphql", format: :json, params: { ... }
(I think format: :json will do the trick -- you might have to use .to_json on the params, too)
Let me know if that helps!
Thank you! It worked after adding what you suggested.
post "/graphql", headers: { 'Content-Type' => 'application/json', 'X-Token' => session.token }, params: params.to_json
Most helpful comment
Hi, it looks like you're passing graphql variables as query parameters (eg
?variables[amount]=200.00) is that right?If so, then you have to recognize that they won't be parsed by Rails into anything but strings. Query parameters don't have any typing information, so Rails can't tell that you mean
200.0, not"200.00".You could confirm this by debugging in your controller:
To work around this, use a JSON request body instead of query parameters. For example:
(I think
format: :jsonwill do the trick -- you might have to use.to_jsonon the params, too)Let me know if that helps!