Skip to content

Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects#9619

Merged
Tobbe merged 11 commits intomainfrom
dt-gql-client-typed-doc-node
Dec 28, 2023
Merged

Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects#9619
Tobbe merged 11 commits intomainfrom
dt-gql-client-typed-doc-node

Conversation

@dthyresson
Copy link
Copy Markdown
Contributor

@dthyresson dthyresson commented Dec 2, 2023

Fixes #9232

See the above issue for key points.

TLDR;

Before this PR, it wasn't possible to infer types from the useQuery or useMutation GraphQL hooks.

Also, TypedDocumentNode was not conveniently exported from @redwoodjs/web (and is now).

Now, you can do:

import type { FindPostByIdVariables, FindPostById } from 'types/graphql'

import type {
  CellSuccessProps,
  CellFailureProps,
  TypedDocumentNode,
} from '@redwoodjs/web'

import Post from 'src/components/Post/Post'

export const QUERY: TypedDocumentNode<
  FindPostById,
  FindPostByIdVariables
> = gql`
  query FindPostById($id: Int!) {
    post: post(id: $id) {
      id
      title
      body
      authorId
      createdAt
    }
  }

and infer types:

image

image

For context:

See https://the-guild.dev/blog/typed-document-node

TypedDocumentNode is a development tool for creating fully typed DocumentNode objects. It means that just by passing the GraphQL query/mutation/subscription/fragment to a supporting GraphQL client library, you'll get fully-typed result and variables objects.

This is made possible by TypeScript type inference.

TypedDocumentNode is a development tool that allows you to create fully typed DocumentNode objects. This means that by simply passing your GraphQL query, mutation, subscription, or fragment to a supporting GraphQL client library, you'll get fully-typed result and variables objects. This is made possible by TypeScript type inference.

@dthyresson dthyresson added the release:chore This PR is a chore (means nothing for users) label Dec 2, 2023
@dthyresson dthyresson added this to the next-release milestone Dec 2, 2023
@dthyresson dthyresson self-assigned this Dec 2, 2023
@dthyresson dthyresson changed the title DRAFT: chore: Allow GraphQL documents to be typed with a TypedDocumentNode for DRAFT: chore: Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects Dec 2, 2023
@dthyresson dthyresson changed the title DRAFT: chore: Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects chore: Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects Dec 4, 2023
@dthyresson dthyresson requested a review from Tobbe December 4, 2023 17:48
@dthyresson dthyresson marked this pull request as ready for review December 4, 2023 18:02
@dthyresson
Copy link
Copy Markdown
Contributor Author

@Tobbe and others: one question I had is if the generators should be updated to detect if using TS and infer types as above. If som, we can make this a separate PR enhancement to update the generators.

Copy link
Copy Markdown
Contributor

Tobbe commented Dec 4, 2023

@dthyresson Initial reaction, without looking at any code, is yes, we should, and yes, let’s do that in a separate PR 🙂

@dthyresson
Copy link
Copy Markdown
Contributor Author

dthyresson commented Dec 5, 2023

@dthyresson Initial reaction, without looking at any code, is yes, we should, and yes, let’s do that in a separate PR 🙂

@Tobbe I added issue #9632 to implement/discuss

Comment thread packages/internal/src/generate/clientPreset.ts
@Tobbe Tobbe added release:feature This PR introduces a new feature and removed release:chore This PR is a chore (means nothing for users) labels Dec 28, 2023
@Tobbe Tobbe changed the title chore: Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects Dec 28, 2023
@Tobbe Tobbe enabled auto-merge (squash) December 28, 2023 15:05
@Tobbe Tobbe merged commit ce96c02 into main Dec 28, 2023
@Tobbe Tobbe deleted the dt-gql-client-typed-doc-node branch December 28, 2023 15:42
dac09 added a commit to dac09/redwood that referenced this pull request Dec 29, 2023
…od into feat/api-skip-prebuild-try-2

* 'feat/api-skip-prebuild-try-2' of github.com:dac09/redwood:
  chore(router): Miniscule fixes
  chore(router): Move useMatch to its own file (redwoodjs#9770)
  Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects (redwoodjs#9619)
  chore(ci): retry detectChanges on error, and await result (redwoodjs#9772)
@jtoar jtoar modified the milestones: next-release, v7.0.0 Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:feature This PR introduces a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug?]: The TData and TVariables types on useMutation cannot be inferred properly when using TypedDocumentNode

3 participants