Skip to content

Commit 4cac754

Browse files
committed
Rework example
1 parent e77bcab commit 4cac754

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

utils/website/graphile-config/middleware.md

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -184,70 +184,80 @@ something along these lines (replacing `libraryName` with the name of your
184184
library):
185185

186186
```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-
195187
/***** interfaces.ts *****/
196-
export type PromiseOrDirect<T> = Promise<T> | T;
197188

198189
// Define the middlewares that you support, their event type and their return type
199-
interface MyMiddleware {
190+
export interface MyMiddleware {
200191
someAction(event: SomeActionEvent): PromiseOrDirect<SomeActionResult>;
201192
}
202193
interface SomeActionEvent {
203-
someParameter: unknown;
194+
someParameter: number;
204195
/*
205196
* Use a per-middleware-method interface to define the various pieces of
206197
* data relevant to this event. **ALWAYS** use the event as an abstraction
207198
* so that new information can be added in future without causing any
208199
* 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.
210202
*/
211203
}
212204
// 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;
228206

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);
230237

231238
// Then in the relevant place in your code, call the middleware around the
232239
// relevant functionality
233240
const result = await middleware.run(
234241
"someAction",
235-
{
236-
/* event details here */
237-
someParameter: 42,
238-
},
242+
{ someParameter: 42 }, // < `event` object
239243
async (event) => {
244+
// Call the underlying method to perform the action.
240245
// Note: `event` will be the same object as above, but its contents may
241-
// have been modified.
246+
// have been modified by middlewares.
242247
return await someAction(event.someParameter);
243248
},
244249
);
245-
// The value of `result` should match the return value of `doSomeAction`
250+
// The value of `result` should match the return value of `someAction(...)`
246251
// (unless a middleware tweaked or replaced it, of course!)
247252

248253
// This is the thing that your middleware wraps. It can do anything, it's just
249254
// an arbitrary JavaScript function.
250255
function someAction(someParameter: number): PromiseOrDirect<SomeActionResult> {
251256
// Do something here...
257+
if (Math.random() < 0.5) {
258+
return someParameter;
259+
} else {
260+
return sleep(200).then(() => someParameter);
261+
}
252262
}
253263
```

0 commit comments

Comments
 (0)