Search code examples
apolloapollo-clientreact-apollo

Apollo client only returns { __typename: 'Typehere' } when fetchPolicy is not 'no-cache'


I have a query that returns the sample data below

{ 
  __typename: 'Typehere',
  name: 'type',
  address: 'address type'
}

When I use useQuery with an option of fetchPolicy: 'cache-first' the GQL API returns the correct data. But when I use console.log(response), the data will now transform to

{ 
  __typename: 'Typehere'
}

Api call and response for fetchPolicy: 'cache-first' enter image description here

console.log(response) enter image description here

Why is this happening?

I tried fetching the data using fetchPolicy: 'no-cache' and works without issues, however, I need to cache it for optimization. I've also tried other fetch policies stated in the apollo client docs.


Solution

  • Solved it using possibleTypes

    const cache = new InMemoryCache({
      typePolicies: {....}
      possibleTypes: {
        Typehere: ['Type1', 'Type2']
      }
    });
    

    Turns out that apollo client needs to know possible implementations of Typehere