Git Product home page Git Product logo

ddp-apollo's Introduction

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
  • 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

Build Status Greenkeeper badge

Contents

Installation

meteor add swydo:ddp-apollo
meteor npm install --save graphql apollo-link

Client setup

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()
});

Options

  • connection: The DDP connection to use. Default Meteor.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
});

Server setup

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
});

Options

  • schema: The GraphQL schema. Default undefined. Required.
  • method: The name of the method. Default __graphql.
  • publication: The name of the publication. Default __graphql-subscriptions.
  • disableOptics: Disable Apollo Optics monitoring. Default undefined. See Apollo Optics.

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
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.

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.

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()
});

Apollo Optics

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.

Sponsor

Swydo

Want to work with Meteor and GraphQL? Join the team!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.