---
description:
  Discover a world of possibilities with Gateway plugins! Extend your capabilities with mock,
  caching, and more. Envelop also offers customizable plugins for a faster GraphQL API.
---

import { Callout } from '@theguild/components'

# Plugins

The gateway can gain new capabilities by using plugins.

```yaml filename=".meshrc.yaml"
plugins:
  - some-plugin:# name of the plugin
    # ...pluginConfig can be found inside the dedicated documentation
```

| Plugin                      | NPM Package                                        | Docs                                              |
| --------------------------- | -------------------------------------------------- | ------------------------------------------------- |
| Mock                        | `@graphql-mesh/plugin-mock`                        | [docs](/docs/plugins/mock)                        |
| Live Queries                | `@graphql-mesh/plugin-live-query`                  | [docs](/docs/plugins/live-queries)                |
| Response Caching            | `@graphql-mesh/plugin-response-cache`              | [docs](/docs/plugins/response-caching)            |
| StatsD                      | `@graphql-mesh/plugin-statsd`                      | [docs](/docs/plugins/statsd)                      |
| Prometheus                  | `@graphql-mesh/plugin-prometheus`                  | [docs](/docs/plugins/prometheus)                  |
| NewRelic                    | `@graphql-mesh/plugin-newrelic`                    | [docs](/docs/plugins/new-relic)                   |
| Operation Field Permissions | `@graphql-mesh/plugin-operation-field-permissions` | [docs](/docs/plugins/operation-field-permissions) |
| Rate Limit                  | `@graphql-mesh/plugin-rate-limit`                  | [docs](/docs/plugins/rate-limit)                  |
| Caching in HTTP             | `@graphql-mesh/plugin-http-cache`                  | [docs](/docs/plugins/http-cache)                  |
| HTTP Details in Extensions  | `@graphql-mesh/plugin-http-details-extensions`     | [docs](/docs/plugins/http-details-extensions)     |
| Deduplicate HTTP Requests   | `@graphql-mesh/deduplicate-request`                | [docs](/docs/plugins/deduplicate-request)         |

## Additional Plugins

[Envelop](https://envelop.dev) 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:

```yaml filename=".meshrc.yaml"
sources:
  # …

additionalEnvelopPlugins: './envelopPlugins'
```

```ts filename="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
```

<Callout>
  Note: The file can also export a factory function returning a `PluginOrDisabledPlugin` list
</Callout>

## 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](https://the-guild.dev/graphql/envelop/v4/plugins/lifecycle) 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:

```ts
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:

```yaml filename=".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](https://the-guild.dev/graphql/envelop/v3/plugins/custom-plugin#the-plugin-interface)
