apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

PossibleTypes not working as intended #11878

Closed Gowthaman-Opti-Twin closed 5 months ago

Gowthaman-Opti-Twin commented 5 months ago

I have a query that uses union types inside it.

sample query

person {
      ... on jedi{
        id
        lightsaber
      }
      ... on mandalorian{
        id
        beskar
      }
    }

when I try to use useQuery from apolloClient it is returning the person object with empty brackets [{},{}] but when using fetch() it is returning [{id:232, lightsaber:green},{id:242, beskar: old}]

I tried specifying the union type in the apollo client

const client = new ApolloClient({
  link: concat(authMiddleware, httpLink),
  cache: new InMemoryCache({
    addTypename: false,
    possibleTypes:{Person:['jedi','mandalorian']}
  }),
})

it is not returning the any properties inside the object just an empty object {}

here is the useQuery

const {data:QueryData} = useQuery<PersonQuery>(GET_PERSON)

dylanwulf commented 5 months ago
addTypename: false,

This part is likely your problem. Apollo client needs to have the __typename field available for this functionality to work correctly

alessbell commented 5 months ago

Yes, thanks @dylanwulf!

@Gowthaman-Opti-Twin can you confirm this solved your issue?

Gowthaman-Opti-Twin commented 5 months ago

Yep, that's the issue. It is working fine now. Thanks

github-actions[bot] commented 5 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

github-actions[bot] commented 4 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.