-
A built-in way for automatic TypeScript type generation and better GraphQL IntelliSense SummaryGraphQL and TypeScript is a match made in heaven. When writing GraphQL queries in Gatsby, you’d also want exact TypeScript types for the respective return values to have type-safety and IntelliSense in your IDE. However, right now you manually have to create these TypeScript types or use a third-party plugin like gatsby-plugin-typegen. You can follow this milestone for updates on the progress. Try it outYou can follow this guide: https://www.gatsbyjs.com/docs/how-to/local-development/graphql-typegen/ Then you can run Then inside your pages you can access the global namespace This project shows all this: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-graphql-typegen Basic ExampleYou can watch a short GIF here: https://www.gatsbyjs.com/docs/reference/release-notes/v4.14/#experimental-graphql-typegen Note: https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql needs to be installed to have it working exactly as shown in the video. Some of this extension’s functionality is based on the GraphQL language server which is also available to other IDEs than VSCode. Motivation
Detailed DesignThe plugin’s code is on GitHub. It will be (partially) incorporated into Gatsby’s core which brings some additional benefits:
This is (on a high-level) what the plugin currently does and how it’ll roughly work also once it’s in Gatsby itself:
Things we’d want to change when bringing it into Gatsby:
Drawbacks
Alternatives
Adoption StrategyA canary will be available for usage behind a feature flag. In the alpha and beta stage it’ll be in stable version of Gatsby but behind a feature flag (we might automatically opt-in people to see behavior/adoption on non-TS/TS sites). How we teach this
Questions for You
|
Beta Was this translation helpful? Give feedback.
Replies: 23 comments 66 replies
-
I am playing around with GraphQL-codegen for a while and have been committing the generated files. Have to say they bloat my commits and give no real further use besides that others don't have to generate them. But now they can forget to generate them and this can cause confusion. Plus the source of truth for this information is already in other files, so IMHO: No need to have them outside of
Well, good question. On build it would probably slow down build times. But if we skip it there, we might have some people slipping through and using "old types". For sure it should generate types when the cache is flushed from dependency updates and so on. BUT I'd say thats okay, very most will develp with
|
Beta Was this translation helpful? Give feedback.
-
i don't know how are the generated files look like. but are the generated files useful to see the development changes better with just reading a commit? or there are just doubled the code changes which are also visible in other code places? |
Beta Was this translation helpful? Give feedback.
-
You can now use it with Please give it a try 🎉 You can also try this example project: https://github.com/LekoArts/graphql-typegen-testing |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm the author of gatsby-plugin-typegen. I'm so excited to see this integrated into Gatsby. Changes looks super great. Just a little suggestions.
Anyway, this is a nice topic to cover in Gatsby core. I wrote an RFC to explain another approach that the user plugin cannot do. |
Beta Was this translation helpful? Give feedback.
-
I merged #35581 - already available on I'll now focus on adding documentation: #35584 |
Beta Was this translation helpful? Give feedback.
-
Very happy this is getting integrated into Gatsby's core. Please make sure that whatever you setup works smoothly with Contentful. Would be extra awesome if it could take a field being Required or not into account for nullability in TypeScript, that would be a great improvement over any of the current setups. Looking forward to this |
Beta Was this translation helpful? Give feedback.
-
This is gonna be so great! A question: Where would you put the files? I have a yarn workspace monorepo. The structure is somewhat like this.
I start my builds in |
Beta Was this translation helpful? Give feedback.
-
gatsbyImage comes through as |
Beta Was this translation helpful? Give feedback.
-
Is there any easy way to handle the case of of templated page queries where the returned object can't be null? As in I've got a page query:
and the typegen semi-correctly points out that |
Beta Was this translation helpful? Give feedback.
-
This feature is available in 4.15 now 🎉 https://www.gatsbyjs.com/docs/reference/release-notes/v4.15/#graphql-typegen |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to please get the outputted data in gatsby-types.d.ts sorted alphabetically or at least given a stable order? It seems that running |
Beta Was this translation helpful? Give feedback.
-
I'm using this with tailwind and have found that it creates an infinite loop. Patching the ts-codegen file to put the gatsby-types.d.ts into the top directory appears to fix it. I'll get an issue created around this as well as I can reproduce it reasonably easily. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to migrate to the built in solution for Graphql type generation from @graphql-codegen. How to deal with inline queries in such situations? The situation looks like this: import { CreatePagesArgs } from 'gatsby';
import { resolve } from 'path';
export async function CreateGenericPages({ actions, graphql }: CreatePagesArgs) {
const { createPage } = actions;
const allPages = await graphql<AllGenericPagesQuery>(`
query AllGenericPages {
allMdx(filter: { fields: { kind: { eq: "pages" } } }) {
edges {
node {
id
fields {
path
translations {
locale
path
}
}
}
}
}
}
`);
allPages.data?.allMdx.edges.forEach(p => {
if (p.node.fields && p.node.fields.path) {
createPage({
component: resolve('./src/templates/GenericPage.tsx'),
context: { id: p.node.id, translations: p.node.fields.translations },
path: p.node.fields.path,
});
}
});
} How to I get the generated type for the |
Beta Was this translation helpful? Give feedback.
-
Is there any reason why the I know they are generated in the query, but sometimes I will have a shared component where accessing the __typename field is simple. Here's an example bit of code using it
|
Beta Was this translation helpful? Give feedback.
-
I'm curious why the GraphQL typegen doesn't automatically infer whether or not a field is required (non-nullable) since everything in the data layer is already coming from Gatsby source plugins? https://www.gatsbyjs.com/docs/how-to/local-development/graphql-typegen/#non-nullable-types Lack of proper typing by default means you can't destructure data objects and have to do a lot of non-null assertions in all .tsx files with queries. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if I'm misunderstanding something, or if my setup is configured incorrectly. For the following: const result = useStaticQuery<Queries.FooterQuery>(graphql`
query Footer {
dataJson {
footer {
phone {
text
}
}
}
}
`); If I hover those fields in the query I see type DataJsonFooterPhone {
text: String
} However the type of the returned result of the query is the following: type FooterQuery = { readonly dataJson: { readonly footer: { readonly phone: { readonly text: string | null } | null } | null } | null }; Is this the expected result without explicitly typing via the guide: https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization/ ? Is there any way to get the generated (I assume by the |
Beta Was this translation helpful? Give feedback.
-
If you have other good examples for typegen + schema cusomization, please share! This is an example PR for safely refactor blog content with custom schema |
Beta Was this translation helpful? Give feedback.
-
I just upgraded to the latest version of Gatsby and enabled I don't know if the cause of this is that I warn Warning: Event "xstate.after(200)#waitingMachine.aggregatingFileChanges" was sent to stopped service "waitingMachine". This service has already reached its final state, and will not transition.
Event: {"type":"xstate.after(200)#waitingMachine.aggregatingFileChanges"}
success Re-building development bundle - 0.895s
success Writing page-data.json files to public directory - 0.008s - 0/60 7287.88/s
success onPreExtractQueries - 0.001s
success extract queries from components - 0.124s
success write out requires - 0.002s |
Beta Was this translation helpful? Give feedback.
-
Hi guys in my project I use static graphql sources plus client-side graphql source(live data with apollo). Before this new feature I have used gatsby graphql codegen plugin for the static and live queries. I use also hook generators for my live queries. My question is would it be possible "in the future" to use gatsby's internal codegen to generate my live hooks? I would really thing that this is a good idea but that's just me! |
Beta Was this translation helpful? Give feedback.
-
Hi 👋 If I import If I leave it out, Gatsby builds correctly but I don't understand how I am the only one reporting this. Here's my tsconfig file (taken from gatsby-minimal-starter-ts): tsconfig.json{
"compilerOptions": {
/* Language and Environment */
"target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["dom", "esnext"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"jsx": "react", /* Specify what JSX code is generated. */
/* Modules */
"module": "esnext", /* Specify what module code is generated. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["./src/**/*", "./gatsby-node.ts", "./gatsby-config.ts", "./plugins/**/*"]
}
Thanks 🙏 |
Beta Was this translation helpful? Give feedback.
-
Looking for Gatsby + TypeScript HELP! I'm having a query like this and using Typegen. From this, how can I pick the type of videoContentReference alone? I have to pass the data from videoContentReference into another component. In general, from a query, can we get the type of a particular nested object alone? Right now I'm doing something like this on the child component Interface: Not sure if is it the right way. Looking for some guidance. Thanks for your time! |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, maybe it's a stupid question, but the file is generated during the build? I tried yarn build and the file with the types is not generated, so I had to commit it to avoid problems with the tests during my CI |
Beta Was this translation helpful? Give feedback.
-
@LekoArts can you please help me with this : https://stackoverflow.com/questions/74065597/warn-warning-event-xstate-after200waitingmachine-aggregatingfilechanges-wa? Simply put: enabling graphql typegen get me into infinite build loop. |
Beta Was this translation helpful? Give feedback.
This feature is available in 4.15 now 🎉
https://www.gatsbyjs.com/docs/reference/release-notes/v4.15/#graphql-typegen