Using Envelop is straight-forward and intuitive, in this section you will learn how to install and
set up a GraphQL server using Envelop from scratch.
Installation
Start by adding the core of envelop and graphql to your codebase.
npm i @envelop/core graphql
yarn add @envelop/core graphql
pnpm add @envelop/core graphql
bun add @envelop/core graphql
Create Your First Envelop
After installing the @envelop/core package, you can use the envelop function for creating your
getEnveloped function. We use a simple GraphQL schema that we build with the buildSchema
function from graphql.
import * as GraphQLJS from 'graphql'import { envelop, useEngine, useSchema } from '@envelop/core'const schema = GraphQLJS.buildSchema(/* GraphQL */ ` type Query { hello: String }`)export const getEnveloped = envelop({ plugins: [useEngine(GraphQLJS), useSchema(schema)]})
Use Your Envelop
The result of envelop is a factory function that allows you to get everything you need for the
GraphQL execution: parse, validate, contextBuilder, execute and subscribe. It is usually
named getEnveloped.
By calling the getEnveloped function you will get all the primitive functions required for the
GraphQL execution layer.
After you set up your base Envelop, you can start customizing your GraphQL server. Adding new
functionality is as easy as adding a new envelop plugin to your base envelop setup.
Let’s add a parser and validation cache, so sending the same operation string sent to our server is
cached using an LRU cache, allowing to improve the overall performance of the GraphQL execution
layer.
npm i @envelop/parser-cache @envelop/validation-cache