Packages

@envelop/core

6.0.1 · 12 Nov 2026MIT12.6M / weekRepositoryHomepagenpmAtom feed

Your version

5.3.0

20 Jun 2025Where it was published

5.6.1 is the newest release of 5.x.

In short

4 new

Release note

The section about @envelop/core of a note shared by several packages.

Minor Changes

New plugin utility to ease data sharing between hooks

Sometimes, plugins can grow in complexity and need to share data between its hooks.

A way to solve this can be to mutate the graphql context, but this context is not always available
in all hooks in Yoga or Hive Gateway plugins. Moreover, mutating the context gives access to your
internal data to all other plugins and graphql resolvers, without mentioning performance impact on
field access on this object.

The recommended approach to this problem was to use a WeakMap with a stable key (often the
context or request object). While it works, it's not very convenient for plugin developers,
and is prone to error with the choice of key.

The new withState utility solves this DX issue by providing an easy and straightforward API for
data sharing between hooks.

import { withState } from '@envelop/core'

type State = { foo: string }

const myPlugin = () =>
  withState<Plugin, State>(() => ({
    onParse({ state }) {
      state.forOperation.foo = 'foo'
    },
    onValidate({ state }) {
      const { foo } = state.forOperation
      console.log('foo', foo)
    }
  }))

The state payload field will be available in all relevant hooks, making it easy to access shared
data. It also forces the developer to choose the scope for the data:

  • forOperation for a data scoped to GraphQL operation (Envelop, Yoga and Hive Gateway)

  • forRequest for a data scoped to HTTP request (Yoga and Hive Gateway)

  • forSubgraphExecution for a data scoped to the subgraph execution (Hive Gateway)

Not all scopes are available in all hooks, the type reflects which scopes are available

Under the hood, those states are kept in memory using WeakMap, which avoid any memory leaks.

It is also possible to manually retrieve the state with the getState function:

const myPlugin = () =>
  withState(getState => ({
    onParse({ context }) {
      // You can provide a payload, which will dictate which scope you have access to.
      // The scope can contain `context`, `request` and `executionRequest` fields.
      const state = getState({ context })
      // Use the state elsewhere.
    }
  }))

A whole project to check? Paste its package.json in the dependency check: every deprecated, outdated or vulnerable dependency, with what to upgrade to. Free, no account.