graphql-js: Schema circular dependency issue
Keeping Entire Schema in One file is very ugly and hard to maintain. If we keep it in seperate files there is a Circular Dependency Issue with Interface.
Is there a solution to this?
CategoryEdge.node field type must be Output Type but got: undefined.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (6 by maintainers)
Commits related to this issue
- RFC: Allow interfaces to implement other interfaces This is an initial implementation of the RFC described by spec PR #373 https://github.com/graphql/graphql-spec/pull/373 — committed to mike-marcacci/graphql-js by mike-marcacci 5 years ago
- RFC: Allow interfaces to implement other interfaces This is an initial implementation of the RFC described by spec PR #373 https://github.com/graphql/graphql-spec/pull/373 — committed to mike-marcacci/graphql-js by mike-marcacci 5 years ago
- RFC: Allow interfaces to implement other interfaces This is an initial implementation of the RFC described by spec PR #373 https://github.com/graphql/graphql-spec/pull/373 — committed to mike-marcacci/graphql-js by mike-marcacci 5 years ago
- RFC: Allow interfaces to implement other interfaces This is an initial implementation of the RFC described by spec PR #373 https://github.com/graphql/graphql-spec/pull/373 Add test for empty interfa... — committed to mike-marcacci/graphql-js by mike-marcacci 5 years ago
- RFC: Allow interfaces to implement other interfaces This is an implementation of the RFC described by spec PR #373 https://github.com/graphql/graphql-spec/pull/373 — committed to mike-marcacci/graphql-js by mike-marcacci 5 years ago
- RFC: Allow interfaces to implement other interfaces (#2084) This is an implementation of the RFC described by spec PR #373 https://github.com/graphql/graphql-spec/pull/373 — committed to graphql/graphql-js by mike-marcacci 5 years ago
This is not an issue with GraphQL-JS, but it’s true that the solution to this is not well documented.
You can use a thunk for your fields like so:
I have a circular dependency in my Connection definitions
ProductTypeis undefined in this casehow can I use a “thunk” to solve this?
Was there a updated solution to this? I tried doing all fields as thunk and @holmesal hacky fix without any luck. I created a Stack Overflow question on it
https://stackoverflow.com/questions/44527183/how-to-fix-circular-dependency-with-graphql-relay-connections
Edit: Solution was found! More info in the post but this is the fix for importing a circular type:
I highly recommend using separate files for your schema definitions. A good practice is one type per file with well named files.
As Jonas pointed out, whenever there is a circular dependency between types, you need to use a “thunk” (a function that returns a value, called later) for your fields
@joewoodhouse Sorry got confused, not it can’t 😦 I checked the source code and only
fields,interfacesandtypes(for Union) could be a thunk.Also, don’t use CommonJS. From this reference: https://gist.github.com/fbaiodias/77406c29ddf37fe46c3c
Going back to the original issue…
So I have split up my schema into a number of seperate files, but now seem to have hit a circular dependency issue I can’t resolve.
Basically I want to keep each query and mutation in a separate file.
So at the top-level I might have a schema like:
With a projectType definition in a separate file
Now suppose that my definition of
updateProjectis as followsWhen this setup is run, it will fall over with:
So what happens is:
updateProjectmutationupdateProjectimportsprojectTypeprojectType.fieldsthunk has not been run, hence the error above.It feels like I need a thunk on the
typefield of my mutation? Does anyone have any suggestions?If a mutation or query has a type of something that uses a thunk for it’s fields, then any field that uses it must also be inside a thunk? Seems impossible then to define it in a standalone file?
@sibelius AFAIK you can’t use a think on connectionDefinitions yet. I posted my current workaround in https://github.com/graphql/graphql-js/issues/612
@sibelius I don’t know the answer, but since your question is about graphql-relay-js you might have more luck getting it answered there.