3

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?

Kam
  • 126
  • 2
  • 5
  • Did you ever find a solution to this? This is prompting me to reluctantly abandon RQ in favor of Apollo Client :/ – bombillazo Feb 11 '22 at 03:13
  • 1
    The only way I could manage so far was to use `as` ts keyword to cast the type. for example: ```const emoji = media as Emoji``` – Kam Feb 15 '22 at 01:31

0 Answers0