dotansimha / dotansimha/graphql-typed-document-node

3.1.1 breaks type inference

Open
#130 12 comments 7 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
412
Forks
22
PR merge metrics
No merged PRs in 30d

Description

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:
```graphql
query ExampleQuery($id: ID!) {
user(id: $id) {
id
}
}
```

Generated document:

```typescript
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;

```

Component:

```typescript
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

{t}
;
}
```

With v3.1.0
![image](https://user-images.githubusercontent.com/140714/148182141-15acc97a-a0ed-460d-810f-2efc6b1b9fab.png)

![image](https://user-images.githubusercontent.com/140714/148182159-3f60e51a-5465-47b6-97dc-29ec48f66e53.png)

After upgrade to v3.1.1
![image](https://user-images.githubusercontent.com/140714/148181805-25cefc3b-2e16-47d1-8bfa-f5430bd7d7ae.png)

![image](https://user-images.githubusercontent.com/140714/148181850-7641fb21-f4b8-4907-b830-2ae137583d9a.png)

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:
```typescript
import { DocumentNode } from 'graphql';

declare module '@graphql-typed-document-node/core' {
interface TypedDocumentNode
extends DocumentNode {
__resultType?: Result;
__variablesType?: Variables;
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.