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
# Not relevant for the issue
query search($search: String!) {
search(first: 10, search: $search) {
nodes {
__typename
id
... on Movie {
title
}
... on Person {
name
}
}
}
}
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:
@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.0Seems 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 added a test for it: https://github.com/dotansimha/graphql-code-generator/commit/d8e6d9bf47ac0db68224c84d874ca1e1ab98872f and it seems like it works.
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.
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