0no-co / gql.tada

🪄 Magical GraphQL query engine for TypeScript
https://gql-tada.0no.co
MIT License
2.58k stars 43 forks source link

RFC: Implement codegen command to use AOT-compiled, cached types (Turbo Mode) #160

Closed kitten closed 6 months ago

kitten commented 7 months ago

Context

Currently, gql.tada is an alternative to codegen tools, which generate types, like the client-preset.

Instead of assigning pre-generated types to GraphQL documents, they're instead parsed and inferred in TypeScript’s type system itself. However, this increases tsc type checking time at least linearly with the amount of documents a user writes and creates. While this also is a problem with codegen tools (the time of tools run in parallel in a watch mode, or ahead of time, will increase as well), the processing that type inference takes will almost always be much larger.

In #150, we've experimented with printing internal types from TypeScript in the CLI.

Summary

Instead of always inferring the type of GraphQL documents, we could instead allow graphql() to lookup pre-computed types, which are created ahead of time.

This would mean that graphql() has two ways of looking up a type for a given document:

The CLI would be used to populate the cache that graphql() reads from (#76). This is dependent on the CLI having an implementation to discover documents, which is necessary for #77 anyway.

tl;dr: This means that a user can, after they're done editing queries and getting type updates in real-time, run the CLI and commit ahead-of-time-precompiled types for all queries.

Since this is a transparent optimisation, this is like a turbo mode for gql.tada and merges the approaches of codegen and inferred GraphQL types and creates a seamless bridge.

This should also alleviate performance problems when compared to other codegen approaches indefinitely.

Proposed Solution