@@ -184,70 +184,80 @@ something along these lines (replacing `libraryName` with the name of your
184
184
library):
185
185
186
186
``` ts
187
- import { Middleware , orderedApply , resolvePresets } from " graphile-config" ;
188
-
189
- // Get the user's Graphile Config from somewhere, e.g.
190
- import config from " ./graphile.config.js" ;
191
-
192
- // Resolve the above config, recursively applying all the presets it extends from
193
- const resolvedPreset = resolvePresets ([config ]);
194
-
195
187
/** *** interfaces.ts *****/
196
- export type PromiseOrDirect <T > = Promise <T > | T ;
197
188
198
189
// Define the middlewares that you support, their event type and their return type
199
- interface MyMiddleware {
190
+ export interface MyMiddleware {
200
191
someAction(event : SomeActionEvent ): PromiseOrDirect <SomeActionResult >;
201
192
}
202
193
interface SomeActionEvent {
203
- someParameter: unknown ;
194
+ someParameter: number ;
204
195
/*
205
196
* Use a per-middleware-method interface to define the various pieces of
206
197
* data relevant to this event. **ALWAYS** use the event as an abstraction
207
198
* so that new information can be added in future without causing any
208
199
* knock-on consequences. Note that these parameters of the event may be
209
- * mutated.
200
+ * mutated. The values here can be anything, they don't need to be simple
201
+ * values.
210
202
*/
211
203
}
212
204
// Middleware wraps a function call; this represents whatever the function returns
213
- type SomeActionResult = unknown ;
214
-
215
- /** *** resolveMiddleware.ts *****/
216
-
217
- // Create your middleware instance. The generic describes the events supported
218
- const middleware = new Middleware <MyMiddleware >();
219
- // Now apply the relevant middlewares registered by each plugin (if any) to the
220
- // Middleware instance
221
- orderedApply (
222
- resolvedPreset .plugins ,
223
- (plugin ) => plugin .libraryName ?.middleware ,
224
- (name , fn , _plugin ) => {
225
- middleware .register (name , fn as any );
226
- },
227
- );
205
+ type SomeActionResult = number ;
228
206
229
- /** *** someAction.ts *****/
207
+ export type PromiseOrDirect <T > = Promise <T > | T ;
208
+
209
+ /** *** getMiddleware.ts *****/
210
+
211
+ import { Middleware , orderedApply , resolvePresets } from " graphile-config" ;
212
+
213
+ export function getMiddleware(resolvedPreset : GraphileConfig .ResolvedPreset ) {
214
+ // Create your middleware instance. The generic describes the events supported
215
+ const middleware = new Middleware <MyMiddleware >();
216
+ // Now apply the relevant middlewares registered by each plugin (if any) to the
217
+ // Middleware instance
218
+ orderedApply (
219
+ resolvedPreset .plugins ,
220
+ (plugin ) => plugin .libraryName ?.middleware ,
221
+ (name , fn , _plugin ) => {
222
+ middleware .register (name , fn as any );
223
+ },
224
+ );
225
+ }
226
+
227
+ /** *** main.ts *****/
228
+
229
+ // Get the user's Graphile Config from somewhere, e.g.
230
+ import config from " ./graphile.config.js" ;
231
+
232
+ // Resolve the above config, recursively applying all the presets it extends from
233
+ const resolvedPreset = resolvePresets ([config ]);
234
+
235
+ // Get the middleware for this preset
236
+ const middleware = getMiddleware (resolvedPreset );
230
237
231
238
// Then in the relevant place in your code, call the middleware around the
232
239
// relevant functionality
233
240
const result = await middleware .run (
234
241
" someAction" ,
235
- {
236
- /* event details here */
237
- someParameter: 42 ,
238
- },
242
+ { someParameter: 42 }, // < `event` object
239
243
async (event ) => {
244
+ // Call the underlying method to perform the action.
240
245
// Note: `event` will be the same object as above, but its contents may
241
- // have been modified.
246
+ // have been modified by middlewares .
242
247
return await someAction (event .someParameter );
243
248
},
244
249
);
245
- // The value of `result` should match the return value of `doSomeAction `
250
+ // The value of `result` should match the return value of `someAction(...) `
246
251
// (unless a middleware tweaked or replaced it, of course!)
247
252
248
253
// This is the thing that your middleware wraps. It can do anything, it's just
249
254
// an arbitrary JavaScript function.
250
255
function someAction(someParameter : number ): PromiseOrDirect <SomeActionResult > {
251
256
// Do something here...
257
+ if (Math .random () < 0.5 ) {
258
+ return someParameter ;
259
+ } else {
260
+ return sleep (200 ).then (() => someParameter );
261
+ }
252
262
}
253
263
```
0 commit comments