- Setup and Subscribe to GraphQL-Subscriptions
- Use Publish/Subscribe programmatically
- Publish via Web-Hooks
# ...
type Subscription {
onItemUpserted: Item
}
# ...
export const resolvers = {
// ...
Subscription: {
onItemUpserted: (source, args, context, info) =>
context.loaders.items.find(x => x.id === source.id)
}
// ...
};
export const channels = {
// ...
// implementation of event handler and event filter for the pub/sub channel
onItemUpserted: (options, args) => ({
itemUpsertedChannel: {filter: event => true}
})
// ...
};
subkit request \
--token eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
--url http://localhost:8080/graphql \
--query 'subscription onItemUpserted {onItemUpserted {id email}}'
const {pubsub} = require('subkit');
// subscribe to events by channelName
pubsub.subscribe('channelName', (error, event) => {
//...
});
// publish event by channelName and payload
pubsub.publish('channelName', {});
To specific channel:
curl http://localhost:8080/subscriptions/publish/itemUpsertedChannel -H "content-type:application/json" -d '{"id": "1"}' -XPOST
Optional subscription to specific channel:
import {pubsub} from 'subkit';
pubsub.subscribe('itemUpsertedChannel', msg => {
console.log(msg);
});
To event-web-publish
channel:
curl http://localhost:8080/subscriptions/publish -H "content-type:application/json" -d '{"msg": "Hello SubKit!"}' -XPOST
Optional subscription to event-web-publish
channel:
import {pubsub} from 'subkit';
pubsub.subscribe('event-web-publish', msg => {
console.log(msg);
});