Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union types and a default-fragment or wildcard-fragment #956

Closed
valentin-panalyt opened this issue Jun 1, 2022 · 3 comments
Closed

Union types and a default-fragment or wildcard-fragment #956

valentin-panalyt opened this issue Jun 1, 2022 · 3 comments

Comments

@valentin-panalyt
Copy link

I hope I have just missed it, but it seems there is now way to make a graphql client forward compatible so that a server can add a new type to an existing union without breaking the client.

E.g. I imagine a query like:

query {
  trafficLight {
    __typename
    ... on Green {
      field1
      field2
    }
    ... on Red {
      field3
      field4
    }
    ... on *default {
      ???
    }
  }
}

Is something like that already in the Spec or any reason it's not in there and a way to work around it?

@benjie
Copy link
Member

benjie commented Jun 2, 2022

Looking at your example, if there are fields that you can query on any arbitrary future type (???) then what you have is actually an interface, not a union.

Unions are forward compatible, clients must be written to support this. Typically this is handled with a switch statement with a default case that either does nothing (return null) or displays a placeholder indicating that the data is not supported/understood; e.g.:

const l = data.trafficLight;
switch (l.__typename) {
  case 'Green': return <Green field1={l.field1} />;
  case 'Red': return <Red field3={l.field3} />;
  default: return <UnknownLightPleaseUpdate typename={l.__typename} />;
} 

Also of relevance: #951

@benjie
Copy link
Member

benjie commented Jun 17, 2022

@valentin-panalyt Did the above solve your issue? If so, can we close this issue?

@valentin-panalyt
Copy link
Author

Yes, the issue can be closed. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants