Graphql-code-generator: Missing `__typename` when explicitly set

Created on 4 Oct 2019  Â·  6Comments  Â·  Source: dotansimha/graphql-code-generator

When generating the TypeScript definitions by typescript-operations with nonOptionalTypename=false and skipTypename=true, __typename is missing for the type definitions even if it is explicitly requested in the query.

It is possible that it is the intended behavior. If it is, is there a way to achieve this?

To Reproduce

  1. GraphQL schema:
# Not relevant for the issue
  1. GraphQL operations:
query search($search: String!) {
  search(first: 10, search: $search) {
    nodes {
      __typename
      id
      ... on Movie {
        title
      }
      ... on Person {
        name
      }
    }
  }
}

  1. codegen.yml config file:
schema: schema.json
overwrite: true
generates:
  src/graphql.tsx:
    documents: 'src/**/*.gql'
    plugins:
      - typescript
      - typescript-operations
    config:
      nonOptionalTypename: false
      skipTypename: true
      preResolveTypes: true

Expected behavior

__typename should be generated when explicitly requested.

Additional context

__typename was correctly generated in 1.7.0.

Environment:

  • OS: macOS 10.14.6
  • @graphql-codegen/add: 1.8.0
  • @graphql-codegen/cli: 1.8.0
  • @graphql-codegen/fragment-matcher: 1.8.0
  • @graphql-codegen/typescript: 1.8.0
  • @graphql-codegen/typescript-operations: 1.8.0
  • NodeJS: 12.10.0
bug plugins waiting-for-release

Most helpful comment

@ooflorent I think I managed to fix that, see: https://github.com/dotansimha/graphql-code-generator/pull/2700
Can you please try this alpha? 1.8.2-alpha-a83224e6.9

All 6 comments

Seems like this is caused by some subdependency, because if I went back to 1.7, but updated all the subdependencies, it still wasn't giving me __typename. I had to revert my package-lock.json to the exact subdependencies as before.

I redid the test and it kinda works.

Let's consider this schema:

type Search {
  search: [SearchResult!]!
}

type Movie { … }
type Person { … }

union SearchResult = Movie | Person

An the following query:

query q {
  search {
    ... on Movie { __typename id title  }
    ... on Person { __typename id name }
  }
}

The generated types include __typename. However, if the query is rewritten like this:

query q {
  search {
    __typename
    ... on Movie { id title  }
    ... on Person { id name }
  }
}

Then __typename is not included.

@ooflorent I think I managed to fix that, see: https://github.com/dotansimha/graphql-code-generator/pull/2700
Can you please try this alpha? 1.8.2-alpha-a83224e6.9

Thanks @dotansimha it fixes the the issue!

Fixed in 1.8.2.

Was this page helpful?
0 / 5 - 0 ratings