Rw 1.0->1.5 upgrade started overwriting __typename for similar object shapes

I created a sample project to show the issue:

Basically, if you have a union type with multiple types of similar shape, it will override the __typename value even if it’s explicitly set. I’m not sure which underlying technology I should submit this issue to, or if there’s a way to fix it. Thanks!

1 Like

Hi @tal and thanks for finding this issue with union types – and for a great reproduction + repo. That always helps!

The issue is:


GraphQL issue with two types of same structure

For the following type definitions:

type One {
  id: String!
  other: String
}

type Two {
  id: String!
  other: String
}

union All = One | Two

The type will always come out as __typename: "One"

{
  __typename: 'One',
  id: 'twoone',
  other: 'other',
}

This happens regardless of what __typename is set to or any typescript types.


If you could write up an issue in GitHub here: Sign in to GitHub · GitHub we can look into this more and fix.

Thanks again!

cc @alicelovescake

I’m curious, if type One and type Two both have the same structure, why have them as separate types?

The reason the typename is resolved to one is because there is no way to uniquely identify one type from the other. Both have the same fields. In the Redwood’s implementation of unions, we have a resolver that looks through all the fields within the union to identify the type.

If you have types that have overlapping fields, you might consider using Interfaces. When you want to use unions, each type has to have an unique identifier. There is an example in the official docs that combines union and interfaces.

Other than that, you can also consider aliasing the field to make it different. Here are some stackoverflow answers I saw: here and here

Let us know if that helps?

The shapes are not the same they have a different __typename. But regardless it’s a regression so it can break people’s code out there.

You are correct that __typename is different and graphql does use it to identify which type is returned by a field that can return multiple types. But __typename is only resolved after the object is resolved. In the example in the docs, Human, Droid and Starship all have different typenames and requesting the same field (name) in their fragment. Because each type has unique fields, even though they are all requesting name, the resolver is able to resolve the type and assign the correct __typename.

I’ll double check with our the GraphQL Guild to see if they have a better solution to resolve this issue. Appreciate you raising your concerns!

2 Likes

I checked with one of the members of the Guild and he agrees that there is no way to resolve the types if they have the exact same fields.

Let me know if you have a realistic example of what you are trying to do or if you have a suggested solution on how to resolve the __typename and the type given the same fields. I can try my best to work with our team to find a solution to your problem!

2 Likes