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.

Allow result type and variables type to be inferred from `DocumentNode`/`Source` using TypeScript type system

See original GitHub issue

Hi!

We recently implemented an improved version of DocumentNode, called TypedDocumentNode, that allow type generics for result type and variables type:

export interface TypedDocumentNode<Result = { [key: string]: any }, Variables = { [key: string]: any }> extends DocumentNode {}

This improved version allows TypeScript to infer the types automatically when an object that matches the signature is used, for example:

const typedDocument: TypedDocumentNode<{ test: string }, { v: string }> = parse(`query($v: String) { test(v: $v) }`);

// Now `result` will be automatically of type `{ test: string }`
const result = await execute({
  // ...
  document: typedDocument,
  // variableValues will be typed to `{ v: string }` automatically
  variableValues: { v: 'test' },
});

This will allow to have the types set within the DocumentNode and not externally as used today, and avoid incorrect TypeScript types used. This made possible because TypeScript does type inference when possible.

The existing solution is implemented in this repo: https://github.com/dotansimha/graphql-typed-document-node and it’s currently implemented by patching graphql library and add support to it. I think this concept could be merged into this PR, just like we allow extensions or other to be typed with generics.

Developers who chose to automate the typings creation, can use GraphQL Code Generator to generate a pre-compiled DocumentNode<T, R> from their operations, the output file looks like that:

https://github.com/dotansimha/graphql-typed-document-node/blob/master/examples/graphql/typed-document-nodes.ts#L45

Adding this to the core implementation of graphql won’t break anything, since it effects only TypeScript types, and the generics are mostly there today, so it’s just a few non-breaking adjustments to support this.

To have it fully supported across graphql and execute method, it needs to be added to Source and DocumentNode. I created a PR here: https://github.com/graphql/graphql-js/pull/2728

Related:

@IvanGoncharov

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
benjamncommented, Aug 3, 2020

Apollo Client would gladly use this type, and we think it would be ideal for it to be provided by the same package that defines DocumentNode, though that’s not an immediate blocker for us. See also https://github.com/apollographql/apollo-client/pull/6720#issuecomment-668203672.

1reaction
yaacovCRcommented, Oct 10, 2021

@dotansimha can this be closed? Is there additional pending integration with TypedDocumentNode?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Type Inference - TypeScript
This kind of inference takes place when initializing variables and members, setting parameter default values, and determining function return types. In most ...
Read more >
Documentation - Advanced Types - TypeScript
Such inferred type variables may be referenced in the true branch of the conditional type. It is possible to have multiple infer locations...
Read more >
Documentation - Conditional Types - TypeScript
Create types which act like if statements in the type system.
Read more >
Documentation - Type Compatibility - TypeScript
The basic rule for TypeScript's structural type system is that x is compatible with y if y has at least the same members...
Read more >
Documentation - Everyday Types - TypeScript
Functions are the primary means of passing data around in JavaScript. TypeScript allows you to specify the types of both the input and...
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