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.

3.1.1 breaks type inference

See original GitHub issue

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

My setup:

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 https://github.com/dotansimha/graphql-typed-document-node/commit/6ee77c273422bea7df58f92607e54fe73eba673a#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;
  }
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
stephane303commented, Jan 25, 2022

Glad I found this thread, I confirm @graphql-typed-document-node/core: 3.1.1 is breaking the inference, 3.1.0 is good.

3reactions
lensbartcommented, Jun 2, 2022

Works here, too. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

9.6. Type Inference — OCaml Programming - GitHub Pages
Type Inference #. OCaml and Java are statically typed languages, meaning every binding has a type that is determined at compile time—that is,...
Read more >
Java Type Inference Is Broken: Can We Fix It? - RICE CS
compiler includes a pragmatic but flawed type inference al- ... This paper dissects the type inference algorithm of Java ... 3.1.1 Types.
Read more >
Type Inference, Type Improvement, and Type Simplification in ...
with a type inference algorithm, based on the theory of qualified types, to correctly capture ... 3.1.1 The Notion of Record and the...
Read more >
10.5.3. Finishing Type Inference
Next, we introduced the unification algorithm for solving constraint sets. That algorithm produces as output a sequence S of substitutions, or it fails....
Read more >
Why does regular function break type inference?
UPDATE type Constructors = | BooleanConstructor | ObjectConstructor | StringConstructor type DefaultFactory<T> = (props: Data) => T; ...
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