All Projects → axelspringer → Graphql Google Pubsub

axelspringer / Graphql Google Pubsub

Licence: mit
A graphql-subscriptions PubSub Engine using Google PubSub

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Graphql Google Pubsub

Morpheus Graphql
Haskell GraphQL Api, Client and Tools
Stars: ✭ 285 (+163.89%)
Mutual labels:  graphql, graphql-subscriptions
Aws Mobile Appsync Sdk Js
JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Stars: ✭ 806 (+646.3%)
Mutual labels:  graphql, graphql-subscriptions
Altair
✨⚡️ A beautiful feature-rich GraphQL Client for all platforms.
Stars: ✭ 3,827 (+3443.52%)
Mutual labels:  graphql, graphql-subscriptions
Djangochannelsgraphqlws
Django Channels based WebSocket GraphQL server with Graphene-like subscriptions
Stars: ✭ 203 (+87.96%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Subscriptions
📰 A small module that implements GraphQL subscriptions for Node.js
Stars: ✭ 1,390 (+1187.04%)
Mutual labels:  graphql, graphql-subscriptions
36 Graphql Concepts
📜 36 concepts every GraphQL developer should know.
Stars: ✭ 209 (+93.52%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Yoga
🧘 Fully-featured GraphQL Server with focus on easy setup, performance & great developer experience
Stars: ✭ 6,573 (+5986.11%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Mqtt Subscriptions
graphql-subscriptions implementation for MQTT protocol
Stars: ✭ 133 (+23.15%)
Mutual labels:  graphql, graphql-subscriptions
Tutorial Graphql Subscriptions Redis
GraphQL server implementation with Redis backing allowing pubsub
Stars: ✭ 12 (-88.89%)
Mutual labels:  graphql, graphql-subscriptions
Kikstart Graphql Client
🚀 Small NodeJS Wrapper around apollo-client that provides easy access to running queries, mutations and subscriptions.
Stars: ✭ 27 (-75%)
Mutual labels:  graphql, graphql-subscriptions
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (+79.63%)
Mutual labels:  graphql, graphql-subscriptions
Angular Fullstack Graphql
🚀 Starter projects for fullstack applications based on Angular & GraphQL.
Stars: ✭ 92 (-14.81%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Kafka Subscriptions
Apollo graphql subscriptions over Kafka protocol
Stars: ✭ 154 (+42.59%)
Mutual labels:  graphql, graphql-subscriptions
Aws Mobile Appsync Sdk Ios
iOS SDK for AWS AppSync.
Stars: ✭ 231 (+113.89%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Genie
Simply pass in your GraphQL type defintions and get a fully featured GraphQL API with referential integrity, inverse updates, subscriptions and role based access control that can be used client side or server side.
Stars: ✭ 147 (+36.11%)
Mutual labels:  graphql, graphql-subscriptions
Aws Lambda Graphql
Use AWS Lambda + AWS API Gateway v2 for GraphQL subscriptions over WebSocket and AWS API Gateway v1 for HTTP
Stars: ✭ 313 (+189.81%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Postgres Subscriptions
A graphql subscriptions implementation using postgres and apollo's graphql-subscriptions
Stars: ✭ 133 (+23.15%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Redis Subscriptions
A graphql subscriptions implementation using redis and apollo's graphql-subscriptions
Stars: ✭ 829 (+667.59%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Rxjs
fork of Graphql which adds Observable support
Stars: ✭ 78 (-27.78%)
Mutual labels:  graphql, graphql-subscriptions
React Fullstack Graphql
Starter projects for fullstack applications based on React & GraphQL.
Stars: ✭ 1,352 (+1151.85%)
Mutual labels:  graphql, graphql-subscriptions

graphql-google-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 Google PubSub mechanism to support multiple subscription manager instances.

Installation

npm install @axelspringer/graphql-google-pubsub or yarn add @axelspringer/graphql-google-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 GooglePubSub instance:

import { GooglePubSub } from '@axelspringer/graphql-google-pubsub';
const pubsub = new GooglePubSub();

Now, implement your Subscriptions type resolver, using the pubsub.asyncIterator to map the event you need:

const SOMETHING_CHANGED_TOPIC = 'something_changed';

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),
    },
  },
}

Subscriptions resolvers are not a function, but an object with subscribe method, that returns AsyncIterable.

Calling the method asyncIterator of the GooglePubSub instance will subscribe to the topic provided and will return an AsyncIterator binded to the GooglePubSub 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 GooglePubSub 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.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: { id: "123" }});

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

The received message from Google PubSub gets directly passed as payload to the resolve/filter function.

You might extract the data (Buffer) in there or use a common message handler to transform the received message.

function commonMessageHandler ({attributes = {}, data = ''}) {
  return {
    ...attributes,
    text: data.toString()
  };
}

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:

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: (_, args) => pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`),
    },
  },
}

Using both arguments and payload to filter events

import { withFilter } from 'graphql-subscriptions';

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: withFilter(
        (_, args) => pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`),
        (payload, variables) => payload.somethingChanged.id === variables.relevantId,
      ),
    },
  },
}

Creating the Google PubSub Client

import { GooglePubSub } from '@axelspringer/graphql-google-pubsub';

const pubSub = new GooglePubSub(options, topic2SubName, commonMessageHandler)

Options

These are the options which are passed to the internal or passed 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: '[email protected]',
    private_key: '-BEGIN PRIVATE KEY-\nsample\n-END PRIVATE KEY-\n'
  }
};

Subscription Options

Subscription options can be passed into subscribe or asyncInterator.

Note: google.protobuf.Duration types must be passed in as an object with a seconds property ({ seconds: 123 }).

const dayInSeconds = 60 * 60 * 24;

const subscriptionOptions = {
  messageRetentionDuration: { seconds: dayInSeconds },
  expirationPolicy: {
    ttl: { seconds: dayInSeconds * 2 }, // 2 Days
  },
};

await pubsub.asyncIterator("abc123", subscriptionOptions);

topic2SubName

Allows building different workflows. If you listen on multiple server instances to the same subscription, the messages will get distributed between them. Most of the time you want different subscriptions per server. That way every server instance can inform their clients about a new message.

const topic2SubName = topicName => `${topicName}-${serverName}-subscription`

commonMessageHandler

The common message handler gets called with the received message from Google 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 = () => new DataLoader(...);
const commonMessageHandler = ({attributes: {id}, data}) => ({id, dataLoader: getDataLoader()});
export const resolvers = {
  Subscription: {
    somethingChanged: {
      resolve: ({id, dataLoader}) => dataLoader.load(id)
    },
  },
}

Author

Jonas Hackenberg - jonas-arkulpa

Acknowledgements

This project is mostly inspired by graphql-redis-subscriptions. Many thanks to its authors for their work and inspiration. Thanks to the Lean Team (Daniel Vogel, Martin Thomas, Marcel Dohnal, Florian Tatzky, Sebastian Herrlinger, Mircea Craculeac and Tim Susa).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].