Skip to content

Query and union fragment does not fit interface #10481

@wiegell

Description

@wiegell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions