Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@aaronpowell/graphql-cosmosdb-subscriptions

aaronpowell4MIT0.3.0TypeScript support: included

A GraphQL subscription library that uses the CosmosDB change feed as the event source

azure, graphql, websockets

readme

CosmosDB GraphQL Subscriptions npm - CosmosDB package

This package contains support for Apollo GraphQL Subscriptions, using the Azure CosmosDB Change Feed.

Installation

Install via npm or GitHub Packages:

$> npm install --save @aaronpowell/graphql-cosmosdb-subscriptions

Usage

You'll need a SignalR Service account (if you don't have an Azure account sign up for free). Copy the connection string and provide it when you create an instance of SignalRPubSub:

import { CosmosDBPubSub } from "@aaronpowell/graphql-cosmosdb-subscriptions";

const cosmosPubSub = new CosmosDBPubSub(
  new CosmosClient(process.env.COSMOS_CONNECTION_STRING || "")
    .database(process.env.COSMOS_DB || "")
    .container(process.env.COSMOS_CONTAINER || "")
);

Unlike most pubsub libraries, you don't need to publish directly, messages are received when the Change Feed receives messages. When creating the subscription, you subscribe to a CosmosDB partition key value (in the below example type is the partition key and we're subscription when type = 'message').

export const resolvers = {
  Query: {
    async hello(parent, args, { dataSources }) {
      const text = `Message! ${Date.now()}`;
      await dataSources.messages.createOne({
        id: Date.now() + "",
        text,
        type: "message",
      });
      return text;
    },
  },
  Subscription: {
    getMessage: {
      subscribe: () => cosmosPubSub.asyncIterator(["message"]),
    },
  },
};

changelog

Changelog for graphql-azure-subscriptions

0.3.0 - 2021-03-16

@aaronpowell/graphql-signalr-subscriptions

Changed

  • Moved from the @aspnet/signalr to @microsoft/signalr package
  • Changed the disconnection logic to do better subsubscribing of clients

0.2.0 - 2021-02-26

A new package has been added, subscription to CosmosDB change feeds!

@aaronpowell/graphql-signalr-subscriptions

Fixed

  • TypeScript definitions weren't working
  • Missing readme

@aaronpowell/graphql-cosmosdb-subscriptions

Added

  • Initial Release

0.1.0 - 2021-02-17

@aaronpowell/graphql-signalr-subscriptions

Added

  • Initial Release