0
votes

there is no information available on how to mount graphql union type on subscriptions. is it even possible?

type Subscription {
   joinRequest(id: UUID!): MemberUnion!
}

type User { ... }
type Guest { ... }

union MemberUnion = User | Guest

and it's working perfectly with my queries or mutations, just need any information about how to implement with my subscription. joinRequest key is userId and role. role should stand for resolver where either user or guest information should be fetched by passed userId argument.

error I see after trying to launch code as it is now:

"Abstract type "MemberUnion" must resolve to an Object type at runtime for field "Subscription.joinRequest" with value { userId: "3bdb0eb5-f237-40a7-bebc-6263cc18dba5", name: "trolololo3", payload: { ... } }, received "null". Either the "MemberUnion" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function."

my resolvers files which export queries, mutations and subscriptions:

import ....

export default {
  Query: {
    ...User,
    ...Listing,
    ...Products
  },
  Mutation: {
    ...ListingMutations,
    ...ProductsMutations
  },
  Subscription: {
    ...ProductsSubscription,
    ...ListingSubscription
  },
Products: {
    items: ({ id: itemId }) => method({ itemId })
  },
 MemberUnion: {
    // eslint-disable-next-line no-underscore-dangle
    __resolveType: obj => {
      if (obj.role === USER_ROLE.GUEST) {
        return 'Guest';
      }

      if (obj.role === USER_ROLE.USER) {
        return 'User';
      }

      return null;
    }
  },
....

and as I said before, this works fine when I implement MemberUnion on any query or mutation response where I want to select user object which could be user or guest.

1

1 Answers

1
votes

Subscriptions can return abstract types (unions and interfaces) just like queries and mutations. There is nothing different about how you handle abstract types with subscriptions.

If you're seeing that particular error, then the issue is usually with your implementation of the resolveType function for the abstract in question. For example, if you didn't provide a resolveType function in the first place, or if it's written in such a way that it doesn't always return a string with the name of an object type.

Based on the value shown in the error ({ userId: "3bdb0eb5-f237-40a7-bebc-6263cc18dba5", name: "trolololo3", payload: { ... } }), the issue could also be with how you're resolving the joinRequest field. It's possible you don't have a resolver for the field in the first place or at least the shape of the object the resolver is returning does not match the shape expected by GraphQL (i.e. User or Guest).

Without seeing more of the relevant code, it's impossible to know for sure where the problem lies.