{"name":"graphql-subscriptions","version":"0.2.2","description":"GraphQL subscriptions for node.js","main":"dist/index.js","repository":{"type":"git","url":"git+https://github.com/apollostack/graphql-subscriptions.git"},"dependencies":{"es6-promise":"^3.2.1"},"peerDependencies":{"graphql":"^0.7.0 || ^0.8.0"},"scripts":{"compile":"tsc","typings":"typings install","pretest":"npm run compile","test":"npm run testonly --","posttest":"npm run lint","lint":"tslint ./src/**/*.ts","watch":"tsc -w","testonly":"mocha --reporter spec --full-trace ./dist/test/tests.js","coverage":"node ./node_modules/istanbul/lib/cli.js cover _mocha -- --full-trace ./dist/test/tests.js","postcoverage":"remap-istanbul --input coverage/coverage.raw.json --type lcovonly --output coverage/lcov.info"},"devDependencies":{"chai":"^3.5.0","chai-as-promised":"^5.3.0","graphql":"^0.8.2","istanbul":"^1.0.0-alpha.2","mocha":"^3.0.0","remap-istanbul":"^0.6.4","sinon":"^1.17.6","sinon-chai":"^2.8.0","tslint":"^3.13.0","typescript":"^2.0.0","typings":"^1.3.2"},"typings":"dist/index.d.ts","typescript":{"definition":"dist/index.d.ts"},"contributors":[{"name":"Authors"},{"name":"Jonas Helfer","email":"helfer@users.noreply.github.com"},{"name":"Jonas Helfer","email":"jonas@helfer.email"},{"name":"Quint Stoffers","email":"quintstoffers@users.noreply.github.com"},{"name":"Sashko Stubailo","email":"s.stubailo@gmail.com"},{"name":"Sashko Stubailo","email":"sashko@stubailo.com"},{"name":"David Yahalomi","email":"davidyaha@users.noreply.github.com"},{"name":"Alexander Anich","email":"Anichale@users.noreply.github.com"},{"name":"Francois Valdy","email":"gluck@users.noreply.github.com"}],"gitHead":"748d4e1f385fc2d7ca628852696a459310a1f041","bugs":{"url":"https://github.com/apollostack/graphql-subscriptions/issues"},"homepage":"https://github.com/apollostack/graphql-subscriptions#readme","_id":"graphql-subscriptions@0.2.2","_shasum":"28c8e19d24b31abc6e97f4c9719ea99f890f6192","_from":".","_npmVersion":"3.6.0","_nodeVersion":"5.6.0","_npmUser":{"name":"helfer","email":"helfer@meteor.com"},"dist":{"shasum":"28c8e19d24b31abc6e97f4c9719ea99f890f6192","tarball":"http://registry-npm.ywwl.com/graphql-subscriptions/download/graphql-subscriptions-0.2.2.tgz","integrity":"sha512-zYTDcV+MHv2EaeWoqsoYEB69JnaxBezV3/Bxk2/BQB0u4Gnu+TAVvUSENWuRzdrV5MsUPUgRgutV4+5B9vFEeg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIF88oO0gNatZsqRLz9GCF2a+I1LYbEq9y3ZbkG4QUfFCAiEAi1SPfgsE+1DSQcmpUOfmhVQqaMVtEFnl9/tCconeUlQ="}],"size":30271,"noattachment":false,"key":"/graphql-subscriptions/-/graphql-subscriptions-0.2.2.tgz"},"maintainers":[{"name":"helfer","email":"helfer@meteor.com"},{"name":"sashko","email":"sashko@stubailo.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/graphql-subscriptions-0.2.2.tgz_1480439697779_0.43239351408556104"},"directories":{},"readme":"[![npm version](https://badge.fury.io/js/graphql-subscriptions.svg)](https://badge.fury.io/js/graphql-subscriptions) [![GitHub license](https://img.shields.io/github/license/apollostack/graphql-subscriptions.svg)](https://github.com/apollographql/graphql-subscriptions/blob/master/LICENSE)\n\n# graphql-subscriptions\n\nGraphQL subscriptions is a simple npm package that lets you wire up GraphQL with a pubsub system (like Redis) to implement subscriptions in GraphQL.\n\nYou can use it with any GraphQL client and server (not only Apollo).\n\n### Installation\n\n`npm install graphql-subscriptions graphql` or `yarn add graphql-subscriptions graphql`\n\n> This package should be used with a network transport, for example [subscriptions-transport-ws](https://github.com/apollographql/subscriptions-transport-ws).\n\n### TypeScript\n\nIf you are developing a project that uses this module with TypeScript:\n\n* ensure that your `tsconfig.json` `lib` definition includes `\"esnext.asynciterable\"`\n* `npm install @types/graphql` or `yarn add @types/graphql`\n\n### Getting started with your first subscription\n\nTo begin with GraphQL subscriptions, start by defining a GraphQL `Subscription` type in your schema:\n\n```graphql\ntype Subscription {\n    somethingChanged: Result\n}\n\ntype Result {\n    id: String\n}\n```\n\nNext, add the `Subscription` type to your `schema` definition:\n\n```graphql\nschema {\n  query: Query\n  mutation: Mutation\n  subscription: Subscription\n}\n```\n\nNow, let's create a simple `PubSub` instance - it is a simple pubsub implementation, based on `EventEmitter`. Alternative `EventEmitter` implementations can be passed by an options object\nto the `PubSub` constructor.\n\n```js\nimport { PubSub } from 'graphql-subscriptions';\n\nexport const pubsub = new PubSub();\n```\n\nNow, implement your Subscriptions type resolver, using the `pubsub.asyncIterator` to map the event you need:\n\n```js\nconst SOMETHING_CHANGED_TOPIC = 'something_changed';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),\n    },\n  },\n}\n```\n\n> Subscriptions resolvers are not a function, but an object with `subscribe` method, that returns `AsyncIterable`.\n\nNow, the GraphQL engine knows that `somethingChanged` is a subscription, and every time we use `pubsub.publish` over this topic - it will publish it using the transport we use:\n\n```js\npubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: { id: \"123\" }});\n```\n\n> Note that the default PubSub implementation is intended for demo purposes. It only works if you have a single instance of your server and doesn't scale beyond a couple of connections.\n> For production usage you'll want to use one of the [PubSub implementations](#pubsub-implementations) backed by an external store. (e.g. Redis)\n\n### Filters\n\nWhen publishing data to subscribers, we need to make sure that each subscriber gets only the data it needs.\n\nTo do so, we can use `withFilter` helper from this package, which wraps `AsyncIterator` with a filter function, and lets you control each publication for each user.\n\n`withFilter` API:\n- `asyncIteratorFn: (rootValue, args, context, info) => AsyncIterator<any>` : A function that returns `AsyncIterator` you got from your `pubsub.asyncIterator`.\n- `filterFn: (payload, variables, context, info) => boolean | Promise<boolean>` - A filter function, executed with the payload (the published value), variables, context and operation info, must return `boolean` or `Promise<boolean>` indicating if the payload should pass to the subscriber.\n\nFor example, if `somethingChanged` would also accept a variable with the ID that is relevant, we can use the following code to filter according to it:\n\n```js\nimport { withFilter } from 'graphql-subscriptions';\n\nconst SOMETHING_CHANGED_TOPIC = 'something_changed';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: withFilter(() => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC), (payload, variables) => {\n        return payload.somethingChanged.id === variables.relevantId;\n      }),\n    },\n  },\n}\n```\n\n> Note that when using `withFilter`, you don't need to wrap your return value with a function.\n\n### Channels Mapping\n\nYou can map multiple channels into the same subscription, for example when there are multiple events that trigger the same subscription in the GraphQL engine.\n\n```js\nconst SOMETHING_UPDATED = 'something_updated';\nconst SOMETHING_CREATED = 'something_created';\nconst SOMETHING_REMOVED = 'something_removed';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: () => pubsub.asyncIterator([ SOMETHING_UPDATED, SOMETHING_CREATED, SOMETHING_REMOVED ]),\n    },\n  },\n}\n````\n\n### Payload Manipulation\n\nYou can also manipulate the published payload, by adding `resolve` methods to your subscription:\n\n```js\nconst SOMETHING_UPDATED = 'something_updated';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      resolve: (payload, args, context, info) => {\n        // Manipulate and return the new value\n        return payload.somethingChanged;\n      },\n      subscribe: () => pubsub.asyncIterator(SOMETHING_UPDATED),\n    },\n  },\n}\n````\n\n### Usage with callback listeners\n\nYour database might have callback-based listeners for changes, for example something like this:\n\n```JS\nconst listenToNewMessages = (callback) => {\n  return db.table('messages').listen(newMessage => callback(newMessage));\n}\n\n// Kick off the listener\nlistenToNewMessages(message => {\n  console.log(message);\n})\n```\n\nThe `callback` function would be called every time a new message is saved in the database. Unfortunately, that doesn't play very well with async iterators out of the box because callbacks are push-based, where async iterators are pull-based.\n\nWe recommend using the [`callback-to-async-iterator`](https://github.com/withspectrum/callback-to-async-iterator) module to convert your callback-based listener into an async iterator:\n\n```js\nimport asyncify from 'callback-to-async-iterator';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: () => asyncify(listenToNewMessages),\n    },\n  },\n}\n````\n\n### Custom `AsyncIterator` Wrappers\n\nThe value you should return from your `subscribe` resolver must be an `AsyncIterator`.\n\nYou can use this value and wrap it with another `AsyncIterator` to implement custom logic over your subscriptions.\n\nFor example, the following implementation manipulate the payload by adding some static fields:\n\n```typescript\nimport { $$asyncIterator } from 'iterall';\n\nexport const withStaticFields = (asyncIterator: AsyncIterator<any>, staticFields: Object): Function => {\n  return (rootValue: any, args: any, context: any, info: any): AsyncIterator<any> => {\n\n    return {\n      next() {\n        return asyncIterator.next().then(({ value, done }) => {\n          return {\n            value: {\n              ...value,\n              ...staticFields,\n            },\n            done,\n          };\n        });\n      },\n      return() {\n        return Promise.resolve({ value: undefined, done: true });\n      },\n      throw(error) {\n        return Promise.reject(error);\n      },\n      [$$asyncIterator]() {\n        return this;\n      },\n    };\n  };\n};\n```\n\n> You can also take a look at `withFilter` for inspiration.\n\nFor more information about `AsyncIterator`:\n- [TC39 Proposal](https://github.com/tc39/proposal-async-iteration)\n- [iterall](https://github.com/leebyron/iterall)\n- [IxJS](https://github.com/ReactiveX/IxJS)\n\n### PubSub Implementations\n\nIt can be easily replaced with some other implementations of [PubSubEngine abstract class](https://github.com/apollographql/graphql-subscriptions/blob/master/src/pubsub-engine.ts). Here are a few of them:\n- Use Redis with https://github.com/davidyaha/graphql-redis-subscriptions\n- Use Google PubSub with https://github.com/axelspringer/graphql-google-pubsub\n- Use MQTT enabled broker with https://github.com/davidyaha/graphql-mqtt-subscriptions\n- Use RabbitMQ with https://github.com/cdmbase/graphql-rabbitmq-subscriptions\n- Use AMQP (RabbitMQ) with https://github.com/Surnet/graphql-amqp-subscriptions\n- Use Kafka with https://github.com/ancashoria/graphql-kafka-subscriptions\n- Use Postgres with https://github.com/GraphQLCollege/graphql-postgres-subscriptions\n- Use NATS with https://github.com/moonwalker/graphql-nats-subscriptions\n- Use multiple backends with https://github.com/jcoreio/graphql-multiplex-subscriptions\n- [Add your implementation...](https://github.com/apollographql/graphql-subscriptions/pull/new/master)\n\nYou can also implement a `PubSub` of your own, by using the exported abstract class `PubSubEngine` from this package. By using `extends PubSubEngine` you use the default `asyncIterator` method implementation; by using `implements PubSubEngine` you must implement your own `AsyncIterator`.\n\n#### SubscriptionManager **@deprecated**\n\n`SubscriptionManager` is the previous alternative for using `graphql-js` subscriptions directly, and it's now deprecated.\n\nIf you are looking for its API docs, refer to [a previous commit of the repository](https://github.com/apollographql/graphql-subscriptions/blob/5eaee92cd50060b3f3637f00c53960f51a07d0b2/README.md)\n","publish_time":1480439698453,"_cnpm_publish_time":1480439698453,"dist-tags":{"latest":"3.0.0"}}