Version: Next

Subscriptions

Graphback provides out of the box subscriptions support by providing one of the PubSubEngines from https://github.com/apollographql/graphql-subscriptions library. Developers can connect to any publish subscribe mechanism that is officially supported by graphql-subscriptions or even write their own. Graphback templates by default using InMemoryPubSubEngine which is not designed to be used for production use cases.

We recomend following engines:

Subscriptions explained#

Graphback provides subscriptions for every type of the operation that is happening on the server. This means that developers can explicitly subscribe to create, update and delete operations for particular resource.

Subscriptions can be also suppressed by developers by enabling or disabling subscription CRUD flags when initializing Graphback server

{
...
subCreate: true
subUpdate: true
subDelete: false
}

Configuration#

By default, Graphback uses an in-memory PubSub mechanism. You can configure your own in buildGraphbackAPI through the serviceCreator.

In this example, we are using a MQTT enabled broker with @aerogear/graphql-mqtt-subscriptions.

import { buildGraphbackAPI, createCRUDService } from 'graphback'
import { MQTTPubSub } from '@aerogear/graphql-mqtt-subscriptions';
const mqttConfig = {...};
const client = mqtt.connect(mqttConfig.mqttHost, mqttConfig);
// creates a schema, CRUD resolvers, services and data providers
const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(schema, {
serviceCreator: createCRUDService({
pubSub: new MQTTPubSub({ client })
})
});
info

In this example the default createCRUDService service creator is used. There can be variations in how to configure your PubSub mechanism depending on what service creator you choose to use.

Changing Subscription Topics#

Some of the pub sub mechanism will require different format of the topic. Graphback CRUD services expose method that can be used to override how topics are build. For example for AMQ we can create extension of the CRUD service as follows

export class AMQCRUDService extends CrudService {
protected subscriptionTopicMapping(triggerType: GraphbackOperationType, objectName: string) {
// Support AMQ topic creation format
return `graphql/${objectName}_${triggerType}`
}
}

Running example#

If you wish to use authorization mechanism you can try it on DataSync starter project