Skip to content

RangeError for combination of recursive input and default value #3871

@mat-sop

Description

@mat-sop

Hi, I was working with input fields' default values in graphql-core and I encountered a problem with a combination of recursive inputs and default values which also occurs in graphql-js.
Let's take this SDL as an example:

"""Example 1"""
type Query { field(recursiveInput: RecursiveInput): String }

input RecursiveInput {
  self: RecursiveInput = {value: "foo"}
  value: String 
}

If I understand correctly that example is invalid, because {value: "foo"} is lacking the self field, then the default value for self is looked up, which leads to a recursion error - RangeError: Maximum call stack size exceeded.

When the default value is removed, everything works fine:

"""Example 2"""
type Query { field(recursiveInput: RecursiveInput): String }

input RecursiveInput {
  self: RecursiveInput
  value: String 
}

However, the following also looks fine, because self doesn't need to be looked up, but it still causes a recursion error:

"""Example 3"""
type Query { field(recursiveInput: RecursiveInput): String }

input RecursiveInput {
  self: RecursiveInput = {value: "foo", self: null}
  value: String 
}

On the other hand, the following actually invalid example does not give an error:

"""Example 4"""
type Query { field(recursiveInput: RecursiveInput): String }

input RecursiveInput {
  self: RecursiveInput!
  value: String 
}

If I'm not mistaken:

  • Example 1 should produce a proper error instead of creating a stack overflow
  • Example 3 should not produce an error
  • Example 4 should produce an error showing that this is an invalid definition

To test all examples I used graphql@16.6.0 and following code:

const { buildSchema } = require('graphql');
const schemaString = `...`;
const schema = buildSchema(schemaString);

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