graphql-awssqs-pubsub
This package implements the PubSubEngine Interface from the graphql-subscriptions package and also the new AsyncIterator interface. It allows you to connect your subscriptions manger to a AWS SQS mechanism to support multiple subscription manager instances.
Installation
npm install graphql-awssqs-pubsub
or
yarn add graphql-awssqs-pubsub
Using as AsyncIterator
Define your GraphQL schema with a Subscription
type:
schema { query: Query mutation: Mutation subscription: Subscription} type Subscription { somethingChanged: Result} type Result { id: String}
Now, let's create a simple SQSSubPub
instance:
;const pubsub = ;
Now, implement your Subscriptions type resolver, using the pubsub.asyncIterator
to map the event you need:
const SOMETHING_CHANGED_TOPIC = 'something_changed'; const resolvers = Subscription: somethingChanged: pubsub
Subscriptions resolvers are not a function, but an object with
subscribe
method, that returnsAsyncIterable
.
Calling the method asyncIterator
of the SQSPubSub
instance will subscribe to the topic provided and will return an AsyncIterator
binded to the SQSPubSub instance and listens to any event published on that topic.
Now, the GraphQL engine knows that somethingChanged
is a subscription, and every time we will use pubsub.publish
over this topic, the SQSPubSub
will PUBLISH
the event to all other subscribed instances and those in their turn will emit the event to GraphQL using the next
callback given by the GraphQL engine.
pubsub;
The topic doesn't get created automatically, it has to be created beforehand.
If you publish non string data it gets stringified and you have to parse the received message data.
Receive Messages
You might extract the data (Buffer) in there or use a common message handler to transform the received message.
{ return ...attributes text: data ;}
The can use custom message handler
test illustrates the flexibility of the common message handler.
Dynamically use a topic based on subscription args passed on the query:
const resolvers = Subscription: somethingChanged: pubsub
Using both arguments and payload to filter events
; const resolvers = Subscription: somethingChanged: subscribe:
Creating the AWSSQS PubSub Client
; const pubSub = options commonMessageHandler
Options
This are the options which are passed to the internal Google PubSub client. The client will extract credentials, project name etc. from environment variables if provided. Have a look at the authentication guide for more information. Otherwise you can provide this details in the options.
const options = projectId: 'project-abc' credentials: client_email: 'client@example-email.iam.gserviceaccount.com' private_key: '-BEGIN PRIVATE KEY-\nsample\n-END PRIVATE KEY-\n' ;
commonMessageHandler
The common message handler gets called with the received message from AWS SQS PubSub. You can transform the message before it is passed to the individual filter/resolver methods of the subscribers. This way it is for example possible to inject one instance of a DataLoader which can be used in all filter/resolver methods.
const getDataLoader = ...;const commonMessageHandler = id dataLoader: ;
const resolvers = Subscription: somethingChanged: dataLoader
Author
Acknowledgements
This project is mostly inspired by graphql-redis-subscriptions.