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

shared-dataloader

v0.2.0

Published

A class for sharing dataloaders across GraphQL subscriptions

Downloads

6

Readme

shared-dataloader

A class for sharing dataloaders across GraphQL subscriptions

Installation

yarn add shared-dataloader

What's it do

Allows you to use the same dataloader for multiple GraphQL subscription clients. If you think of a mutation as a single operation, this allows you to share data across that operation.

Usage

1. Create it on your GraphQL server

import SharedDataLoader from 'shared-dataloader';
const sharedDataLoader = new SharedDataLoader({PROD, onShare: '_share', ttl: 5000});

2. Add it to your query/mutation context. Call dispose after it's completed.

import DataLoader from 'dataloader';
import {graphql} from 'graphql';

const allMyDataLoaders = {todos: new DataLoader(todoBatchFn)};
const dataLoader = sharedDataLoader.add(allMyDataLoaders);
const result = await graphql(schema, query, {}, {dataLoader}, variables);
dataLoader.dispose();

3. Add it to your subscription context. Call dispose after the subscription ends.

import {subscribe} from 'graphql';

// Important! Note {cache: false}. You should already have been doing this since subs are long lived.
const allMyDataLoaders = {todos: new DataLoader(todoBatchFn, {cache: false})};
const dataLoader = sharedDataLoader.add(allMyDataLoaders);
const asyncIterator = await subscribe(schema, document, {}, {dataLoader}, variables);
await forAwaitEach(asyncIterator, iterableCb);
dataLoader.dispose();

4. Share the ID when you push to the pubsub

// UpdateTodo.js
resolve(source, args, {dataLoader}) {
  const updatedTodo = db.update({foo: 'bar'});
  const operationId = dataLoader.share();
  pubsub.publish('updatedTodo', {updatedTodo, operationId})
}

5. Use the shared ID in your subscription iterator and unsub when the sub closes

async subscribe(source, args, {dataLoader}) {
  const asyncIterator = pubsub.asyncIterator('updatedTodo');
  const getNextPromise = async () => {
    const nextRes = await asyncIterator.next();
    const {value, done} = nextRes;
    if (done) {
      return asyncIterator.return();
    }
    if (value.operationId) {
      dataLoader.useShared(value.operationId);
    }
    return nextRes;
  };
  return {
    next() {
      return getNextPromise();   
    },
    return() {
      dataLoader.dispose();
      return asyncIterator.return();
    }
  }
}

6. Use the dataloader getter method to get individual loaders:

todos: {
  resolve: (source, args, {dataLoader}) {
    return dataLoader.get('todos').load(source.id)
  }
}

API

The SharedDataLoader takes the following args

  • PROD: true if running in production (don't show warnings). Defaults to false.
  • ttl: time to live (ms). Smaller number means less memory usage. 100-5000 is reasonable.
  • onShare: The name of the method in your dataloader object to call when you call share(). Use this to sanitize your dataloader of any sensitive info that might have been provided to it (such as an auth token) This is not required, but provides peace of mind if you're unsure about your schema authorization.

The SharedDataLoader has a single public method:

  • add(allMyLoaders): Call this with an object containing all your loaders. It returns a ShareableDataLoader.

The ShareableDataLoader (the result of SharedDataLoader#add) has the following API

  • dispose(options): dispose of the data loader if it is not being shared. It has the following option:
    • force: boolean, defaults to false. If true, calling dispose will dispose of the dataloader even if it is being shared.
  • share(ttl): Returns a unique ID to be fed to useShared. Also begins the TTL. Although strongly discouraged, you may provide a TTL here to override the one defined by the SharedDataLoader. This is useful if you need to extend the time because you are making an external API call, or using setTimeout.
  • useShared(operationId): Replaces the current dataloader with the dataloader belonging to the operationId. You'll want to call this on your subscription with the operationId that comes from the mutation
  • getID: returns the ID of the current dataloader. Useful for testing.
  • isShared: returns true if the dataloader is currently being shared. Useful for testing.

License

MIT