DDP-Apollo
DDP-Apollo has been created to leverage the power of DDP for GraphQL queries and subscriptions. For Meteor developers there is no real need for an HTTP server or extra websocket connection, because DDP offers all we need and has been well tested over time.
- DDP-Apollo is one of the easiest ways to get GraphQL running for Meteor developers
- Works with the Meteor accounts packages out of the box, giving a userId in your resolvers
- Doesn’t require an HTTP server to be setup, like with express, koa or hapi
- Supports GraphQL Subscriptions out-of-the-box
- Doesn’t require an extra websocket for GraphQL Subscriptions, because DDP already has a websocket
- Already have a server setup? Use
DDPSubscriptionLink
stand-alone for just Subscriptions support. Read more
Because it's "just another Apollo Link":
- It works with Apollo Dev Tools
- It's easy to combine with other Apollo Links
- It's front-end agnostic
Checkout this starter kit to see Meteor, Apollo, DDP and React all work together.
Note: DDP-Apollo works with all front-ends, not just React
Contents
Installation
meteor add swydo:ddp-apollo
meteor npm install --save graphql apollo-link apollo-client apollo-cache-inmemory
Client setup
This package gives you a DDPLink
for your Apollo Client.
1import ApolloClient from 'apollo-client'; 2import { DDPLink } from 'meteor/swydo:ddp-apollo'; 3// Choose any cache implementation, but we'll use InMemoryCache as an example 4import { InMemoryCache } from 'apollo-cache-inmemory'; 5 6export const client = new ApolloClient ({ 7 link: new DDPLink(), 8 cache: new InMemoryCache() 9});
Options
connection
: The DDP connection to use. DefaultMeteor.connection
.method
: The name of the method. Default__graphql
.publication
: The name of the publication. Default__graphql-subscriptions
.
1// Pass options to the DDPLink constructor 2new DDPLink({ 3 connection: Meteor.connection 4});
Server setup
The server will add a method that will be used by the DDP network interface.
1import { schema } from './path/to/your/executable/schema'; 2import { setup } from 'meteor/swydo:ddp-apollo'; 3 4setup({ 5 schema, 6 ...otherOptions 7});
Options
schema
: The GraphQL schema. Defaultundefined
. Required.context
: A customer context. Either an object or a function returning an object. Optional.method
: The name of the method. Default__graphql
.publication
: The name of the publication. Default__graphql-subscriptions
.
Custom context
To modify or overrule the default context, you can pass a context
object or function to the setup:
1// As an object: 2const context = { 3 foo: 'bar' 4} 5 6// As a function, returning an object: 7const context = (currentContext) => ({ ...currentContext, foo: 'bar' }); 8 9setup({ 10 schema, 11 context, 12});
GraphQL subscriptions
Subscription support is baked into this package. Simply add the subscriptions to your schema and resolvers and everything works.
// schema.graphql type Query { name: String } type Subscription { message: String } schema { query: Query subscription: Subscription }
Setting up PubSub
meteor npm install --save graphql-subscriptions
1import { PubSub } from 'graphql-subscriptions'; 2 3// The pubsub mechanism of your choice, for instance: 4// - PubSub from graphql-subscriptions (not recommended for production) 5// - RedisPubSub from graphql-redis-subscriptions 6// - MQTTPubSub from graphql-mqtt-subscriptions 7const pubsub = new PubSub(); 8 9export const resolvers = { 10 Query: { 11 name: () => 'bar', 12 }, 13 Subscription: { 14 message: { 15 subscribe: () => pubsub.asyncIterator('SOMETHING_CHANGED'), 16 }, 17 }, 18}; 19 20// Later you can publish updates like this: 21pubsub.publish('SOMETHING_CHANGED', { message: 'hello world' });
See graphql-subscriptions package for more setup details and other pubsub mechanisms.
Using DDP only for subscriptions
If you already have an HTTP server setup and you are looking to support GraphQL Subscriptions in your Meteor application, you can use the DDPSubscriptionLink
stand-alone.
1import { ApolloClient } from 'apollo-client'; 2import { split } from "apollo-link"; 3import { HttpLink } from "apollo-link-http"; 4import { DDPSubscriptionLink, isSubscription } from 'meteor/swydo:ddp-apollo'; 5import { InMemoryCache } from 'apollo-cache-inmemory'; 6 7const httpLink = new HttpLink({ uri: "/graphql" }); 8const subscriptionLink = new DDPSubscriptionLink(); 9 10const link = split( 11 isSubscription, 12 subscriptionLink, 13 httpLink, 14); 15 16export const client = new ApolloClient ({ 17 link, 18 cache: new InMemoryCache() 19});
Rate limiting GraphQL calls
Meteor supports rate limiting for DDP calls. This means you can rate limit DDP-Apollo as well!
meteor add ddp-rate-limiter
1import { DDPRateLimiter } from 'meteor/ddp-rate-limiter'; 2 3// Define a rule that matches graphql method calls. 4const graphQLMethodCalls = { 5 type: 'method', 6 name: '__graphql' 7}; 8 9// Add the rule, allowing up to 5 messages every 1000 milliseconds. 10DDPRateLimiter.addRule(graphQLMethodCalls, 5, 1000);
See DDP Rate Limit documentation.
Sponsor
Want to work with Meteor and GraphQL? Join the team!