Socket
Book a DemoInstallSign in
Socket

@mavenoid/dataloader

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mavenoid/dataloader

npm-package-template

Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
15
66.67%
Maintainers
2
Weekly downloads
 
Created
Source

@mavenoid/dataloader

Utilities for caching, batching and deduping requests in GraphQL. Inspired by dataloader but extended using WeakMap to create a more convenient API for GraphQL

Rolling Versions

Installation

yarn add @mavenoid/dataloader

Usage

import {
  getDataLoadersForContextType,
  RequestsBatch,
} from '@mavenoid/dataloader';

const {batch, dedupe} = getDataLoadersForContextType<ResolverContextType>();

const getUserDeduped = dedupe(
  async (userID: number, _resolverContext: ResolverContextType) => {
    return await getUserUncached(userID);
  },
);

// You can load a user within a GraphQL resolver using:
//
//   getUserDeduped(userID, resolverContext)
//
// You can call this as many times as you like, and `getUserUncached` will only be called
// once per request.
//
// if you modify a user within a request, you can delete it from the cache using:
//
//   getUserDeduped.cache(resolverContext).delete(userID)
//
// or
//
//   getUserDeduped.cache(resolverContext).clear()

const getUserBatched = batch(
  async (userIDs: number[], _resolverContext: ResolverContextType) => {
    // load all the requested userIDs in one go, and then return a map from
    // the requested IDs to the results
    const users = await db_users.find({id: anyOf(userIDs)}).all();
    return new Map(users.map((user) => [user.id, user]));
  },
  {maxBatchSize: 100},
);

// You can load a user within a GraphQL resolver using:
//
//   getUserBatched(userID, resolverContext)
//
// You can call this as many times as you like, and the database will only be queried
// once per request. The requests will be batched so that up to 100 users are loaded
// at a time.
//
// if you modify a user within a request, you can delete it from the cache using:
//
//   getUserBatched.cache(resolverContext).delete(userID)
//
// or
//
//   getUserBatched.cache(resolverContext).clear()

Handling undefined

The batch methods return undefined for any requested records that are not included in the results. If this is not what you want, you can add a fallback using:

import {
  handleUndefinedResult,
  errorOnUndefinedResult,
} from '@mavenoid/dataloader';

const getUserOrNull = handleUndefinedResult(getUserBatched, () => null);
const getUserOrError = errorOnUndefinedResult(
  getUserBatched,
  (id) => `Could not find user: ${id}`,
);

FAQs

Package last updated on 18 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts