Graphql-code-generator: [TypeScript] Missing type generation for a union that only resolves __typename

Created on 4 Jan 2019  路  6Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
If you only query a union for the __typename, codegen doesn't generate a type but still references it. See example:
screen shot 2019-01-04 at 15 57 06

To Reproduce
Steps to reproduce the behavior:

  1. Write a schema containing a union
  2. Query for the union __typename only

Expected behavior
A type with only the __typename should be generated. I.e. in this case the generated file should also contain:

export type UserUnion = {
  __typename: "OneUnion" | "TwoUnion";
}
bug plugins waiting-for-release

All 6 comments

@csoLs Just my 2c (I'm looking to fix this bug) - it would be awesome if your copy-paste text content in report, not just screenshots. That will simplify life just a bit, no offense.

Being able to reproduce bug quickly is invaluable :)

@xanf cool, and fair point 馃憤

Schema:

schema {
  query: Query
}
type Query {
  user(id: ID!): User
}
type User {
  id: ID!
  union: UserUnion
}
union UserUnion = OneUnion | TwoUnion
type OneUnion {
  id: ID!
  name: String!
}
type TwoUnion {
  id: ID!
  name: String!
}

Document:

query findUser($userId: ID!) {
  user(id: $userId) {
    id
    union {
      __typename    
    }
  }
}

Config:

generates:
  client-types.ts:
    - typescript-common
    - typescript-client

Thanks for reporting if @csoLs !
We are filtering __typename and ignoring it from the fields list. It requires a more complex solution.

At the moment we are looking for refactoring the typescript packages, and use the same approach as flow plugins (use Pick and build a single type only, instead of namespace with multiple interfaces), and we also implemented there a solution for __typename fields).

Hi, I think this my question regards something else, but:
shouldn't the __typename field be required? It is even mentioned in the query. Shouldn't __typename be non-nullable by default?

Edit: Apollo client is requiring for __typename by default and is using it for caching responses etc., it would be very inconvenient with nullable field.

Fixed in the recent refactor :)

Fix available in 1.0.0 馃憤

Was this page helpful?
0 / 5 - 0 ratings