Can Gatsby infer the structure and create GraphQL types automatically from data returned by a resolver
See original GitHub issueI’ve written a custom resolver that returns a data which is a nested mix of arrays and objects. It seems that I need to explicitly define a type for each nested object, something like this for just one CustomAuthor
object:
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type ProcessedHits implements Node {
name: String,
}
type CustomAuthor {
author: String,
hits: Int
}
`;
createTypes(typeDefs);
};
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
ProcessedHits: {
custom: {
type: "CustomAuthor",
resolve(source, args, context, info) {
const posts = context.nodeModel.getAllNodes({ type: "Post" });
...
return { author: 'John', hits: 3};
},
},
},
};
createResolvers(resolvers);
};
Is there any way, like helper or something, for Gatsby to infer types from the returned data?
As you can imagine, if an object is deeply nested it’s going to be a lot of type definitions simply for the purpose of making graphql work.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Schema Inference - Gatsby
The first step in inferring GraphQL Fields is to generate an exampleValue . It is the result of merging all fields of all...
Read more >Gatsby Data Relationships With Foreign-Key Fields
Gatsby is able to automatically infer a GraphQL Schema from your data, and in many cases, this is really all you need. There...
Read more >Gatsby Graphql schema customization for beginners
Learn how to replace data in the existing Graphql fields, add new ones and define relationships between objects.
Read more >Data fetching with Gatsby and GraphQL - LogRocket Blog
Use GraphQL to fetch data from a Gatsby configuration and different sources including the file system, external APIs, databases, and CMSs.
Read more >gatsby-plugin-mdx - npm
GraphQL MDX Node structure. In your GraphQL schema, you will discover several additional data related to your MDX content. While your local ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for reporting ya’ll! I’ve assigned to @vladar who leads development on our graphql code. He’ll be able to discuss and find out solutions here. 💜
We’re marking this issue as answered and closing it for now but please feel free to reopen this and comment if you would like to continue this discussion. We hope we managed to help and thank you for using Gatsby! 💜
If you decide to open a PR with
inferType
utility, please just mention me in the PR description!