-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: added support for automatic type inference with typed-document-node
#6720
feature: added support for automatic type inference with typed-document-node
#6720
Conversation
Hi @benjamn ! I heard that 0.0.1 hurts your eyes :D I published it as 1.0.0 and updated the PR ;) Btw, I think we should be able to treat this package as |
If someone want to try it out now with latest apollo-client v3 (or even with v2 and older), please refer to the README of the repo: https://github.com/dotansimha/graphql-typed-document-node , we are publishing patch file and a solution for applying support for |
@dotansimha This is a really fantastic improvement, and definitely the direction we want to take TypeScript code generation for Apollo Client. I could show you some Slack conversations proving that we've discussed something like this before, but you went ahead and implemented it, so you deserve all the credit here. Thanks for bumping the major version of Longer term, I would love to see In the short term, I think we (Apollo Client) would be happy to merge this PR, add a (dev) dependency on Would you be open to allowing your users to configure where the Thanks again for tackling this! |
Yeah exactly, we aim to get this merged into I think merging it and making it available in apollo-client will be great in short-term, and if at some point it will be available as part of Dev-dependency and re-exporting sounds good, and the codegen plugin for generating the actual config:
documentNodeImport: '@apollo/client#TypedDocumentNode' I pushed change to update the version, move to What do you think? @benjamn something is missing? |
When I `npm pack`ed @apollo/client and `npm install`ed it into a test application, I found it simpler if @apollo/client depended directly on @graphql-typed-document-node/core, so that I didn't have to install that package explicitly in the test application. We already get complaints from folks who don't realize they need to install the graphql package (a peer dependency of @apollo/client), so I can easily imagine how much confusion would be caused by needing to explicitly install graphql-typed-document-node/core, too. @dotansimha Enjoy that boost in download counts!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rebased and wrote some more tests to get a feel for how this system works, and I can report it does work quite well!
In particular, I wanted to make sure that using a TypedDocumentNode
for options.query
would actually constrain the type of options.variables
, so passing illegal variables would be a type error. Since both options.query
and options.variables
use the TVariables
type parameter, I was worried that TypeScript might infer a more general/useless type for TVariables
if you made a mistake in options.variables
(see microsoft/TypeScript#14829 for discussion of that kind of problem). Thankfully, options.query
seems to "win" over options.variables
in all the cases I tested, so I don't think we need to use a NoInfer<T>
wrapper for the various variables
fields (but that's an option in the future if necessary).
I also decided it will make our lives a lot easier if @graphql-typed-document-node/core
is a direct dependency of @apollo/client
, so we don't have people complaining that they had to install it explicitly, like we currently do with the (much shorter and easier to type) graphql
package.
Since this is a significant new feature, I'm also going to retarget this PR to the release-3.2
branch, which will eventually/soon be merged back into master
, so that we can keep merging and releasing 3.1.x changes in the meantime.
Ok, these changes have been released in Thanks again to @dotansimha for this amazing contribution! |
That's great, thank you @benjamn ! |
@benjamn I will create a PR to bump this to v3 in a few minutes. I moved everything that isn't related to the pure |
Hi!
This PR adds support for typed-document-node interface as part of the core API and React support.
The idea behind it is to have
TData
andTVariables
burnt into theDocumentNode
, and allow TypeScript to automatically infer the result type and variables type, based on thequery
object passed.TL;DR
DocumentNode
instead of being separated, and specified in each use.TypedDocumentNode
is declared, and then inferred automatically by TS.Overview
It makes integration of GraphQL frontend consumers and TypeScript much easier, since types are located "inside" the
DocumentNode
. This simplifies work for developers, since they don't need to manually specifyTData
orTVariables
in order to have typings for their operations, and the types are being defined within the operation itself.It can be used with manual typings, but the core goal is to use GraphQL Codegen, in order to pre-compile
.graphql
files intoDocumentNode
, and generate and burn in the types, and produceTypedDocumentNode<TData, TVariables>
that can be used.Changes in this PR
This PR is not breaking any API, since it uses
TData
when possible (I added it to some interfaces where, as 2nd generic, with defaultany
, so all APIs remains the same). Logic isn't effected at all, since it's all types. So does bundle-size, since the@graphql-typed-document-node/core
package just contains types, and not code.Code Examples
Without
typed-document-node
:With
typed-document-node
: