Skip to content

Ambiguous behavior for type nullability mismatch between variables and arguments #3715

Closed
@DifferentialOrange

Description

@DifferentialOrange

GraphQL spec (October 2021) states that

IsVariableUsageAllowed(variableDefinition, variableUsage)

  • Let variableType be the expected type of variableDefinition.
  • Let locationType be the expected type of the Argument, ObjectField, or ListValue entry where variableUsage is located.

AreTypesCompatible(variableType, locationType)

  • If locationType is a non-null type:
    • If variableType is NOT a non-null type, return false.

I build a simple node application with graphql 15.4.0.

var { graphql, buildSchema } = require('graphql');

var rootValue = {
  test: (args) => {
    return args;
  },
}

function PrintResponse(response) {
  if (response.hasOwnProperty('data')) {
    console.log(JSON.parse(JSON.stringify(response.data)))
  }

  if (response.hasOwnProperty('errors')) {
    console.log(JSON.parse(JSON.stringify(response.errors)))
  }
}

var schema = buildSchema(`
  type result {
    arg1: Float!
  }

  type Query {
    test(arg1: Float!): result
  }
`)

Calling this query results in error.

var query1 = `query MyQuery($var1: Float = null)  { test(arg1: $var1)  { arg1 } }`

graphql({
  schema,
  source: query1,
  rootValue,
  variableValues: { var1: 1.12}
}).then(PrintResponse)
[ { message:
     'Variable "$var1" of type "Float" used in position expecting type "Float!".',
    locations: [ [Object], [Object] ] } ]

Calling this query results in success.

var query2 = `query MyQuery($var1: Float = 0)  { test(arg1: $var1)  { arg1 } }`

graphql({
  schema,
  source: query2,
  rootValue,
  variableValues: { var1: 1.12}
}).then(PrintResponse)
{ test: { arg1: 1.12 } }

The only difference is default value -- types are the same. Based on specification, it is expected to get an error in the second case too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions