API does not return `__typename`

Issue Description: What’s happening?

I want each record in the data I return from the GraphQL API to include its own type. Usually GraphQL APIs expose the field name __typename that has what I need.

When I try to do this in the API explorer, this doesn’t work. I can see that __typename is clearly included in the schema, because I don’t get an error when I query it and the playground gives me details about it.

Reproduce the Issue: What steps can someone take to replicate the problem?

  1. Go to API explorer
  2. Query any data type and return the field __typename
  3. Observe no error message, but also no __typename in the return fields

Example:

{
  usersList {
    items {
      __typename
    }
  }
}

Expected Behavior: What did you expect to happen?

  1. __typename should be returned (preferred), OR
  2. Error should be thrown, as invalid field name is used in the query, and API explorer should not expose __typename.

Expected response:

{
  "data": {
    "usersList": {
      "items": [
        {
          "__typename": "User"
        }
      ]
    }
  }
}

Actual Behavior: What actually happened?

Actual response:

{
  "data": {
    "usersList": {
      "items": [
        {}
      ]
    }
  }
}

More details or screenshot

This is important because having the type means not having to maintain field mappings on the client side. There is no particular reason why blogPosts.authors should contain Users, but if each record under .authors has its type included, I can have lighter-weight, more generic client-side code where I don’t have to explicitly maintain that level of schema configuration. There’s a package I use to treat the response data that needs the types.

Other GraphQL APIs do return __typename. GraphCMS recommends using it in their documentation: Programmatically create forms & submissions with NextJS & GraphQL | GraphCMS

If there’s another field I could use for this, that would be fine. I couldn’t find another way to do this though, and as I said __typename clearly is part of the schema.

(I did not try to get this via the SDK, but I assume the situation is the same there.)

1 Like

Hello!
Thanks for the feedback.
We already know about this issue. Actually, the __typename field still exists, but it’s hidden from the API Explorer responses on purpose. We will discuss the feature to see the __typename field directly from the API Explorer with the dev team on the nearest sprint planning and consider adding this to our backlog. Meanwhile, I can suggest using the following tools to get the __typename field as a temporary solution:

  • GraphQL Network Inspector (Google Chrome tools)

Add it to your Google Chrome, refresh the page, open DevTools (F12), and click on >> to find the GraphQL Network tool in the list. Then refresh the page with API Explorer and execute your query, find it in the GraphQL Network Inspector list (probably the last one) and __typename will be there.

  • Postman, Altair GraphQL, Apollo Plugin for Chrome, or any other API client.
1 Like

Hello :wave:

I am experiencing the same issue.

On the front-end side, Apollo is able to return a __typename along with the object requested.

However, on the back-end side, there is no way to access the __typename.

This would provide convenience for better code factoring and type checking.

Do you have an alternative solution in the meanwhile?

Regards,

Andréas

1 Like

Hello @andreasblondeau !
I’m afraid using the side tool is the only option till we properly fix this.
Thanks for the feedback.

1 Like