Subscriptions and interfaces #174
-
I am currently porting over a subscription from our apollo server to giraph. We have a single resolver for our subscription, which can emit different kinds of events, which all implement an interface. It looks something like this: export class BaseSampleEvent {
id: string;
constructor(id: string) {
this.id = id;
}
}
builder.interfaceType(BaseSampleEvent, {
name: 'IBaseSampleEvent',
fields: (t) => ({
id: t.exposeID('id'),
}),
});
export class NewUserEvent extends BaseSampleEvent {
...
}
builder.objectType(NewUserEvent, {
name: 'NewUserEvent',
interfaces: [BaseTeamEvent],
isTypeOf: (value) => value instanceof NewUserEvent,
fields: (t) => ({
id: t.exposeID('id'),
}),
});
builder.subscriptionField('sampleEvents', (t) => {
return t.field({
type: BaseSampleEvent
subscribe: () => {
// from graphql-subscriptions lib
return withFilter(...)()
},
resolve: (payload) => payload as BaseSampleEvent
})
}) When I attempt to publish the
Not sure what I'm doing wrong here as I do have If that is the case, do I need the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Are your events actually instances of the classes you are defining, or are they just objects with the same properties? |
Beta Was this translation helpful? Give feedback.
-
They're actually instances of the classes. It seems I can't remove But, I think I figured out what's going on. I noticed this behavior too when it came to writing the subscribe function in our apollo server. The We actually add in an
|
Beta Was this translation helpful? Give feedback.
They're actually instances of the classes. It seems I can't remove
isTypeOf
without removing the interfaces property. If I remove both, the event types do not implement anything in the generated schema.But, I think I figured out what's going on. I noticed this behavior too when it came to writing the subscribe function in our apollo server.
The
value
passed toisTypeOf
is serialized to an object. When I console.logged out the event inisTypeOf
, it was a pure object. I wonder if the serialization is a native gql thing or specific to the pubsub lib being used.We actually add in an
eventType
property in our events, and so when I compared that property, it works!