[client-preset] support explicit type annotation #8591
-
BackgroundDue to Typescript limitations, tagged template literals don't work with overrides, making inferring the correct TypedDocumentNode type impossible using the following idiomatic syntax (see #8584): import { gql } from './generated'
const fooQuery = gql`
query Foo {
bar {
baz
}
}
` Instead, the currently supported syntax looks as follows: import { graphql } from './generated'
const fooQuery = graphql(/* GraphQL */`
query Foo {
bar {
baz
}
}
`) Proposed syntax / workflowThe following has the benefit of being more explicit without requiring any more effort than the current approach, while still supporting the more idiomatic tagged template literal syntax people are likely to be using prior to adopting import { gql } from '@apollo/client/core'
import { Foo } from './generated'
const fooQuery: Foo = gql`
query Foo {
bar {
baz
}
}
` Note that in typical usage, the developer would start with an untyped document, Also note that a very close analog of this workflow / syntax is already supported when using typed-document-node and related plugins instead of the preset, only one would have to write |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @revero-doug, Thank you for your proposal; however, the point of the If you are willing to use |
Beta Was this translation helpful? Give feedback.
-
The question I'm wondering is, is there any hope for typescript supporting the template syntax? |
Beta Was this translation helpful? Give feedback.
Hi @revero-doug,
Thank you for your proposal; however, the point of the
client-preset
is to not manipulate generated types.We already have plugins that provide such developer experience.
If you are willing to use
gql
with a tagged template approach, I encourage you to use thetyped-document-nodes
plugin instead of theclient-preset
while we work on improving it: https://www.the-guild.dev/graphql/codegen/plugins/typescript/typed-document-node