Skip to content

3.1.1 breaks type inference #130

@aloker

Description

@aloker

After I upgraded from 3.1.0 to 3.1.1 I got a lot of type errors in my project.

My setup:

  • graphql-core-generator with @graphql-codegen/typed-document-node 2.2.2
  • urql 2.0.6 with @urql/preact 2.0.3
  • graphql 16
  • typescript 4.5.4

Query:

query ExampleQuery($id: ID!) {
  user(id: $id) {
    id
  }
}

Generated document:

export type ExampleQueryResult = { user?: { __typename: 'User', id: string } | null | undefined };

export type ExampleQueryVariables = Exact<{
  id: Scalars['ID'];
}>;

export const ExampleQueryDocument = {
  kind: "Document",
  definitions: [
    {
      kind: "OperationDefinition",
      operation: "query",
      name: { kind: "Name", value: "ExampleQuery" },
      variableDefinitions: [
        {
          kind: "VariableDefinition",
          variable: { kind: "Variable", name: { kind: "Name", value: "id" } },
          type: {
            kind: "NonNullType",
            type: { kind: "NamedType", name: { kind: "Name", value: "ID" } },
          },
        },
      ],
      selectionSet: {
        kind: "SelectionSet",
        selections: [
          {
            kind: "Field",
            name: { kind: "Name", value: "user" },
            arguments: [
              {
                kind: "Argument",
                name: { kind: "Name", value: "id" },
                value: {
                  kind: "Variable",
                  name: { kind: "Name", value: "id" },
                },
              },
            ],
            selectionSet: {
              kind: "SelectionSet",
              selections: [
                { kind: "Field", name: { kind: "Name", value: "id" } },
              ],
            },
          },
        ],
      },
    },
  ],
} as unknown as TypedDocumentNode<ExampleQueryResult, ExampleQueryVariables>;

Component:

import { ExampleQueryDocument } from '$api';
import { useQuery } from '@urql/preact';

export function ExampleComponent() {
  const [{ data }] = useQuery({
    query: ExampleQueryDocument,
    variables: {
      id: '123',
    },
  });

  const t = data?.user?.id;

  return <div>{t}</div>;
}

With v3.1.0
image

image

After upgrade to v3.1.1
image

image

This seems to be caused by the removal of __resultType and __variablesType in 6ee77c2#diff-9a4ceebe7c6f86856371906c3f061d3b56b7457022b05179884a113e7ced67e8

If I add the properties back using interface augmentation, it works again:

import { DocumentNode } from 'graphql';

declare module '@graphql-typed-document-node/core' {
  interface TypedDocumentNode<Result = { [key: string]: any }, Variables = { [key: string]: any }>
    extends DocumentNode {
    __resultType?: Result;
    __variablesType?: Variables;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions