How to handle union types globally like apollo-client?. Typescript throws Property 'urls' does not exist on type 'Media' for graphql Union

Viewed 324

I have a union type Media = Image | Emoji and I'm using it on many different and deeply nested entities in my graphql schema. I'm also using graphql-code-generator to generate types. I used to use apollo-client to handle my requests and it was handling union types like a charm in the cache.

const cache = new InMemoryCache({
    possibleTypes: {
      Media: ['Image', 'Emoji'],
      ...

But for some reason, I had to migrate to react-query and graphql-request and now every time I want to use object.logo: Media for example and this logo can have different types but typescript throws this error:

Property 'url' does not exist on type 'Media'.
  Property 'url' does not exist on type 'Emoji'.

I don't want to check for the type each time and cast so I wonder how can I implement this globally like what apollo-client did?

0 Answers
Related