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
- Easy to combine with other Apollo Links
- Already have a server setup? Use
DDPSubscriptionLink
stand-alone for just Subscriptions support. Read more - Works with Apollo Dev Tools, just like any other Apollo link
meteor add swydo:ddp-apollo
meteor npm install --save graphql apollo-link
This package gives you a DDPLink
for your Apollo Client.
import ApolloClient from 'apollo-client';
import { DDPLink } from 'meteor/swydo:ddp-apollo';
// Apollo requires a cache to be added as well
import { InMemoryCache } from 'apollo-cache-inmemory';
export const client = new ApolloClient ({
link: new DDPLink(),
cache: new InMemoryCache()
});
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
.
// Pass options to the DDPLink constructor
new DDPLink({
connection: Meteor.connection
});
The server will add a method that will be used by the DDP network interface.
import { schema } from './path/to/your/executable/schema';
import { setup } from 'meteor/swydo:ddp-apollo';
setup({
schema,
...otherOptions
});
schema
: The GraphQL schema. Defaultundefined
. Required.method
: The name of the method. Default__graphql
.publication
: The name of the publication. Default__graphql-subscriptions
.disableOptics
: Disable Apollo Optics monitoring. Defaultundefined
. See Apollo Optics.
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
}
meteor npm install --save graphql-subscriptions
import { PubSub } from 'graphql-subscriptions';
const pubsub = new PubSub();
export const resolvers = {
Query: {
name: () => 'bar',
},
Subscription: {
message: {
subscribe: () => pubsub.asyncIterator('SOMETHING_CHANGED'),
},
},
};
// Later you can publish updates like this:
pubsub.publish('SOMETHING_CHANGED', { message: 'hello world' });
See graphql-subscriptions package for more setup details and other pubsub mechanisms.
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.
import { ApolloClient, split } from 'apollo-client';
import { HttpLink } from "apollo-link-http";
import { DDPSubscriptionLink, isSubscription } from 'meteor/swydo:ddp-apollo';
import { InMemoryCache } from 'apollo-cache-inmemory';
const httpLink = new HttpLink({ uri: "/graphql" });
const subscriptionLink = new DDPSubscriptionLink();
const link = split(
isSubscription,
subscriptionLink,
httpLink,
);
export const client = new ApolloClient ({
link,
cache: new InMemoryCache()
});
IMPORTANT: Optics is being replaced by Engine. Optics is still operational, but Engine is it's official successor. At the time of writing Engine only works as an HTTP middleware, meaning it has no support for GraphQL Subscriptions or DDP-Apollo.
To use Optics, you must first instrument the schema before passing it to the setup function:
import OpticsAgent from 'optics-agent';
OpticsAgent.instrumentSchema(schema);
That's it! Now ddp-apollo
will take care of the rest.
See the Optics README for all setup details and options.
Want to work with Meteor and GraphQL? Join the team!