v0PluginsIntroduction
⚠️
This is the documentation for the old GraphQL Mesh version v0. We recommend upgrading to the latest GraphQL Mesh version v1.

Migrate to GraphQL Mesh v1

Plugins

The gateway can gain new capabilities by using plugins.

.meshrc.yaml
plugins:
  - some-plugin:# name of the plugin
    # ...pluginConfig can be found inside the dedicated documentation
PluginNPM PackageDocs
Mock@graphql-mesh/plugin-mockdocs
Live Queries@graphql-mesh/plugin-live-querydocs
Response Caching@graphql-mesh/plugin-response-cachedocs
StatsD@graphql-mesh/plugin-statsddocs
Prometheus@graphql-mesh/plugin-prometheusdocs
NewRelic@graphql-mesh/plugin-newrelicdocs
Operation Field Permissions@graphql-mesh/plugin-operation-field-permissionsdocs
Rate Limit@graphql-mesh/plugin-rate-limitdocs
Caching in HTTP@graphql-mesh/plugin-http-cachedocs
HTTP Details in Extensions@graphql-mesh/plugin-http-details-extensionsdocs
Deduplicate HTTP Requests@graphql-mesh/deduplicate-requestdocs

Additional Plugins

Envelop is a library that helps build GraphQL API faster and flexibly with plugin-based architecture.

Similar to Express middlewares allowing you to customize requests’ behavior, Envelop applies the same idea to GraphQL requests.

By exposing hooks in all the phases of a GraphQL Request execution, Envelop enables the creation of plugins that simplify the setup of standard API features such as:

  • Security: Depth limits, Rate limiting
  • Authentication
  • Advanced caching
  • Error handling: Sentry, error masking
  • Monitoring: Hive
  • Logging
  • Tracing: NewRelic, Datadog, StatsD, Apollo Tracing

An Envelop plugin is a standalone npm package that provides a plugin function that can be used in an Envelop setup to customize a GraphQL API behavior.

Examples of plugins are:

  • useGenericAuth (@envelop/generic-auth): Custom authentication flow by providing a custom user resolver.
  • useDepthLimit (@envelop/depth-limit): Limit the depth of executed selection sets.

The GraphQL Mesh configuration accepts an additionalEnvelopPlugins parameter that should point to a file that exports a list of Envelop plugins, as shown below:

.meshrc.yaml
sources:
  # …
 
additionalEnvelopPlugins: './envelopPlugins'
envelopPlugins.ts
import { useDepthLimit } from '@envelop/depth-limit'
import { useSentry } from '@envelop/sentry'
import { MeshPlugin } from '@graphql-mesh/types'
 
const plugins: MeshPlugin<any>[] = [
  useDepthLimit({
    maxDepth: 10
  }),
  useSentry({
    includeRawResult: false
  })
]
 
export default plugins
💡

Note: The file can also export a factory function returning a PluginOrDisabledPlugin list

Mesh-specific Plugin Hooks

Mesh provides each plugin with unique hooks:

  • onFetch: triggered when fetch is called. Enables parameters manipulation, logging and even fetch function replacement.
  • onDelegate: triggered when a request is forwarded to the upstream (Either by context SDK or directly through the gateway).

These hooks have access to all remote execution parameters (root, args, context, info etc).

Mesh plugins also support all envelop hooks and a custom plugin can make use of a mix of envelop hooks and the mesh specific hooks.

Unlike envelop hooks onFetch and onDelegate do not have a extendContext function, but the same can be achieved by modifying the context object that is accessible in all hooks.

Example Mesh Envelop plugin therefore can look like this:

import { MeshPlugin, MeshPluginOptions } from '@graphql-mesh/types'
 
export default function customMeshPlugin(options: MeshPluginOptions<YourConfig>): MeshPlugin<any> {
  // Some other logic or config handling
 
  return {
    onFetch(payload) {
      // your logic with payload
    },
    onDelegate(payload) {
      // your logic with payload
    },
    onExecute(payload) {
      // your logic with payload
    }
  }
}

To use a custom plugin that is in the same codebase:

.meshrc.yaml
plugins:
  - ./path/to/plugin: # path to custom plugin instead of just plugin name

Usage of plugin and Native Envelop hooks are described in Envelop page Building Plugins