npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-live-server

v0.2.2

Published

Apollo Live Server - Making GraphQL Reactive

Downloads

36

Readme

Apollo Live Server

This project is sponsored by Cult of Coders

The interfaces have been crafted to suit Meteor applications.

  • React integration: apollo-live-client
  • Meteor integration: https://github.com/cult-of-coders/apollo

Read more about GraphQL Subscriptions here: https://github.com/apollographql/graphql-subscriptions

Defining Subscription Type

type Subscription {
  notifications: SubscriptionEvent
}

# No need to add this, this is already implemented in apollo package
type SubscriptionEvent {
  event: String
  doc: JSON
}

Creating Subscriptions

A simple, common example:

import { asyncIterator } from 'apollo-live-server';

{
  Subscription: {
    notifications: {
      resolve: payload => payload,
      subscribe(_, args, { db }, ast) {
        // We assume that you inject `db` context with all your collections
        // If you are using Meteor, db.notifications is an instance of Mongo.Collection
        const observable = db.notifications.find();

        return asyncIterator(observable);
      }
    }
  }
}

The current limitation is that the reactive events dispatched by this publication are only at Notification level in the database, but what happens when we want to also send down the pipe relational info, such as, Task linked to Notification by taskId, how can we handle this ?

We hook into the resolver:

import { Event } from 'apollo-live-server';

{
  notifications: {
    resolve: ({event, doc}, args, { db }, ast) => {
      // The doc in here represents only the changeset

      if (event === Event.ADDED) {
        // Feel free to create a server-only client that would allow you to do an actual GraphQL request
        Object.assign(doc, {
          task: db.tasks.findOne(doc.taskId);
        })
      }
      // You can also apply the same concepts for example when a certain relation is changing.

      return {event, doc};
    },
    subscribe() { ... }
  }
}

When we subscribe, we have the ability to subscribe only to certain fields, as-in we don't want all the changes inside the document to trigger change events, so let's try to do that:

import { asyncIterator, astToFields } from 'apollo-live-server';

{
  Subscription: {
    notifications: {
      resolve: payload => payload,
      subscribe(_, args, { db }, ast) {
        const observable = db.notifications.find({}, {
          // This will extract the fields from fields specified under doc
          // in the subscription "query" to GraphQL
          // This would allow clients to subscribe only to the fields they want
          fields: astToFields(ast)
        })

        return asyncIterator(observable);
      }
    }
  }
}

You have the ability to listen only to some events and in the same breath, notify the client about something being added without the changed dataset, only the event. For example you have a newsfeed, and you notify the user that new things have been added, so you can show an actionable: "New stuff appeared, want to reload?"

import { Event } from 'apollo-live-server';

{
  Subscription: {
    notifications: {
      resolve: payload => ({ event: payload.event })
      subscribe(_, args, {db}) {
        const observable = db.feed.find({}, {
          fields: {_id}
        })

        return asyncIterator(observable, {
          events: [Event.ADDED]
        })
      }
    }
  }
}

In order to display that actionable it's up to you to implement your custom subscription handler. A general example is something along

// client = Apollo Client
const observable = client.subscribe({ query: SUBSCRIBE_NEWSFEED });
const subscription = observable.subscribe({
  next({ data }) {
    // data is your payload
    // do something with it
  },
});
subscription.unsubscribe();

Options

asyncIterator(observer, options);
type Options = {
  // You can listen only to some custom events
  events?: Event[],

  // Sends the initial image of your observer down the pipe
  sendInitialAdds?: boolean,
};