Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
dataloader-align-results
Advanced tools
Takes your back-end results and returns an Array acceptable to DataLoader.
Takes your back-end results and returns an Array acceptable to DataLoader.
npm install dataloader-align-results --save
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
null
.null
.null
.null
.serializeDataLoaderKey
(optional, function(key)) - Function to serialize a key passed from DataLoader.
new DataLoader(keys => {/* ... */}, { cacheKeyFn: serializeDataLoaderKey })
,
than to dataLoaderAlignResults.key => key.toString()
.serializeRecordKey
(optional, function(record) or string) - Function to serialize key from the record.
record => record[serializeRecordKey].toString()
is used.onError
(optional, function(msg, detail)) - Handler for terminal errors.
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
.
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.
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));
}
Copyright (c) 2017
Licensed under the MIT license.
v0.3.0 (2017-10-08)
FAQs
Takes your back-end results and returns an Array acceptable to DataLoader.
The npm package dataloader-align-results receives a total of 0 weekly downloads. As such, dataloader-align-results popularity was classified as not popular.
We found that dataloader-align-results demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.