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));
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).
fixEnsure 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.
fixReturn 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.
fixValidate 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.
fixDefine the batch resolver once and assign it directly as the field's resolve property, not inside another function.
Audit
Dependencies
graphqlrequiredPeer dependency required for GraphQL schema types and execution.