apollographql / apollo-tooling

✏️ Apollo CLI for client tooling (Mostly replaced by Rover)
https://apollographql.com
MIT License
3.04k stars 468 forks source link

Generated types include unused unions. #2433

Open simeonkerkola opened 3 years ago

simeonkerkola commented 3 years ago

Not sure if it is correct behaviour to include all possible union types, even the unused ones, to a generated type file. When codegen is run for this query:

export const NEW_SKILL_CARD_SEARCH_QUERY = gql`
query SearchQuery {
  search(query: "*") {
    hits {
      ... on CVHit {
        __typename
        id
      }
    }
  }
}
`

It gives out typescript like this:

export interface SearchQuery_search_hits_CustomerHit {
  __typename: "CustomerHit" | "ProjectHit" | "SuggestionHit";
}

export interface SearchQuery_search_hits_CVHit {
  __typename: "CVHit";
  id: string | null;
}

export type SearchQuery_search_hits = SearchQuery_search_hits_CustomerHit | SearchQuery_search_hits_CVHit;

export interface SearchQuery_search {
  __typename: "SearchResult";
  hits: (SearchQuery_search_hits | null)[] | null;
}

My question is, why are types "CustomerHit" | "ProjectHit" | "SuggestionHit" included in generated types, even thou only "CVHit" is used in the search query? Now I will have to trick typescript to comply like this: search.hits as SearchQuery_search_hits_CVHit[]

dobesv commented 2 years ago

Just because your query only specifies fields for specific union variants doesn't mean the server cannot return one of the other variants.