Skip to content

Commit 2810b3a

Browse files
committed
docs(instruments): Add documentation for new Instruments API
1 parent bf06e94 commit 2810b3a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

packages/web/docs/src/content/gateway/other-features/custom-plugins.mdx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,123 @@ Prefer `onRequestParse` when possible, or wrap the hook code in a `try` block.
826826
| `serverContext` | The final context object that is shared between all hooks and the GraphQL execution. [Learn more about the context](https://the-guild.dev/graphql/yoga-server/docs/features/context#server-context). |
827827
| `response` | The outgoing HTTP response as WHATWG `Response` object. [Learn more about the response interface](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
828828

829+
### `instruments`
830+
831+
An optional `instruments` instance can be present in the plugin.
832+
833+
This `Instruments` instance allows to wrap an entire phase execution (including all plugin hooks),
834+
meaning running code just before, just after and around the execution of the phase.
835+
836+
Instruments doesn't have access to input/output of a phase, use hooks to have access to those data.
837+
If needed, we recommend to share data between instruments and hooks with a `WeakMap` and the given
838+
`context` as the key.
839+
840+
All instruments takes 2 parameters:
841+
842+
- `payload`: an object containing the graphql context or the http request depending on the
843+
instrument.
844+
- `wrapped`: The function representing the execution of the phase. It takes no parameters, and
845+
returns `void` (or `Promise<void>` for asynchrone phases). **This function must always be
846+
called**. If this function returns a `Promise`, the instrument should return a `Promise` resolving
847+
after it.
848+
849+
#### Instruments composition
850+
851+
If multiple plugins have `instruments`, they are composed in the same order they are defined the
852+
plugin array (the first is outtermost call, the last is inner most call).
853+
854+
It is possible to customize this composition if it doesn't suite your need (ie. you need hooks and
855+
instruments to have a different oreder of execution).
856+
857+
```ts
858+
import { composeInstruments, envelop } from '@envelop/core'
859+
860+
const { instruments: instruments1, ...plugin1 } = usePlugin1()
861+
const { instruments: instruments2, ...plugin2 } = usePlugin2()
862+
863+
const instruments = composeInstruments([instruments2, instruments1])
864+
865+
const getEnveloped = envelop({
866+
plugin: [{ insturments }, plugin1, plugin2]
867+
})
868+
```
869+
870+
#### `request`
871+
872+
Wraps the HTTP request handling. This includes all the plugins `onRequest` and `onResponse` hooks.
873+
874+
This instrument can be asynchronous, the wrapped funcion **can be** asynchronous. Be sure to return
875+
a `Promise` if `wrapped()` returned a `Promise`.
876+
877+
#### `requestParse`
878+
879+
Wraps the parsing of the request phase to extract grapqhl params. This include all the plugins
880+
`onRequestParse` hooks.
881+
882+
This insturment can be asynchronous, the wrapped function **can be** asynchrounous. Be sure to
883+
return a `Promise` if `wrapped()` returns a `Promise`.
884+
885+
#### `operation`
886+
887+
Wraps the Graphql operation execution pipeline. This is called for each graphql operation, meaning
888+
it can be called mutliple time for the same HTTP request if batching is enabled.
889+
890+
This instrument can be asynchronous, the wrapped function **can be** asynchronous. Be sur to return
891+
a `Promise` if `wrapped()` returnd a `Promise`.
892+
893+
#### `init`
894+
895+
Wraps the envelop (the call to `envelop` function) initialisation.
896+
897+
This includes all the plugins `onEnveloped` hooks, and the creation of the Envelop Orchestrator.
898+
899+
This instruments must be synchrone, the wrapped function is always synchrone.
900+
901+
#### `parse`
902+
903+
Wraps the parse phase. This includes all the plugins `onParse` hooks and the actual engine `parse`.
904+
905+
This instrument must be synchrone, the wrapped function is always synchrone.
906+
907+
#### `validate`
908+
909+
Wraps the validate phase. This includes all the plugins `onValidate` hooks and the actual engine
910+
`validate`.
911+
912+
This instrument must be synchrone, the wrapped function is always synchrone.
913+
914+
#### `context`
915+
916+
Wraps the context building phase. This includes all the plugins `onContextBuilding` hooks.
917+
918+
This instrument must be synchrone, the wrapped function is always synchrone.
919+
920+
#### `execute`
921+
922+
Wraps the execute phase. This includes all the plugins `onExecute` hooks.
923+
924+
This instrument can be asynchrone, the wrapped function **can be** asynchrone. Be sure to `await` or
925+
use `.then` on the result of the `wrapped` function to run code after the `execute` phase.
926+
927+
Note that `wrapped` is not guaranted to return a promise.
928+
929+
#### `subscribe`
930+
931+
Wraps the subscribe phase. This includes all the plugins `onSubsribe` hooks. Note that it doesn't
932+
wrap the entire lifetime of the subscription, but only it's intialisation.
933+
934+
This instrument can be asynchrone, the wrapped function **can be** asynchrone. Be sure to `await` or
935+
use `.then` on the result of the `wrapped` function to run code after the `subsribe` phase.
936+
937+
Note that `wrapped` is not guaranted to return a promise.
938+
939+
#### `resultProcess`
940+
941+
Wraps the context result processing phase. This includes all the plugins `onResultProcess` hooks.
942+
943+
This instruments can be asynchrone, the wrapped function **can be** asynchronous. a `Promise` if
944+
`wrapped()` returns a `Promise`.
945+
829946
### Plugin Context
830947

831948
Hive Gateway comes with ready-to-use `logger`, `fetch`, cache storage and etc that are shared across

0 commit comments

Comments
 (0)