question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TypeScript] Add __typename to typings

See original GitHub issue

I use GraphQL interfaces. When querying for a field that might be any type implementing a specific interface I need to know which implementation it is, for this I currently use __typename.

Would be great if you would add the __typename field for all typings (should probably be an optional field).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
leebensoncommented, Apr 4, 2019

Keep in mind that for example apollo-client always selects __typename even if it’s not specified in the query.

This is true, but because GraphQL Code Generator doesn’t include __typename on individual fields, TS only reports it being available at the top-level.

Example –

Given something like this:

fragment Source on Source {
  __typename  # <-- explicitly requested on the fragment
  id
  name
  type
}

query Builder(
  $source: Boolean!
  $sourceId: ID!
) {

  # ... other types omitted for brevity...

  source: node(id: $sourceId) @include(if: $source) {
    __typename # <-- explicitly requested at the root-level, e.g. same as the fragment
    ...BuilderSource
  }
}

I get __typename on the root-level query:

Screenshot 2019-04-04 at 12 14 01

But not on the individual source field,

Screenshot 2019-04-04 at 12 15 21

TL;DR - We should get __typename on fields, too.

1reaction
dotansimhacommented, May 1, 2019

You are right, @leebenson , I’m working on a fix. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
"typename" , where "typename" can be one of typeof operator's return ... Just like interfaces, type aliases can also be generic - we...
Read more >
TypeScript with GraphQL done right - Charly Poly
TypeScript helps with typing data and following how the data is used and transformed by subsequent functions or method calls.
Read more >
TypeScript with Apollo Client - Apollo GraphQL Docs
Typing Render Prop components. To type render prop components, you'll first define a GraphQL query using the generated gql function (from src/__generated__/ ...
Read more >
How To Create Custom Types in TypeScript - DigitalOcean
Though the basic types in TypeScript will cover many use cases, creating your own custom ... Add a double colon and then add...
Read more >
How to set a property type to textable representation of Type ...
You might feel that this is redundant, but remember that structural typing means it's not. The type name Bar is irrelevant, and interface ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found