The Guild LogoThe Guild Monogram

Search docs

Search icon

Products by The Guild

Products

Hive logoHive blurred logo

Hive

Schema Registry for your GraphQL Workflows

Envelop Logo

Envelop

Get Started

Getting Started#

Getting Started#

Installation#

Start by adding the core of envelop to your codebase:

yarn add graphql @envelop/core

Create your Envelop#

Then, create a simple Envelop based on your GraphQL schema:

import { envelop, useSchema } from '@envelop/core'; import { buildSchema } from 'graphql'; const mySchema = buildSchema(/* GraphQL */ ` type Query { hello: String } `); export const getEnveloped = envelop({ plugins: [useSchema(mySchema)], });

Use your Envelop#

The result of envelop is a function that allows you to get everything you need for the GraphQL execution: parse, validate, contextBuilder and execute. Use that to run the client's GraphQL queries.

Here is the most basic example of a GraphQL server using envelop with the http module.

import { createServer } from 'http'; import { getEnveloped } from './envelop'; const httpServer = createServer(); httpServer.on('request', async (req, res) => { // Here you get the alternative methods that are bundled with your plugins // You can pass anything from your incoming request to make it available // for Envelop plugins, and to make it available as part of your // GQL execution context const { parse, validate, contextFactory, execute, schema } = getEnveloped({ req }); // Parse the initial request and validate it const { query, variables } = JSON.parse(req.payload); const document = parse(query); const validationErrors = validate(schema, document); if (validationErrors.length > 0) { return res.end(JSON.stringify({ errors: validationErrors })); } // Build the context and execute const contextValue = await contextFactory(); const result = await execute({ document, schema, variableValues: variables, contextValue, }); // Send the response res.end(JSON.stringify(result)); }); httpServer.listen(3000);

The example above uses Node.js http module, but you can use envelop with all popular frameworks and servers. Check the examples on our Integrations page.

Add your plugins#

Adding new functionality is as easy as adding a new envelop plugin to your base envelop setup.

Here's a simple example for collecting metrics and log all incoming requests, using the built-in plugins:

import { envelop, useSchema, useLogger, useTiming } from '@envelop/core'; const getEnveloped = envelop({ plugins: [useSchema(schema), useLogger(), useTiming()], });

A list of all available plugins can be found on our Plugin Hub!