Code Monkey home page Code Monkey logo

paraloader's Introduction

@yaacovcr/paraloader

  • @yaacovcr/paraloader - A utility for managing concurrent data loaders of different priorities.

Paraloader is a utility that enhances the DataLoader library by adding a layer of priority to batch requests. It leverages DataLoader's batching and caching features, but with a priority system for scheduling the order of execution. This makes Paraloader ideal for applications where certain data fetching operations are more critical than others.

Paraloader is specifically designed to work with the experimental version of incremental delivery for GraphQL, which allows concurrent (early) execution of resolvers for fields in the initial result as well as within deferred payloads. Using DataLoaders with priorities helps prevent entangling of loads from fields in the initial result and deferred payloads.

Getting Started

npm install --save paraloader

Usage

import { ParaLoader } from 'paraloader';
import * as DataLoader from 'dataloader';

const batchLoadFn = async (keys: Array<string>) => {
  // Fetch your data here based on the keys
};

const paraloader = new ParaLoader(batchLoadFn, { maxPriority: 5 });

// Inside your low priority resolver (priority exposed through info argument):
const lowPriorityLoader = paraloader.getLoader(1);
lowPriorityLoader.load('key2'); 

// Elsewhere in your high priority resolver:
const highPriorityLoader = paraloader.getLoader(0);
highPriorityLoader.load('key1');  // <= key1 will be loaded separately and first!

API

ParaLoader

The ParaLoader class is the main interface of the library. It takes a batchLoadFn and an optional options object as arguments.

constructor(batchLoadFn: BatchLoadFn, options: ParaLoaderOptions)

Creates a new ParaLoader instance.

  • batchLoadFn: A function that will be used to fetch the data. The function should return a Promise which resolves to an array of values.
  • options: An optional options object. This can have the following properties:
    • maxPriority: The maximum priority level. Defaults to 9.
    • priorityQueue: An optional custom queue library that should implement the queue and dequeue methods. ParaLoader uses fastpriorityqueue by default.

getLoader(priority: number): DataLoader

Returns a DataLoader instance with the specified priority. If a DataLoader for the given priority already exists, it will return the cached instance. If the specified priority exceeds the maxPriority, it will be set to the maxPriority.

Contribute

If you have any ideas on how we could improve this library, feel free to contribute! Open an issue or a pull request, and we will do our best to respond in a timely manner.

Changelog

Changes are tracked as GitHub releases.

License

@yaacovcr/paraloader is MIT-licensed.

paraloader's People

Contributors

yaacovcr avatar

Watchers

 avatar  avatar

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.