How to get typed data from Craft using GraphQL, TypeScript (and codegen) #15399
-
I'm in the process of upgrading a headless Craft 4 project to Craft 5. As expected there have been quite some changes, and I had to update all my GraphQL queries. It took some time to figure out the name of all the new matrix blocks entries, but that all worked out. In my Craft 4 project I have been using codegen to automatically create TypeScript types from my queries. That way I get nice autocomplete integration with VSCode and (when it works) it really improves the developer experience. What I noticed though was that the number of Craft entry types went from 23 (in Craft 4) to 67 in Craft 5. This might not seem that big of a deal, but the generated code (that VSCode uses for autocompletion) also almost in size (it's now totals around 22mb where it was 8mb). This makes autocomplete really slow (even on a MBP M1 Max) and unusable. I think the graphql schema that is generated by Craft CMS could be a bit smarter. For instance, when I ran a query like this:
I would like
So what I'm basically asking, is there a better way to get typed data from Craft CMS? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
A work around to get the right types returned (or at least only the types within a section) when querying for a single entry is not to use the |
Beta Was this translation helpful? Give feedback.
-
The entry types included in the schema will depend on which sections are included. If you can do with out some sections, exclude them from your schema settings and the entry type count should go down. There are more entry types in Craft 5 because any of your Matrix block types in Craft 4 are entry types now. It should be a mostly-lateral move though.
We don’t know this at the point when the schema is being built out, which needs to be ready to handle any possible query that could execute against it. |
Beta Was this translation helpful? Give feedback.
-
@thomasjonas have you since found a way to get this to work better for you? I'm really struggling to work with the vast codegen'd schemas, and makes working with Craft in my pretty much exclusively typescript workflow more and more of a hassle... |
Beta Was this translation helpful? Give feedback.
The entry types included in the schema will depend on which sections are included. If you can do with out some sections, exclude them from your schema settings and the entry type count should go down.
There are more entry types in Craft 5 because any of your Matrix block types in Craft 4 are entry types now. It should be a mostly-lateral move though.
We don’t know this at the point when the schema is being built out, which needs to be ready to handle any possible query that could execute against it.