What is @envelop/core?
@envelop/core is a powerful library for building GraphQL servers with a focus on extensibility and modularity. It provides a plugin system that allows developers to easily add, remove, or customize functionalities in their GraphQL server setup.
What are @envelop/core's main functionalities?
Plugin System
The plugin system allows you to add various plugins to your GraphQL server. In this example, we use the `useSchema` plugin to set up a basic schema.
const { envelop, useSchema } = require('@envelop/core');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const schema = makeExecutableSchema({
typeDefs: `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello world!',
},
},
});
const getEnveloped = envelop({
plugins: [useSchema(schema)],
});
const { parse, validate, contextFactory, execute, schema: finalSchema } = getEnveloped();
Custom Plugins
You can create custom plugins to extend the functionality of your GraphQL server. This example shows a custom plugin that logs the operation name whenever an operation is executed.
const { envelop, useLogger } = require('@envelop/core');
const customPlugin = {
onExecute({ args }) {
console.log('Executing operation:', args.operationName);
},
};
const getEnveloped = envelop({
plugins: [useLogger(), customPlugin],
});
const { execute } = getEnveloped();
Error Handling
The error handling feature allows you to manage and log errors that occur during GraphQL operations. This example demonstrates how to use the `useErrorHandler` plugin to log errors.
const { envelop, useErrorHandler } = require('@envelop/core');
const errorHandlerPlugin = useErrorHandler((errors) => {
console.error('GraphQL Errors:', errors);
});
const getEnveloped = envelop({
plugins: [errorHandlerPlugin],
});
const { execute } = getEnveloped();
Other packages similar to @envelop/core
apollo-server
Apollo Server is a popular GraphQL server library that provides an easy-to-use setup and a rich ecosystem of tools and integrations. Compared to @envelop/core, Apollo Server is more opinionated and comes with built-in features like caching, tracing, and schema stitching.
graphql-yoga
GraphQL Yoga is a fully-featured GraphQL server with a focus on simplicity and ease of use. It is built on top of GraphQL.js and provides a lot of out-of-the-box features. While @envelop/core focuses on modularity and extensibility through plugins, GraphQL Yoga aims to provide a more straightforward setup.
express-graphql
Express-GraphQL is a minimalistic GraphQL server middleware for Express.js. It is simple to set up and use, making it a good choice for small to medium-sized applications. Unlike @envelop/core, express-graphql does not have a plugin system and is less extensible.