Simon Cooper
Simon Cooper

Reputation: 1594

GraphQL Argument has an invalid value. Expected type 'Int

I'm playing around with a very basic GraphQL API in Rails.

I've got a model 'Gin' where I can return the handful of records I have in the db, however when trying to add a gin via a mutation, I'm getting an issue with the field type:

mutation {
  createGin(name: "Plymouth", 
description: "Gin made in Plymouth", 
abv: "40", 
url: "http://www.plymouthgin.com",) {
    id
    name
    description
    abv
    url
  }
}

gives the following error:

"message": "Argument 'abv' on Field 'createGin' has an invalid value. Expected type 'Int'.",

the abv field in the db is integer.

gin_type.rb

Types::GinType = GraphQL::ObjectType.define do
  name 'Gin'

  field :id, !types.ID
  field :name, !types.String
  field :description, !types.String
  field :abv, !types.Int
  field :url, !types.String
end

create_gin.rb

class Resolvers::CreateGin < GraphQL::Function

  argument :name, !types.String
  argument :description, !types.String
  argument :abv, !types.Int
  argument :url, !types.String

  type Types::GinType

  def call(_obj, args, _ctx)
    Gin.create!(
      name: args[:name],
      description: args[:description],
      abv: args[:abv],
      url: args[:ur],
    )
  end
end

Am I missing something elsewhere?

UPDATE If I change the mutation to remove the " from around the 40, i.e. so as an integer and not a string, I get the following error message:

{
  "data": {
    "createGin": null
  },
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Gin.url"
    }
  ]
}

Upvotes: 1

Views: 10791

Answers (1)

Anas Tiour
Anas Tiour

Reputation: 1432

Try this mutation:

mutation {
  createGin(name: "Plymouth", 
description: "Gin made in Plymouth", 
abv: 40, 
url: "http://www.plymouthgin.com",) {
    id
    name
    description
    abv
    url
 }
}

The error message is right. You were providing the abv argument as a string.

EDIT:

In your create_gin.rb, you have a typo on the line

url: args[:ur],

Everytime you stumble upon an error message, first thing to assume is that it is right.

Upvotes: 0

Related Questions