dotansimha / dotansimha/graphql-code-generator

Subscription "NameOfSubscription" must select only one top level field when using addTypenameSelectionDocumentTransform and generating code for subscription

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

Description

### Which packages are impacted by your issue?

@graphql-codegen/client-preset

### Describe the bug

The `__typename` should not be added to the root node of a subscription when using `addTypenameSelectionDocumentTransform` with `documentTransforms` since a single root node is expected and the code generator fails because of that

### Your Example Website or App

https://github.com/Sojaner/graphql-code-generator-issue-sandbox-template

### Steps to Reproduce the Bug or Issue

1. Clone the [issue-sandbox-template repo](https://github.com/Sojaner/graphql-code-generator-issue-sandbox-template)
2. Install packages using yarn
3. `yarn generate` **(Test without the `addTypenameSelectionDocumentTransform` - Works fine)**
4. `yarn generate --add-typenames` **(Test with the `addTypenameSelectionDocumentTransform` - This will fail)**
5. `yarn generate --add-fixed-typenames` **(Test with the _patched_ `addTypenameSelectionDocumentTransform` - Works fine)**

### Expected behavior

Generating types from all types of documents should work when using the `addTypenameSelectionDocumentTransform` document transformer, but currently, the subscription types will cause an error and fail the code generation.

### Platform

- OS: Windows
- NodeJS: 20.11.1
- `graphql` version: N/A
- `@graphql-codegen/client-preset` version: 4.2.4

### Codegen Config File

```typescript
import { CodegenConfig } from "@graphql-codegen/cli";
import { addTypenameSelectionDocumentTransform } from "@graphql-codegen/client-preset";

const config: CodegenConfig = {
schema: "schema.graphql",
documents: "document.graphql",
generates: {
"types.ts": { plugins: ["typescript", "typescript-operations"], documentTransforms: [ addTypenameSelectionDocumentTransform] },
},
};

export default config;
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Clone the linked issue-sandbox-template, install its packages, and run yarn generate with and without --add-typenames to reproduce the subscription failure. Start by tracing addTypenameSelectionDocumentTransform and the client preset documentTransforms path; done means subscription documents generate successfully while preserving the single top-level field requirement.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.