dotansimha / dotansimha/graphql-code-generator

Query and union fragment does not fit interface

Open
#10,481 1 comment 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/core

### Describe the bug

In our project we have an interface that says that some items should have an ID. This is nice to use in the frontend, where we use that generated interface type in some of our viewmodels.
However when we generate queries, where only a subset of items are wanted we find that such a query is not satisfying the interface, even though the individual fragments, used in the query, do satisfy it. The root cause is that the generator still includes "the rest" of the types that implements the interface, but only with the field `__typename`.

Image

(see [https://github.com/wiegell/gql-bug/blob/main/src/generated/graphql.ts]())

Which results in an error like this:

Image

(see [https://github.com/wiegell/gql-bug/blob/main/src/problem.ts]())

### Your Example Website or App

[https://github.com/wiegell/gql-bug/]()

### Steps to Reproduce the Bug or Issue

1. Create fragments that all satisfy a common interface
2. Create a query that only uses those fragments
3. See that the generated query does not satisfy the interface. This also i the case for fragments used deeper in a query (not demonstrated in the repro)

### Expected behavior

This could be mitigated by filtering out the "empty" types before creating the query union type (or union fragment, when the fragments are used deeper in a query)

### Screenshots or Videos

Image

### Platform

* OS: macos
* NodeJS: v20.11.1
* `graphql` version: 16.12.0
* `@graphql-codegen/*` version(s): 6.0.1

### Codegen Config File

```

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
schema: 'graphql/spec.graphql',
documents: 'graphql/queries/**/*.graphql',
generates: {
'./src/generated/graphql.ts': {
plugins: [
'typescript',
'typescript-operations',
'typed-document-node'
]
}
}
};

export default config;
```

### Additional context

*No response*

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.