apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.59k stars 133 forks source link

PubSubEngine and AsyncIterator state? #121

Closed jthegedus closed 4 years ago

jthegedus commented 6 years ago

[Edit] I'm going to do some more reading and see if I can figure this out myself. Will update with answers or a more succinct question once I've done so. Essentially I want to know the following. What state is kept and how are things triggered (shown in code, not just some diagram) across these parts of a GraphQL Subscription Server:


Firstly, can someone please clarify the purpose of the PubSubEngine for me. Is it meant to notify other instances of the GraphQL server that a subscribe event needs to fire, or is it's publish used to fire the subscribe event on the current instance, or both? If it indeed does fire the subscribe event when publish is called, what GraphQL server code is then executed next? How does this call ultimately get to the point where it pushes data to the client?

What code in the GraphQL server gets executed during/after publish is called? Eg, in this repo, what's executed by this line this.ee.emit(triggerName, payload);

I also have some questions about how the state is managed across the PubSubEngine and AsyncIterator.

For instance, in the unsubscribe method of the PubSubEngine, the signature says it requires a subId: number which I assume is the same id returned from the PubSub subscribe method. Is the subId being passed solely from the async-iterator?

I see in the graphql-redis-subscriptions that the async-iterator stores a list of the subscription ids and the event names, and then the subIds are mapped to the [triggername, onMessage] pair in the pub-sub-engine. I'm confused about where and under which variables the onMessage method is triggered.

@davidyaha any insight would be appreciated.

benseitz commented 6 years ago

Any news on that? I would also be very interested.

ghost commented 4 years ago

Hi @jthegedus PubSubEngine is just abstract class which is describing interface or "contract" for others to implement. It will allow to create libraries like graphql-redis-subscriptions and others which are able to work with graphql server without modifications - because they all implements same interface...

To your second question, on what code is executed when you publish something - it basically depends on what you are listening, if you did not created triggers for which you are publishing event, then nothing happens, otherwise async iterator created listeners here https://github.com/apollographql/graphql-subscriptions/blob/e57f4eb6e5a544ac6b5c63aaed9d7b50b21e858f/src/event-emitter-to-async-iterator.ts#L41 and will execute callbacks once the proper event was triggered

jthegedus commented 4 years ago

@dusan-dragon Thanks for your response.

I was trying to get to the bottom of where the state of the subscription->event pairing was stored and whether or not a plugin would be able to configure that location.

@benseitz Feel free to reopen if you have any unanswered questions from this thread.