项目作者: CodeCommission

项目描述 :
A Server-Side-Events (SSE) client + server for GraphQL subscriptions
高级语言: JavaScript
项目地址: git://github.com/CodeCommission/subscriptions-transport-sse.git
创建时间: 2017-04-26T12:14:34Z
项目社区:https://github.com/CodeCommission/subscriptions-transport-sse

开源协议:MIT License

下载


subscriptions-transport-sse

A GraphQL Server-Side-Events (SSE) server and client to facilitate GraphQL subscriptions.

That’s an API compatible SSE transport implementation of subscriptions-transport-ws.

Example

Getting Started

  1. Using Yarn:
  2. $ yarn add subscriptions-transport-sse
  3. Or, using NPM:
  4. $ npm install --save subscriptions-transport-sse

ExpressJS Server

Starting with the server, create a new simple SubscriptionsManager, with a PubSub implementation:

  1. import { SubscriptionManager, PubSub } from 'graphql-subscriptions'
  2. const schema = {} // Replace with your GraphQL schema object
  3. const pubsub = new PubSub()
  4. const subscriptionManager = new SubscriptionManager({
  5. schema,
  6. pubsub
  7. })

Now, use your subscriptionManager, and create your SubscriptionServer:

  1. const express = require('express')
  2. const app = express()
  3. const expressGraphQLSubscriptionsSSETransport = require('subscriptions-transport-sse')
  4. expressGraphQLSubscriptionsSSETransport.SubscriptionServer({
  5. onSubscribe: (msg, params) => Object.assign({}, params, { context: { loaders: loaders(), } }),
  6. subscriptionManager: graphqlSubscriptions.subscriptionManager,
  7. }, {
  8. express: app,
  9. path: '/subscriptions',
  10. })
  11. app.listen(SERVICE_PORT, () => console.log(`Listen on ${SERVICE_PORT}`))

Apollo Client (Browser)

For client side, we will use SubscriptionClient, and we also need to extend our network interface to use this transport for GraphQL subscriptions:

  1. import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-sse'
  2. const httpClient = createNetworkInterface({uri: `https://my-graphql.example.com/graphql`})
  3. const sseClient = new SubscriptionClient(`https://my-graphql.example.com/subscriptions`)
  4. const apolloClient = new ApolloClient({networkInterface: addGraphQLSubscriptions(httpClient, sseClient)})

Now, when you want to use subscriptions in client side, use your ApolloClient instance, with subscribe or subscribeToMore (according to your apollo-client usage):

  1. apolloClient.subscribeToMore({
  2. document: gql`
  3. subscription onNewItem {
  4. newItemCreated {
  5. id
  6. }
  7. }`,
  8. variables: {},
  9. updateQuery: (prev, {subscriptionData}) => {
  10. return; // Modify your store and return new state with the new arrived data
  11. }
  12. });

API

TBD, but compatible with subscriptions-transport-ws.

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

Thanks

You like this subscriptions-transport-sse and you want to see what coming next? Follow me on Twitter @mikebild.

Enjoy!