You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/web/docs/src/content/gateway/other-features/custom-plugins.mdx
+212Lines changed: 212 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -826,6 +826,218 @@ Prefer `onRequestParse` when possible, or wrap the hook code in a `try` block.
826
826
|`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). |
827
827
|`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). |
828
828
829
+
### `instrumentation`
830
+
831
+
An optional `instrumentation` instance can be present in the plugin.
832
+
833
+
This `Instrumentation` instance allows to wrap an entire phase execution (including all plugin
834
+
hooks), meaning running code just before, just after and around the execution of the phase.
835
+
836
+
It is an advanced feature that most plugins should not need. Its main usage is for tracing or
837
+
monitoring purposes.
838
+
839
+
Instrumentation doesn't have access to input/output of a phase, use hooks to have access to those
840
+
data. If needed, we recommend to share data between instrumentation and hooks with a `WeakMap` and
841
+
the given `context` as the key.
842
+
843
+
All instrumentation takes 2 parameters:
844
+
845
+
-`payload`: an object containing the graphql `context`, http `request`, or the subgraph
846
+
`executionRequest` depending on the instrument.
847
+
-`wrapped`: The function representing the execution of the phase. It takes no parameters, and
848
+
returns `void` (or `Promise<void>` for asynchrone phases). **This function must always be
849
+
called**. If this function returns a `Promise`, the instrument should return a `Promise` resolving
850
+
after it.
851
+
852
+
#### Example
853
+
854
+
```ts
855
+
const useMyTracer = () => ({
856
+
instrumentation: {
857
+
async request({ request }, wrapped) {
858
+
const start =performance.now()
859
+
// The `wrapped` function represent the execution of this phase.
860
+
// Its argument or result are not accessible, it can only be used to perform action before and after it, or modify its execution context.
0 commit comments