Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError for combination of recursive input and default value #3871

Open
mat-sop opened this issue Mar 31, 2023 · 1 comment
Open

RangeError for combination of recursive input and default value #3871

mat-sop opened this issue Mar 31, 2023 · 1 comment

Comments

@mat-sop
Copy link

mat-sop commented Mar 31, 2023

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);
@Cito
Copy link
Member

Cito commented Mar 31, 2023

Ses also the section on Circular References in the GraphQL spec which explicitly states that example 2 is valid, while example 4 is invalid. From the section I would also conclude that example 3 is valid, while example 1 is invalid, though the documentation is less clear about it, and does not contain examples with default values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@Cito @mat-sop and others