dotansimha / dotansimha/graphql-code-generator

TypedDocumentNode does not use type names generated by base visitor

Open
#4,889 0 comments 2 reactions 0 assignees View on GitHub
core plugins
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Describe the bug**
When using `TypedDocumentNode` and generating an anonymous query

**To Reproduce**

- Go to the GraphQL Code Generator home page
- Select the `TypedDocumentNode` example
- Delete the name of the query in the `operation.graphql` pane

Observe that the generated type for the exported `Document` is `DocumentNode`, but the corresponding types are named `Unnamed_1_Query` and `Unnamed_1_QueryVariables`, so the type *should* be `DocumentNode`.

actual output (just the query types)

```ts
export type Unnamed_1_QueryVariables = Exact<{
userId: Scalars['ID'];
}>;

export type Unnamed_1_Query = (
{ __typename?: 'Query' }
& { user?: Maybe<(
{ __typename?: 'User' }
& UserFieldsFragment
)> }
);

export type UserFieldsFragment = (
{ __typename?: 'User' }
& Pick
);

export const UserFieldsFragmentDoc: DocumentNode = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"UserFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"User"}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"username"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"role"},"arguments":[],"directives":[]}]}}]};
export const Document: DocumentNode = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"userId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}},"directives":[]}],"directives":[],"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":"userId"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"UserFields"},"directives":[]}]}}]}},...UserFieldsFragmentDoc.definitions]};
```

**Expected behavior**

The exported `Document` should have the type `DocumentNode`.

expected output (just the query types)

```ts
export type Unnamed_1_QueryVariables = Exact<{
userId: Scalars['ID'];
}>;

export type Unnamed_1_Query = (
{ __typename?: 'Query' }
& { user?: Maybe<(
{ __typename?: 'User' }
& UserFieldsFragment
)> }
);

export type UserFieldsFragment = (
{ __typename?: 'User' }
& Pick
);

export const UserFieldsFragmentDoc: DocumentNode = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"UserFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"User"}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"username"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"role"},"arguments":[],"directives":[]}]}}]};
export const Document: DocumentNode = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"userId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}},"directives":[]}],"directives":[],"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":"userId"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"UserFields"},"directives":[]}]}}]}},...UserFieldsFragmentDoc.definitions]};
```

**Environment:**

Any and all, including

**Additional context**

Caused by dotansimha/graphql-code-generator#4682 not adding the `TypedDocumentNode` name update (because I didn't realize it wouldn't propagate through, and don't know how *to* propagate through this information).

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.