Migrate to GraphQL Mesh v1
Plugins
The gateway can gain new capabilities by using plugins.
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 |
Live Queries | @graphql-mesh/plugin-live-query | docs |
Response Caching | @graphql-mesh/plugin-response-cache | docs |
StatsD | @graphql-mesh/plugin-statsd | docs |
Prometheus | @graphql-mesh/plugin-prometheus | docs |
NewRelic | @graphql-mesh/plugin-newrelic | docs |
Operation Field Permissions | @graphql-mesh/plugin-operation-field-permissions | docs |
Rate Limit | @graphql-mesh/plugin-rate-limit | docs |
Caching in HTTP | @graphql-mesh/plugin-http-cache | docs |
HTTP Details in Extensions | @graphql-mesh/plugin-http-details-extensions | docs |
Deduplicate HTTP Requests | @graphql-mesh/deduplicate-request | docs |
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:
sources:
# …
additionalEnvelopPlugins: './envelopPlugins'
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 whenfetch
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:
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