Skip to content

A set of tools to enable reactivity in Apollo alongside Meteor

License

Notifications You must be signed in to change notification settings

lgandecki/apollo-live-server

 
 

Repository files navigation

Apollo Live Server

This package is mostly for Meteor applications, however you can roll your own integration, as long you respect the interfaces

// make sure you run this before instantiating any collection
// otherwise reactivity may not work for you
import { setup } from 'apollo-live-server';

setup({
  collection: Mongo.Collection,
});

Prerequisites

  • Add GraphQLJSON scalar to your schema.
  • Add ReactiveEvent type from this package
import { ReactiveEventType } from 'apollo-live-server';

Usage

type Query {
  items: [Item],
  item(_id: ID!): Item
}

type Subscription {
  items: ReactiveEvent,
  item(_id: ID!): ReactiveEvent
}
Items.setTypename('Item');

const resolvers = {
  Query: {
    items() {
      return Items.find().fetch()
    }
    item(_, {_id}) {
      return Items.findOne(_id);
    }
  },
  Subscription: {
    items: {
      resolve: payload => payload,
      subscribe() {
        return Items.asyncIterator() // it accepts filters and options as arguments
      }
    },
    items: {
      resolve: payload => payload,
      subscribe(_, {_id}) {
        return Items.asyncIterator({_id})
      }
    }
  }
}

Now when you subscribe to items or item the subscription will send as payload a ReactiveEvent containing:

{
  event: 'added' | 'changed' | 'removed',
  type: 'Item'
  _id: 'XXX',
  doc: { ... }
}

Based on these events you can process and update your query. To learn how to do this please checkout: apollo-live-client package which is suited for React and this ReactiveEvent

Premium Support

Looking to start or develop your new project with GraphQL? Reach out to us now, we can help you along every step: contact@cultofcoders.com. We specialise in building high availability GraphQL APIs and with the help with our awesome frontend developers we can easily consume any GraphQL API.

About

A set of tools to enable reactivity in Apollo alongside Meteor

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%