What is @graphql-tools/executor?
@graphql-tools/executor is a package that provides tools for executing GraphQL operations. It is part of the larger GraphQL Tools ecosystem, which offers a variety of utilities for building and managing GraphQL schemas and operations.
What are @graphql-tools/executor's main functionalities?
Basic Execution
This feature allows you to execute a basic GraphQL query against a schema. The code sample demonstrates creating a simple schema with a single query and executing it.
const { execute } = require('@graphql-tools/executor');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const typeDefs = `
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const query = `{
hello
}`;
execute({ schema, document: query }).then(result => {
console.log(result);
});
Custom Execution Context
This feature allows you to pass a custom context to the execution. The code sample demonstrates how to use a context value in a resolver.
const { execute } = require('@graphql-tools/executor');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const typeDefs = `
type Query {
hello(name: String): String
}
`;
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name || 'world'}!`,
},
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const query = `{
hello(name: "Alice")
}`;
const context = { user: 'Alice' };
execute({ schema, document: query, contextValue: context }).then(result => {
console.log(result);
});
Subscription Execution
This feature allows you to execute GraphQL subscriptions. The code sample demonstrates setting up a subscription and publishing an event.
const { subscribe } = require('@graphql-tools/executor');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();
const typeDefs = `
type Query {
_empty: String
}
type Subscription {
messageSent: String
}
`;
const resolvers = {
Subscription: {
messageSent: {
subscribe: () => pubsub.asyncIterator(['MESSAGE_SENT']),
},
},
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const subscriptionQuery = `
subscription {
messageSent
}
`;
(async () => {
const iterator = await subscribe({ schema, document: subscriptionQuery });
for await (const result of iterator) {
console.log(result);
}
})();
// Simulate sending a message
setTimeout(() => {
pubsub.publish('MESSAGE_SENT', { messageSent: 'Hello world!' });
}, 1000);
Other packages similar to @graphql-tools/executor
graphql
The 'graphql' package is the reference implementation of GraphQL for JavaScript. It provides the core functionality for parsing, validating, and executing GraphQL queries. While it offers similar execution capabilities, it does not include the higher-level utilities provided by @graphql-tools/executor.
apollo-server
Apollo Server is a community-maintained open-source GraphQL server that works with any GraphQL schema. It provides a more comprehensive solution for building GraphQL APIs, including schema stitching, data sources, and more. It includes execution capabilities but is more feature-rich compared to @graphql-tools/executor.
graphql-yoga
GraphQL Yoga is a fully-featured GraphQL server with focus on easy setup, performance, and great developer experience. It provides a higher-level abstraction over the core 'graphql' package, including execution, but also offers additional features like subscriptions and file uploads.