dataloader-warehouse
Enables DataLoader for GraphQL subscriptions
Installation
yarn add dataloader-warehouse
The Problem
A typical data loader is created at the beginning of a mutation and disposed of after it returns. This is a best practice to reduce security concerns and to avoid stale data. Unfortunately, it isn't possible with subscriptions since they are long-lived. The current workaround is to turn off the dataLoader cache, which is horribly inefficient. This package allows you to use the same dataLoader for your mutation and subscription payloads, when safe to do so.
Nomenclature
- dataLoader: an instance created by the DataLoader class
- dataLoaderBase: an object where each key is a dataLoader
- dataLoaderWarehouse: an object where each key is a dataLoaderBase
Usage
1. Create it on your GraphQL server
;const dataLoaderWarehouse = onShare: '_share' ttl: 2000;
2. Add it to your query/mutation context. Call dispose after it's completed.
;; const dataLoaderBase = todos: todoBatchFn;const warehouseWorker = dataLoaderWarehouse;const result = await ;warehouseWorker;
3. Add it to your subscription context. Call dispose after the subscription ends.
; // Important! Note {cache: false}. You should already have been doing this since subs are long-lived.const dataLoaderBase = todos: todoBatchFn cache: false;const warehouseWorker = dataLoaderWarehouse;const asyncIterator = await ;await ;warehouseWorker;
4. Share the ID when you push to the pubsub
// UpdateTodo.js { const updatedTodo = db; const operationId = warehouseWorker; pubsub}
5. Use the operationId in your subscription iterator and unsub when the sub closes
async { const asyncIterator = pubsub; const getNextPromise = async { const nextRes = await asyncIteratornext; const value done = nextRes; if done return asyncIterator; if valueoperationId warehouseWorker; return nextRes; }; return { return ; } return warehouseWorker; return asyncIterator; }
warehouseWorker.get
method to get individual loaders:
6. Use todo: type: Todo resolve: source args warehouseWorker // before // return dataLoaderBase.todos.load(source.id) // after return warehouseWorker
API
The DataLoaderWarehouse takes the following args
ttl
: time to live (ms). Smaller number means less memory usage. 100-5000 is reasonable.onShare
: The name of the method in your dataLoaderBase to call when you callshare()
. Use this to sanitize your dataLoaderBase 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 dataLoaderWarehouse instance has a single public method:
add(dataLoaderBase)
: Call this with an object containing all your loaders. It returns a WarehouseWorker.
The WarehouseWorker (the result of DataLoaderWarehouse#add) has the following methods:
dispose(options)
: schedule the dataloader to be disposed.force
: boolean, defaults to false. If true, calling dispose will dispose of the dataLoaderBase immediately. If falsy, the dataloader will be disposed in the next tick (if not shared) or after the ttl (if shared)
share()
: Returns a unique ID to be fed touseShared
.useShared(operationId)
: Replaces the current dataLoaderBase with the dataLoaderBase belonging to theoperationId
. You'll want to pass in theoperationId
provided by the publishing mutation.getID
: returns the ID of the current dataLoaderBase. Useful for testing.isShared
: returns true if the dataLoaderBase is currently being shared. Useful for testing.
License
MIT