Skip to content

Commit 2894b0e

Browse files
committed
Example adds logging and try/finally for clarity
1 parent faf2f9e commit 2894b0e

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

utils/website/graphile-config/plugin/middleware.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,30 @@ in the same resolved preset.
195195
```ts
196196
export const MyPlugin: GraphileConfig.Plugin = {
197197
name: "MyPlugin",
198-
// Plugins can have order constraints at the plugin level and at the
199-
// middleware level. All middleware in MyPlugin will be executed before any
200-
// middleware in plugins that have `provides: ["featureA" ]`
198+
// Plugins can have default order constraints at the plugin level and can
199+
// override them at the middleware level.
200+
// This states that by default, middleware in MyPlugin will be executed
201+
// before any other plugins' middleware that provides 'featureA'.
201202
before: ["featureA"],
202203
libraryName: {
203204
middleware: {
204205
foo(next) {
205206
// ... do something
206207
return next();
207208
},
209+
208210
bar: {
209211
after: ["featureB"],
210-
callback(next) {
211-
// ... do something
212-
// Will be executed after middleware that set
213-
// `provides: ['featureB']`
214-
return next();
212+
async callback(next) {
213+
// Executed after middleware that provides 'featureB'
214+
console.log("MyPlugin");
215+
try {
216+
return await next();
217+
} finally {
218+
console.log("/MyPlugin");
219+
}
215220
},
216221
},
217-
// ... any other middleware
218222
},
219223
},
220224
};
@@ -225,14 +229,27 @@ export const OtherPlugin: GraphileConfig.Plugin = {
225229
middleware: {
226230
bar: {
227231
provides: ["featureB"],
228-
callback(next) {
229-
// ... do something
230-
return next();
232+
async callback(next) {
233+
console.log("OtherPlugin");
234+
try {
235+
return await next();
236+
} finally {
237+
console.log("/OtherPlugin");
238+
}
231239
},
232240
},
233241
},
234242
},
235243
};
244+
245+
/* Result of executing the `bar` action:
246+
247+
OtherPlugin
248+
MyPlugin
249+
/MyPlugin
250+
/OtherPlugin
251+
252+
*/
236253
```
237254

238255
Similar to plugins' `provides` property, Graphile Config appends the plugin

0 commit comments

Comments
 (0)