Registry / database / graphql-resolve-batch

graphql-resolve-batch

JSON →
library1.0.3jsnpmunverified

A GraphQL batching model that groups execution by GraphQL fields as an alternative to DataLoader. Version 1.0.3 is stable with no recent updates. It integrates with both GraphQL.js and graphql-tools, batching field resolutions by source field arguments rather than simple keys, solving N+1 problems even with varying arguments per field — a key differentiator from DataLoader's per-key batching. Release cadence is low; last release in 2018.

npm install graphql-resolve-batch
INSTALL
IMPORT
SIG · GRAPHQL-RESOLVE-BA
G
graphql-resolve-batch
databasejavascriptv1.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createBatchResolver
import { createBatchResolver } from 'graphql-resolve-batch'
const { createBatchResolver } = require('graphql-resolve-batch')
The package is ESM-first. Use import syntax for best compatibility; require may work in some environments but is not recommended.
default (createBatchResolver)
import createBatchResolver from 'graphql-resolve-batch'
const createBatchResolver = require('graphql-resolve-batch')
The default export is the same function as the named export 'createBatchResolver'. Both are equivalent.
BatchResolver
import { BatchResolver } from 'graphql-resolve-batch'
import { BatchResolverFn } from 'graphql-resolve-batch'
The type is exported as BatchResolver, often used with TypeScript. Not a function, just a type definition.

Shows basic usage of createBatchResolver to batch a nested field, grouping calls by field and arguments.

import { GraphQLObjectType, GraphQLString, GraphQLList, GraphQLSchema, graphql } from 'graphql'; import { createBatchResolver } from 'graphql-resolve-batch'; // Simulate a database const users = [{ id: '1', name: 'Alice' }, { id: '2', name: 'Bob' }]; const friends = { '1': ['2'], '2': ['1'] }; const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ name: { type: GraphQLString }, friends: { type: new GraphQLList(UserType), args: { limit: { type: GraphQLString } }, resolve: createBatchResolver(async (sources, args, context) => { // sources is array of parent objects, args is the last args object? // Actually args here is the first args? Document carefully: // The batch resolver receives (sources, args, context) where sources is array of all parent values for this field, // and args is the common arguments object shared across all batched calls (so must be identical) const { db } = context; // For simplicity, just return friends for each source return sources.map(source => { const friendIds = db.friends[source.id] || []; return friendIds.map(id => db.users.find(u => u.id === id)); }); }), }, }), }); const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: { user: { type: UserType, resolve: () => users[0] } } }) }); const query = `{ user { name friends(limit: "2") { name } } }`; graphql(schema, query).then(result => console.log(result));
Debug
Known issues
breakingcreateBatchResolver requires that all batched calls have identical arguments. If arguments vary, batching will not work correctly and may produce wrong results.
fix
Ensure that field arguments are consistent across all batched calls; otherwise use DataLoader or manual batching with distinct argument sets.
affects: >=1.0.0
deprecatedThe package has not been updated since 2018 and may be incompatible with newer versions of graphql (v16+).
fix
Consider migrating to modern batching solutions like DataLoader with argument hashing, or use graphql@<16 for compatibility.
affects: >=1.0.0
gotchaThe batch resolver receives sources (array of parent values) and args (the arguments from the first call? Actually it's the common args but if arguments differ, only the first args are used). This can cause data corruption if arguments vary.
fix
Ensure all batched invocations use identical argument objects. If not, split into separate batch resolvers per argument set.
affects: >=1.0.0
gotchaThe return value of the batch resolver must be an array of arrays (one per source) for list fields. Incorrect structure leads to runtime errors.
fix
Verify the resolver returns an array where each element corresponds to the resolved value for the respective source in the sources array.
affects: >=1.0.0
gotchaThe batch resolver does not support async iterables or subscriptions, only query/mutation fields.
fix
Use other batching mechanisms for subscription resolvers.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: sources.map is not a function
The batch resolver received a single source object instead of an array, likely because the field was resolved without batching (e.g., at top level).
fix
Ensure createBatchResolver is used only on fields that are nested under a parent that may be resolved multiple times (like list fields). For top-level queries, use normal resolvers.
Cannot read property 'length' of undefined
The resolver returned an array that is not aligned with the number of sources, or returned undefined for some sources.
fix
Return an array of exactly the same length as sources, with each element being the resolved value for that source.
GraphQLError: Argument "limit" has invalid value
The args object may be a single value shared across calls but the resolver expects the correct type per source.
fix
Validate that args are consistently provided and correctly typed; consider using a wrapper to pass distinct args as part of the source.
Error: You may only call createBatchResolver once per field resolver.
Attempting to call createBatchResolver inside a resolver that is already batched, or reusing the same batch resolver instance incorrectly.
fix
Define the batch resolver once and assign it directly as the field's resolve property, not inside another function.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency required for GraphQL schema types and execution.
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-resolve-batch — npm install graphql-resolve-batch · libregistry