rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.38k stars 1.39k forks source link

Error raised (instead of error response) on argument value starting with `-` #5088

Closed fhoeben closed 2 months ago

fhoeben commented 2 months ago

Describe the bug

Queries containing incorrect argument values that start with a - do not result in a proper error message, but cause an exception to be raised.

Versions

graphql version: 2.3.14 rails (or other framework): N/A other applicable versions (graphql-batch, etc) N/A

GraphQL schema

class MyEnum < GraphQL::Schema::Enum
  value "foo"
  value "type"
end

class QueryType < GraphQL::Schema::Object
  field :hello, String do
    argument :arg, MyEnum
  end

  def hello(arg:)
    "Hello #{arg}"
  end
end

class ApplicationSchema < GraphQL::Schema
  query QueryType
end

GraphQL query

The following query works:

{ hello(arg: foo) }
{
  "data": {
    "hello": "Hello foo"
  }
}

And the following incorrect query gives a correct error response:

{ hello(arg: -1) }
{
  "errors": [
    {
      "message": "Argument 'arg' on Field 'hello' has an invalid value (-1). Expected type 'MyEnum!'.",
      "locations": [
        {
          "line": 1,
          "column": 3
        }
      ],
      "path": [
        "query",
        "hello",
        "arg"
      ],
      "extensions": {
        "code": "argumentLiteralsIncompatible",
        "typeName": "Field",
        "argumentName": "arg"
      }
    }
  ]
}

Steps to reproduce

But when I send:

{ hello(arg: -foo) }

An exception is raised

GraphQL::Error: (token_value failed: TypeError: nil can't be coerced into Integer)
rmosolgo commented 2 months ago

Thanks for the detailed report! I've got a fix in the works at #5090

fhoeben commented 2 months ago

@rmosolgo thanks for the fix we indeed get an error message now instead of an exception.

Could you take a look whether it would be possible to change the error to take into account what is actually expected according to the schema (like we get when passing -1)? I now (2.3.16) get only a message and a location (no path). And the message is a bit more general than normally:

Expected a number, but it was malformed (\"-\")

I expected a path to be present and a message like

Argument 'arg' on Field 'hello' has an invalid value (-foo). Expected type 'MyEnum!'.