Security News
npm Updates Search Experience with New Objective Sorting Options
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
@graphql-tools/batch-execute
Advanced tools
A set of utils for faster development of GraphQL tools
@graphql-tools/batch-execute is a utility package for batching and executing GraphQL operations. It is part of the GraphQL Tools ecosystem and is designed to optimize the execution of multiple GraphQL operations by batching them together, which can improve performance and reduce the number of network requests.
Batch Execution
This feature allows you to batch multiple GraphQL operations and execute them together. The code sample demonstrates creating a simple schema and batching two queries ('{ hello }' and '{ world }') for execution.
const { batchExecute } = require('@graphql-tools/batch-execute');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { graphql } = require('graphql');
const typeDefs = `
type Query {
hello: String
world: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello',
world: () => 'World'
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const operations = [
{ query: '{ hello }' },
{ query: '{ world }' }
];
batchExecute({ schema, operations }).then(results => {
console.log(results);
});
Custom Execution Context
This feature allows you to provide a custom execution context for the batched operations. The code sample demonstrates how to pass a context object containing user information to the batchExecute function.
const { batchExecute } = require('@graphql-tools/batch-execute');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { graphql } = require('graphql');
const typeDefs = `
type Query {
hello: String
world: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello',
world: () => 'World'
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const operations = [
{ query: '{ hello }' },
{ query: '{ world }' }
];
const context = { user: { id: 1, name: 'John Doe' } };
batchExecute({ schema, operations, context }).then(results => {
console.log(results);
});
Dataloader is a generic utility to batch and cache data-fetching operations. It is often used in GraphQL servers to optimize the resolution of fields that require fetching data from a database or other remote service. Unlike @graphql-tools/batch-execute, which focuses on batching GraphQL operations, Dataloader is more general-purpose and can be used for any kind of data-fetching.
Apollo Server is a fully-featured GraphQL server with built-in support for batching and caching. It provides a more comprehensive solution for building GraphQL APIs, including schema stitching, subscriptions, and more. While @graphql-tools/batch-execute focuses specifically on batching GraphQL operations, Apollo Server offers a broader set of features for building and optimizing GraphQL APIs.
Check API Reference for more information about this package; https://www.graphql-tools.com/docs/api/modules/batch_execute_src
You can also learn more about Batch Delegation in this chapter; https://www.graphql-tools.com/docs/stitch-schema-extensions#batch-delegation
FAQs
A set of utils for faster development of GraphQL tools
The npm package @graphql-tools/batch-execute receives a total of 2,875,460 weekly downloads. As such, @graphql-tools/batch-execute popularity was classified as popular.
We found that @graphql-tools/batch-execute demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers 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
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.