Comments (6)
GraphQL types can refer to themselves by implementing fields
as a function rather than an object. In fact, supplying an object is a shorthand for a function that returns that object:
var RecursiveType = new GraphQLObjectType({
name: 'Recursive',
fields: {
recurse: { type: RecursiveType }
}
});
Will not work because you reference RecursiveType
before it's defined. Instead, use a function for fields.
var RecursiveType = new GraphQLObjectType({
name: 'Recursive',
fields: function () {
return {
recurse: { type: RecursiveType }
}
}
});
Or if you're using ES6 arrow functions
var RecursiveType = new GraphQLObjectType({
name: 'Recursive',
fields: () => ({
recurse: { type: RecursiveType }
})
});
The function is called after the type is defined, ensuring the variable has been set before it is referenced.
from graphql-js.
Thanks for the tips here.
What if I'm declaring a GraphQLUnionType
instead of GraphQLObjectType
?
I have a NestedType with a 'parent' attribute that is defined as a UnionParentType.
const UnionParentType = GraphQLUnionType({
name: 'ParentType',
types: [RootType, NestedType],
resolveType: (parent) => {
if (parent instanceof Root) {
return RootType;
}
return NestedType;
}
});
When I run this - It doesn't work.
I get an error Error: ParentType may only contain Object types, it cannot contain: undefined.
Is it possible to work recursively like this with union types?
from graphql-js.
Is it possible to work recursively like this with union types?
The types
property of a GraphQLUnionType
can also be thunk, so the previous error might be fixed with:
const UnionParentType = GraphQLUnionType({
name: 'ParentType',
types: () => [RootType, NestedType], // <--- this changed
resolveType: (parent) => {
if (parent instanceof Root) {
return RootType;
}
return NestedType;
}
});
I was also getting an error like:
Type may only contain Object types, it cannot contain: undefined
It was fixed by changing types
from an array to a function that returns an array.
from graphql-js.
Also GraphQL type could reference to itself.
from graphql-js.
@leebyron Thank you very much for quick reply!
from graphql-js.
@leebyron Very helpfull, thanks!
That happened to me when I include same GraphQLObjectType in different files. But I have also Enum dependent in different files
Do you know how to make same trick with GraphQLEnumType?
from graphql-js.
Related Issues (20)
- Global HOT 1
- Depending on the scale a few more fields could result in a lot more function calls in the resolvers. That coupled with Node.js / JS single threaded nature might be problematic.
- IHeyReally
- IHeyReally.org
- IHeyReally.org
- Suggestion: Bundling in v17, ESM, CJS, and the dual package hazard HOT 9
- Just to give my 2c, I'm not sure if `exports.development` and `exports.production` target conditionsa are widely supported, so some `import.env` shenanigans may still be necessary until that's widely adopted. HOT 1
- Collection of libraries and how they import from `graphql` HOT 20
- `process.env`, `globalThis`, and `typeof process` HOT 21
- Introspection queries don't support `@oneOf` HOT 2
- Tutorial data HOT 2
- astFromValue fails with a custom scalar serializing to an object value HOT 5
- In a response to a query about an Issue, the URL and other info is missing for links created with Reference editor button HOT 5
- author/committer -> user fields returning NULL for commits committed by user
- [email protected]
- npm link with graphql package breaks application HOT 2
- IHeyReally.com HOT 1
- Can I ask what is the progress here? Is there a solution being worked on? Do we have some timeline? Or progress with issue? Thanks!
- IHeyReally.com
- Typescript error with 16.9.0 (re ThunkObjMap) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-js.