0

We're using graphql-codegen for generating types for graphql operations. On our schema we have 50+ types that implement a common base interface Item.

Example: All types implement Item:

type Query {
  datasource(value: String!): Item
}

interface Item {
  name: String!
}

type TypeA implements Item {
  name: String!
  other: String!
}

type TypeB implements Item {
  name: String!
  somethingElse: String!
}

type TypeC implements Item {
  name: String!
}

Example Query:

query MyData($datasource: String!) {
    datasource(value: $datasource) {            
        ... on TypeA {
            name
            other  
        }      
    }
}

When generating Typescript with the typescript-operations plugin, we end up with types that look like this:

export type MyDataQuery = {
    __typename?: 'Query';
    datasource?:
        | { __typename?: 'TypeA'; name: string; other: string }
        | { __typename?: 'TypeB' }   # <!-- not needed
        | { __typename?: 'TypeC' }   # <!-- not needed
        | null
        | undefined;
};

Notice TypeB and TypeC are also included as options. This leads to huge generated types with our 50+ types inheriting Item.

Is there a way to specify exactly what type should be used (possibly in the Query?) instead of having all types that inherit Item generated?

Mark Lowe
  • 1,056
  • 8
  • 16
  • all valid, all generated ... what criteria? change base types (by usage/groups/logic/your criteria), don't [over-]use inheritance when not required – xadm Nov 13 '21 at 09:14
  • inheritance is not under our control unfortunately as the schema is given by an existing application – Mark Lowe Nov 23 '21 at 23:00

0 Answers0