apollographql / apollo-link-state

✨ Manage your application's state with Apollo!
MIT License
1.4k stars 100 forks source link

Unions and Interfaces #316

Open ralphchristianeclipse opened 6 years ago

ralphchristianeclipse commented 6 years ago

https://www.apollographql.com/docs/apollo-server/features/unions-interfaces.html

this seems to not work at all on local state.

I have this snippet which resolves the correct type depending on the user.role

const roles = {
  employers: 'Employer',
  jobseekers: 'JobSeeker'
};

  User: {
    __resolveType: user => roles[user.role],
    name: ({ name = '' }) => name,
    role: ({ role = '' }) => role
  },
  JobSeeker: {
    link: ({ id }) => ``,
    image: () => ''
  },

and do it like this on queries

query Auth {
  auth @client {
    id
    role
    ... on JobSeeker {
      link
    }
    ... on Employer {
      id
    }
  }
}

which results to a __typename of User instead of Employer or JobSeeker

i can do this manually every query but it seems reusing it on the interface level would be easier

hwillson commented 6 years ago

__resolveType is a graphql-tools feature, and is not currently supported with local resolvers. Marking as a feature request - thanks!