Skip to content

Latest commit

 

History

History
96 lines (72 loc) · 1.97 KB

subscriptions.md

File metadata and controls

96 lines (72 loc) · 1.97 KB

Subscriptions

GraphQL-Subscriptions

Schema

# ...
type Subscription {
  onItemUpserted: Item
}
# ...

Resolver

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}
  })
  // ...
};

GraphQL Query

subkit request \
  --token eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
  --url http://localhost:8080/graphql \
  --query 'subscription onItemUpserted {onItemUpserted {id email}}'

Use Publish/Subscribe programmatically

const {pubsub} = require('subkit');

// subscribe to events by channelName
pubsub.subscribe('channelName', (error, event) => {
  //...
});

// publish event by channelName and payload
pubsub.publish('channelName', {});

Publish via Web-Hooks

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);
});