question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can Gatsby infer the structure and create GraphQL types automatically from data returned by a resolver

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
blainekastencommented, May 18, 2020

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. 💜

0reactions
vladarcommented, Jun 3, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found