-
Notifications
You must be signed in to change notification settings - Fork 63
Fix compilation and execution for circular dependencies in input types #82
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
Open
boopathi
wants to merge
8
commits into
main
Choose a base branch
from
fix-inf-var
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+980
−94
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
30d30e5
fix infinite recursion in variables compilation
boopathi 2f9022f
fix lists; handle runtime recursion and add tests for recursive varia…
boopathi 93553ea
fix lint errors
boopathi 18c752e
fix recursion in lists; handle only objects in computing visited links
boopathi 89918c7
fix recursive list references; use object for generateInput params
boopathi 5623e3e
remove fn printing
boopathi 94cad3c
make circular reference checks optional
boopathi ffa434d
fix todo comment
boopathi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,357 @@ | ||
| import { DocumentNode, GraphQLSchema, parse, validate } from "graphql"; | ||
| import { makeExecutableSchema } from "graphql-tools"; | ||
| import { compileQuery, isCompiledQuery } from "../execution"; | ||
|
|
||
| describe("recursive input types", () => { | ||
| describe("simple recursive input", () => { | ||
| const schema = makeExecutableSchema({ | ||
| typeDefs: ` | ||
| type Query { | ||
| foo(input: FooInput): String | ||
| } | ||
| input FooInput { | ||
| foo: FooInput | ||
| } | ||
| `, | ||
| resolvers: { | ||
| Query: { | ||
| foo(_, args) { | ||
| // used as the acutal value in test matchers | ||
| return JSON.stringify(args); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("should not fail for recursive input without variables", () => { | ||
| const query = parse(` | ||
| { | ||
| foo(input: { | ||
| foo: { | ||
| foo: { | ||
| foo: { | ||
| foo: {} | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| `); | ||
|
|
||
| const result = executeQuery(schema, query); | ||
|
|
||
| expect(result.errors).toBeUndefined(); | ||
| expect(result.data.foo).toBe( | ||
| JSON.stringify({ | ||
| input: { | ||
| foo: { foo: { foo: { foo: {} } } } | ||
| } | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("should not fail with variables using recursive input types", () => { | ||
| const document = parse(` | ||
| query ($f: FooInput) { | ||
| foo(input: $f) | ||
| } | ||
| `); | ||
| const variables = { | ||
| f: { | ||
| foo: { foo: { foo: {} } } | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(result.errors).toBeUndefined(); | ||
| expect(result.data.foo).toBe( | ||
| JSON.stringify({ | ||
| input: { foo: { foo: { foo: {} } } } | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| // when the recursive variable appers at a nested level | ||
| test("should not fail with variables using recursive input types - 2", () => { | ||
| const document = parse(` | ||
| query ($f: FooInput) { | ||
| foo(input: { | ||
| foo: { foo: { foo: $f } } | ||
| }) | ||
| } | ||
| `); | ||
| const variables = { | ||
| f: { | ||
| foo: { foo: { foo: {} } } | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(result.errors).toBeUndefined(); | ||
| expect(result.data.foo).toBe( | ||
| JSON.stringify({ | ||
| input: { foo: { foo: { foo: { foo: { foo: { foo: {} } } } } } } | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("should work with multiple variables using the same recursive input type", () => { | ||
| const document = parse(` | ||
| query ($f: FooInput, $g: FooInput) { | ||
| a: foo(input: $f) | ||
| b: foo(input: $g) | ||
| } | ||
| `); | ||
| const variables = { | ||
| f: { | ||
| foo: { foo: { foo: {} } } | ||
| }, | ||
| g: { | ||
| foo: {} | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(result.errors).toBeUndefined(); | ||
| expect(result.data.a).toBe( | ||
| JSON.stringify({ | ||
| input: { foo: { foo: { foo: {} } } } | ||
| }) | ||
| ); | ||
| expect(result.data.b).toBe( | ||
| JSON.stringify({ | ||
| input: { foo: {} } | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("should work with multiple variables using the same recursive input type - 2 (reverse order)", () => { | ||
| const document = parse(` | ||
| query ($f: FooInput, $g: FooInput) { | ||
| a: foo(input: $g) | ||
| b: foo(input: $f) | ||
| } | ||
| `); | ||
| const variables = { | ||
| g: { | ||
| foo: {} | ||
| }, | ||
| f: { | ||
| foo: { foo: { foo: {} } } | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(result.errors).toBeUndefined(); | ||
| expect(result.data.b).toBe( | ||
| JSON.stringify({ | ||
| input: { foo: { foo: { foo: {} } } } | ||
| }) | ||
| ); | ||
| expect(result.data.a).toBe( | ||
| JSON.stringify({ | ||
| input: { foo: {} } | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("should capture and throw error when recursive value is passed", () => { | ||
| const document = parse(` | ||
| query ($f: FooInput) { | ||
| foo(input: $f) | ||
| } | ||
| `); | ||
| const variables: any = { | ||
| f: { | ||
| foo: { foo: { foo: {} } } | ||
| } | ||
| }; | ||
| variables.f.foo = variables.f; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(result.errors[0].message).toBe( | ||
| "Circular reference detected in input variable '$f' at foo.foo" | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("mutually recursive input types", () => { | ||
| const schema = makeExecutableSchema({ | ||
| typeDefs: ` | ||
| type Query { | ||
| products(filter: Filter): String | ||
| } | ||
| input Filter { | ||
| and: AndFilter | ||
| or: OrFilter | ||
| like: String | ||
| } | ||
| input AndFilter { | ||
| left: Filter | ||
| right: Filter | ||
| } | ||
| input OrFilter { | ||
| left: Filter | ||
| right: Filter | ||
| } | ||
| `, | ||
| resolvers: { | ||
| Query: { | ||
| products(_, args) { | ||
| // used as the acutal value in test matchers | ||
boopathi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return JSON.stringify(args); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("should not fail for mutually recursive variables", () => { | ||
| const document = parse(` | ||
| query ($filter1: Filter) { | ||
| products(filter: $filter1) | ||
| } | ||
| `); | ||
|
|
||
| const variables = { | ||
| filter1: { | ||
| and: { | ||
| left: { | ||
| like: "windows" | ||
| }, | ||
| right: { | ||
| or: { | ||
| left: { | ||
| like: "xp" | ||
| }, | ||
| right: { | ||
| like: "vista" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(JSON.parse(result.data.products).filter).toEqual( | ||
| variables.filter1 | ||
| ); | ||
| }); | ||
|
|
||
| test("should not fail for mutually recursive variables - multiple variables", () => { | ||
| const document = parse(` | ||
| query ($aFilter: Filter, $bFilter: Filter) { | ||
| a: products(filter: $aFilter) | ||
| b: products(filter: $bFilter) | ||
| } | ||
| `); | ||
|
|
||
| const variables = { | ||
| aFilter: { | ||
| and: { | ||
| left: { | ||
| like: "windows" | ||
| }, | ||
| right: { | ||
| or: { | ||
| left: { | ||
| like: "xp" | ||
| }, | ||
| right: { | ||
| like: "vista" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| bFilter: { | ||
| like: "mac", | ||
| or: { | ||
| left: { | ||
| like: "10" | ||
| }, | ||
| right: { | ||
| like: "11" | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(JSON.parse(result.data.a).filter).toEqual(variables.aFilter); | ||
| expect(JSON.parse(result.data.b).filter).toEqual(variables.bFilter); | ||
| }); | ||
|
|
||
| // when the mutually recursive input type appears at nested level | ||
| // instead of the top-level variable | ||
| test("should not fail for mutually recursive variables - 2", () => { | ||
| const document = parse(` | ||
| query ($macFilter: OrFilter) { | ||
| products(filter: { | ||
| like: "mac" | ||
| and: { | ||
| left: { like: "User" } | ||
| right: { like: "foo" } | ||
| } | ||
| or: $macFilter | ||
| }) | ||
| } | ||
| `); | ||
|
|
||
| const variables = { | ||
| macFilter: { | ||
| left: { like: "Applications/Safari" }, | ||
| right: { like: "Applications/Notes" } | ||
| } | ||
| }; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(JSON.parse(result.data.products).filter.or).toEqual( | ||
| variables.macFilter | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw for mutually recursive runtime values", () => { | ||
| const document = parse(` | ||
| query ($filter1: Filter) { | ||
| products(filter: $filter1) | ||
| } | ||
| `); | ||
|
|
||
| const variables: any = { | ||
| filter1: { | ||
| and: { | ||
| left: {} | ||
| }, | ||
| or: { | ||
| left: {} | ||
| } | ||
| } | ||
| }; | ||
| // create mututal recursion at | ||
| // and.left.or.left.and <-> or.left.and.left.or | ||
| variables.filter1.and.left.or = variables.filter1.or; | ||
| variables.filter1.or.left.and = variables.filter1.and; | ||
|
|
||
| const result = executeQuery(schema, document, variables); | ||
| expect(result.errors[0].message).toBe( | ||
| "Circular reference detected in input variable '$filter1' at and.left.or.left.and" | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function executeQuery( | ||
| schema: GraphQLSchema, | ||
| document: DocumentNode, | ||
| variableValues?: any, | ||
| rootValue?: any, | ||
| contextValue?: any, | ||
| operationName?: string | ||
| ) { | ||
| const prepared: any = compileQuery(schema, document as any, operationName); | ||
| if (!isCompiledQuery(prepared)) { | ||
| return prepared; | ||
| } | ||
| return prepared.query(rootValue, contextValue, variableValues || {}); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.