Integrating with Meteor

Specifics about using Apollo in your Meteor application.

Note: These docs are for using Meteor with the 1.0 version of Apollo Client. The 2.0 upgrade is still in progress and these docs will be updated when it is ready

The Apollo client and server tools are published on npm, which makes them available to all JavaScript applications, including those written with Meteor 1.3 and above. When using Meteor with Apollo, you can use those npm packages directly, or you can use the apollo Atmosphere package, which simplifies things for you.

To install apollo, run these commands:

meteor add apollo
meteor npm install --save apollo-client graphql-server-express express graphql graphql-tools body-parser



You can see this package in action in the Apollo Meteor starter kit.


Connect to the Apollo server with meteorClientConfig:

import ApolloClient from 'apollo-client';
import { meteorClientConfig } from 'meteor/apollo';

const client = new ApolloClient(meteorClientConfig());


Create the following files:

/imports/api/schema.js         # a JavaScript file with the schema
/imports/api/resolvers.js      # a JavaScript file with the Apollo resolvers

Define a simple schema under schema.js.

export const typeDefs = `
type Query {
  say: String

Define your first resolver under resolvers.js.

export const resolvers = {
  Query: {
    say(root, args, context) {
      return 'hello world';

Set up the Apollo server with createApolloServer:

import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';

import { typeDefs } from '/imports/api/schema';
import { resolvers } from '/imports/api/resolvers';

const schema = makeExecutableSchema({


The GraphiQL url by default is http://localhost:3000/graphiql. You can now test your first query:


Inside your resolvers, if the user is logged in, their id will be context.userId and their user doc will be context.user:

export const resolvers = {
  Query: {
    user(root, args, context) {
      // Only return the current user, for security
      if (context.userId === {
        return context.user;
  User: ...

Query batching

meteor/apollo gives you a BatchedNetworkInterface by default thanks to createMeteorNetworkInterface. This interface is meant to reduce significantly the number of requests sent to the server.

In order to get the most out of it, you can attach a dataloader to every request to batch loading your queries (and cache them!).

Here are some great resources to help you integrating query batching in your Meteor application:


It is strongly recommended to explictly specify the ROOT_URL environment variable of your deployment. The configuration of the Apollo client and GraphQL server provided by this package depends on a configured ROOT_URL. Read more about that in the Meteor Guide.


Your Meteor apps may rely on static typings with TypeScript. If so, it is recommended to use the ambient TypeScript definition for this Atmosphere package.



meteorClientConfig(customClientConfig = {})

The customClientConfig is an optional object that can have any Apollo Client options.

Defining a customClientConfig object extends or replaces fields of the default configuration provided by the package.

The default configuration of the client is:

  • networkInterface: createMeteorNetworkInterface(), a pre-configured network interface. See below for more information.
  • ssrMode: Meteor.isServer, enable server-side rendering mode by default if used server-side.

The store is normalized by default with __typename + _id identifiers. See store normalization section for more information.


createMeteorNetworkInterface(customNetworkInterface = {})

customNetworkInterface is an optional object that replaces fields of the default configuration:

  • uri: Meteor.absoluteUrl('graphql'), points to the default GraphQL server endpoint, such as http://localhost:3000/graphql or
  • opts: {}, additional FetchOptions passed to the NetworkInterface.
  • useMeteorAccounts: true, enable the Meteor User Accounts middleware to identify the user with every request thanks to her login token.
  • batchingInterface: true, use a BatchedNetworkInterface by default instead of NetworkInterface.
  • batchInterval: 10, if the batchingInterface field is true, this field defines the batch interval to determine how long the network interface batches up queries before sending them to the server.

Additionally, if the useMeteorAccounts is set to true, you can add to your customNetworkInterface a loginToken field while doing server-side rendering to handle the current user.

createMeteorNetworkInterface example:

import ApolloClient from 'apollo-client'
import { createMeteorNetworkInterface, meteorClientConfig } from 'meteor/apollo';

const networkInterface = createMeteorNetworkInterface({
  // use a batched network interface instead of a classic network interface
  batchingInterface: true,

const client = new ApolloClient(meteorClientConfig({ networkInterface }));


createApolloServer(customOptions = {}, customConfig = {})

createApolloServer is used to create and configure an Express GraphQL server.

customOptions is an object that can have any GraphQL Server options, used to enhance the GraphQL server run thanks to graphqlExpress. Defining a customOptions object extends or replaces fields of the default configuration provided by the package:

  • context: {} is an object or a function returning an object that extends the context object being passed down to the resolvers.
  • formatError: a function used to format errors before returning them to clients.
  • debug: Meteor.isDevelopment, additional debug logging if execution errors occur in dev mode.

This is the object that should have a schema entry created by makeExecutableSchema.

customConfig is an optional object that can be used to replace the configuration of how the Express server itself runs:

  • path: path of the GraphQL server. This is the endpoint where the queries & mutations are sent. Default: /graphql.
  • configServer: a function that is given to the express server for further configuration. You can for instance enable CORS with createApolloServer({}, {configServer: expressServer => expressServer.use(cors())})
  • graphiql: whether to enable GraphiQL. Default: true in development and false in production.
  • graphiqlPath: path for GraphiQL. Default: /graphiql (note the _i_).
  • graphiqlOptions: GraphiQL options Default: attempts to use Meteor.loginToken from localStorage to log you in.

It will use the same port as your Meteor server. Don’t put a route or static asset at the same path as the GraphQL route or the GraphiQL route if in use (again, defaults are /graphql and /graphiql respectively).


You may still use the authentication based on DDP (Meteor’s default data layer) and apollo will send the current user’s login token to the GraphQL server with each request.

If you want to use only GraphQL in your app you can use nicolaslopezj:apollo-accounts. This package uses the Meteor Accounts methods in GraphQL, it’s compatible with the accounts you have saved in your database and you may use nicolaslopezj:apollo-accounts and Meteor’s DDP accounts at the same time.

If you are relying on the current user in your queries, you’ll want to clear the store when the current user state changes. To do so, use client.resetStore() in the Meteor.logout callback:

// The `client` variable refers to your `ApolloClient` instance.
// It would be imported in your template,
// or passed via props thanks to `withApollo` in React for example.

Meteor.logout(function() {
  return client.resetStore(); // make all active queries re-run when the log-out process completed


There are two additional configurations that you need to keep in mind when using React Server Side Rendering with Meteor.

  1. Use isomorphic-fetch to polyfill fetch server-side (used by Apollo Client’s network interface).
  2. Connect your express server to Meteor’s existing server with WebApp.connectHandlers.use
  3. Do not end the connection with res.send() and res.end() use req.dynamicBody and req.dynamicHead instead and call next(). more info

The idea is that you need to let Meteor to finally render the html you can just provide it extra body and or head for the html and Meteor will append it, otherwise CSS/JS and or other merged html content that Meteor serve by default (including your application main .js file) will be missing.

Here is a full working example:

meteor add apollo webapp
meteor npm install --save react react-dom apollo-client redux react-apollo react-router react-helmet express isomorphic-fetch

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { meteorClientConfig, createMeteorNetworkInterface } from 'meteor/apollo';
import React from 'react';
import ReactDOM from 'react-dom/server';
import ApolloClient from 'apollo-client';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { ApolloProvider, renderToStringWithData } from 'react-apollo';
import { match, RouterContext } from 'react-router';
import Express from 'express';
// #1 import isomorphic-fetch so the network interface can be created
import 'isomorphic-fetch';
import Helmet from 'react-helmet';

import routes from '../both/routes';
import rootReducer from '../../ui/reducers';
import Body from '../both/routes/body';

// 1# do not use new
const app = Express(); // eslint-disable-line new-cap

app.use((req, res, next) => {
  match({ routes, location: req.originalUrl }, (error, redirectLocation, renderProps) => {
    if (redirectLocation) {
      res.redirect(redirectLocation.pathname +;
    } else if (error) {
      console.error('ROUTER ERROR:', error); // eslint-disable-line no-console
    } else if (renderProps) {
      // use createMeteorNetworkInterface to get a preconfigured network interface
      // #1 network interface can be used server-side thanks to polyfilled `fetch`
      const networkInterface = createMeteorNetworkInterface({
        opts: {
          credentials: 'same-origin',
          headers: req.headers,
        // possible current user login token stored in the cookies thanks to
        // a third-party package like meteorhacks:fast-render
        loginToken: req.cookies['meteor-login-token'],

      // use meteorClientConfig to get a preconfigured Apollo Client options object
      const client = new ApolloClient(meteorClientConfig({ networkInterface }));

      const store = createStore(
          apollo: client.reducer(),
        {}, // initial state

      const component = (
        <ApolloProvider store={store} client={client}>
          <RouterContext {...renderProps} />

      renderToStringWithData(component).then((content) => {
        const initialState =[client.reduxRootKey].data;
        // the body content we want to append
        const body = <Body content={content} state={initialState} />;
        // #3 `req.dynamicBody` will hold that body and meteor will take care of
        // actually appending it to the end result
        req.dynamicBody = ReactDOM.renderToStaticMarkup(body);
        const head = Helmet.rewind();
        // #3 `req.dynamicHead` in this case we use `react-helmet` to add seo tags
        req.dynamicHead = `  ${head.title.toString()}
        // #3 Important we do not want to return this, we just let meteor handle it
    } else {
      console.log('not found'); // eslint-disable-line no-console
// #2 connect your express server with meteor's

Apollo Engine

Here’s a minimal example of Apollo Engine integration:

import { createApolloServer } from 'meteor/apollo';
import { Engine } from 'apollo-engine';

import executableSchema from 'schema.js';

const PORT = process.env.PORT || 3000;

// Initialize Apollo Engine
const engine = new Engine({
  engineConfig: {
    apiKey: 'your_apollo_engine_api_key',
    logging: {
      level: 'DEBUG', // DEBUG, INFO, WARN or ERROR
  graphqlPort: PORT,
  endpoint: '/graphql',

createApolloServer(req => ({
  schema: executableSchema,
  context: {},
  tracing: true,
  cacheControl: true,
}), {
  configServer: (graphQLServer) => {
    // Any other config server stuff

// Start apollo engine

Importing .graphql files

An easy way to work with GraphQL is by importing .graphql files directly using the import syntax.

meteor add swydo:graphql

Instead of the /imports/api/schema.js file, create a /imports/api/schema.graphql file with the same content as before:

type Query {
  say: String

One of the benefits you’ll get right away is good highlighting by GitHub and your IDE!

Now we can import the schema:

import typeDefs from '/imports/api/schema.graphql';

Use typeDefs as before in the above examples. You can pass it directly to makeExecutableSchema like before.

The import syntax will also work for any other .graphql file besides your main schema. So you’ll be able to import query, mutation and subscription files without needing to manually parse them with the graphql-tag.

For more benefits, see the GrahpQL build plugin README.


If you are looking to integrate Apollo with Blaze, you can use the swydo:blaze-apollo package:

import { setup } from 'meteor/swydo:blaze-apollo';

const client = new ApolloClient(meteorClientConfig());

setup({ client });

This gives you reactive GraphQL queries in your templates!

  hello() {
    return Template.instance().gqlQuery({
      query: HELLO_QUERY


You can also use GraphQL subscriptions with your Meteor app if you need to. The following code gives an example of a complete configuration that enables all the features of subscriptions in addition to base GraphQL.


import { ApolloClient } from 'apollo-client';
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws';
import { getMeteorLoginToken, createMeteorNetworkInterface } from 'meteor/apollo';

// "basic" Meteor network interface
const networkInterface = createMeteorNetworkInterface();

// create a websocket uri based on your app absolute url (ROOT_URL), ex: ws://localhost:3000
const websocketUri = Meteor.absoluteUrl('subscriptions').replace(/^http/, 'ws');

// create a websocket client
const wsClient = new SubscriptionClient(websocketUri, {
  reconnect: true,
  // pass some extra information to the subscription, like the current user:
  connectionParams: {
    // getMeteorLoginToken = get the Meteor current user login token from local storage
    meteorLoginToken: getMeteorLoginToken(),

// enhance the interface with graphql subscriptions
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(networkInterface, wsClient);

// enjoy graphql subscriptions with Apollo Client
const client = new ApolloClient({ networkInterface: networkInterfaceWithSubscriptions });


The same context is used for both the resolvers and the GraphQL subscriptions. This also means that authentication in the websocket transport is configured out-of-the-box.

Note that PubSub from graphql-subscriptions is not suitable for production. You should wire your SubscriptionManager with Redis subscriptions or MQTT subscriptions in case you want to use them in production apps.

import { SubscriptionManager } from 'graphql-subscriptions';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { createApolloServer, addCurrentUserToContext } from 'meteor/apollo';

// your executable schema
const schema = ...

// any additional context you use for your resolvers, if any
const context = {};

// the pubsub mechanism of your choice, for instance:
// - PubSub from graphql-subscriptions (not recommended for production)
// - RedisPubSub from graphql-redis-subscriptions
// - MQTTPubSub from graphql-mqtt-subscriptions
const pubsub = new PubSub();

// subscriptions path which fits witht the one you connect to on the client
const subscriptionsPath = '/subscriptions';

// start a graphql server with Express handling a possible Meteor current user

// create the subscription manager thanks to the schema & the pubsub mechanism
const subscriptionManager = new SubscriptionManager({

// start up a subscription server
new SubscriptionServer(
    // on connect subscription lifecycle event
    onConnect: async (connectionParams, webSocket) => {
      // if a meteor login token is passed to the connection params from the client,
      // add the current user to the subscription context
      const subscriptionContext = connectionParams.meteorLoginToken
        ? await addCurrentUserToContext(context, connectionParams.meteorLoginToken)
        : context;

      return subscriptionContext;
    // bind the subscription server to Meteor WebApp
    server: WebApp.httpServer,
    path: subscriptionsPath,
Edit on GitHub
// search box