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

dataloader-align-results

v0.4.0

Published

Takes your back-end results and returns an Array acceptable to DataLoader.

Downloads

5

Readme

dataloader-align-results

Dependency Status Download Status

Takes your back-end results and returns an Array acceptable to DataLoader.

Installation

npm install dataloader-align-results --save

Documentation

To uphold the constraints of the DataLoader batch function, e.g. new DataLoader(callBatchGetUsers), the batch function must return an Array of values the same length as the Array of keys, and re-order them to ensure each index aligns with the original keys.

The back-end service typically returns results in a different order than we requested, and it typically omits a result for some keys when no value exists for that key.

This utility takes such back-end results and returns an Array acceptable to DataLoader.

dataLoaderAlignResults({ graphqlType, serializeDataLoaderKey, serializeRecordKey, onError });

  • grapgqlType (string) - Type of GraphQL result to return for each DataLoader key
    • '[!]!' - An array of non-null objects.
    • '[!]' - An array of non-null objects, or null.
    • '[]!' - Null, else an array of elements each of which is either an object or null.
    • '[]' - An array of elements each of which is either an object or null.
    • '!' - A non-null object.
    • '' - An object or null.
  • serializeDataLoaderKey (optional, function(key)) - Function to serialize a key passed from DataLoader.
    • A non-trivial serialization function should more efficiently be passed to DataLoader, e.g. new DataLoader(keys => {/* ... */}, { cacheKeyFn: serializeDataLoaderKey }), than to dataLoaderAlignResults.
    • Defaults to the trivial key => key.toString().
  • serializeRecordKey (optional, function(record) or string) - Function to serialize key from the record.
    • The resulting value must be strictly equal to that produced by serializeDataLoaderKey, or passed by DataLoader.
    • If a string is provided, record => record[serializeRecordKey].toString() is used.
  • onError (optional, function(msg, detail)) - Handler for terminal errors.
    • Errors are ignored if onError is not provided.
  • isStrict (optional, boolean) - It may be more convenient at times to return [] for a missing key rather than null, as the null may require special processing in the calling code.
    • true - Return null.
    • else - Return [].

dataLoaderAlignResults returns a function(keys, resultsArray) which should be passed to DataLoader as its first parameter.

  • keys - The keys passed from the DataLoader.
  • resultsArray - Result from back-end service after processing the keys.
    • Results are likely in a different order than that of keys.
    • A result may be omitted for some keys.

The batch loader function calls onError if it finds results incompatible with graphqlType. However it cannot check for a null final result for '[]!' or '[!]!' because DataLoader itself builds the final Array from elements returned by the batch loader plus elements from the cache.

Complete Example

const DataLoader = require('dataloader');
const dataloaderAlignResults = require('dataloader-align-results');
 
const userLoader = new DataLoader(keys => myBatchGetUsers(keys));
const userAlignResults = dataloaderAlignResults({
  graphqlType: '!', onError: (msg, detail) => { throw new Error(`${msg}\n${detail}`); } });
 
// These logical reads will be resolved with one call to the back-end service.
Promise.all([
  userLoader.load(3),
  userLoader.load(2),
  userLoader.load(4),
  userLoader.load(1),
])
  .then(resolvedResults => { /* ... */ });

 
function myBatchGetUsers(keys) {
  return callBatchGetUsers(keys) // Call back-end service.
    .then(resultsArray => userAlignResults(resultsArray, keys));
}

License

Copyright (c) 2017

Licensed under the MIT license.