|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +title: "Library Authors" |
| 4 | +--- |
| 5 | + |
| 6 | +# Library Authors |
| 7 | + |
| 8 | +Graphile Config is currently only used by Graphile projects. Should you find it |
| 9 | +useful for other projects, please reach out via GitHub issues and we can discuss |
| 10 | +what is necessary to make this more universal. Should you decide to not heed |
| 11 | +this advice, please at least make sure that the scopes you add are named to |
| 12 | +reduce the likelihood of future conflicts with features we may wish to add. |
| 13 | + |
| 14 | +## Naming scopes |
| 15 | + |
| 16 | +By convention, scopes are camelCase strings. Scopes should be descriptive enough |
| 17 | +to reduce the chance of either conflicts across libraries or conflicts with |
| 18 | +future additions to Graphile Config. |
| 19 | + |
| 20 | +<details> |
| 21 | + |
| 22 | +<summary>Click for specifically reserved scope names</summary> |
| 23 | + |
| 24 | +The following strings are reserved by Graphile Config, and should not be used as |
| 25 | +preset scopes or plugin scopes: |
| 26 | + |
| 27 | +- Anything beginning with an underscore (`_`) |
| 28 | +- after |
| 29 | +- appendPlugins (to avoid confusion with PostGraphile v4 plugins) |
| 30 | +- before |
| 31 | +- callback |
| 32 | +- description |
| 33 | +- default (to enable compatibility with the various ESM emulations) |
| 34 | +- disablePlugins |
| 35 | +- experimental |
| 36 | +- export |
| 37 | +- exports |
| 38 | +- extend |
| 39 | +- extends |
| 40 | +- functionality |
| 41 | +- id |
| 42 | +- import |
| 43 | +- imports |
| 44 | +- include |
| 45 | +- includes |
| 46 | +- label |
| 47 | +- name |
| 48 | +- plugin |
| 49 | +- plugins |
| 50 | +- prependPlugins (to avoid confusion with PostGraphile v4 plugins) |
| 51 | +- provides |
| 52 | +- skipPlugins (to avoid confusion with PostGraphile v4 plugins) |
| 53 | +- title |
| 54 | + |
| 55 | +</details> |
| 56 | + |
| 57 | +## Middleware |
| 58 | + |
| 59 | +(This section was primarily written by Benjie for Benjie... so you may want to |
| 60 | +skip it.) |
| 61 | + |
| 62 | +If you need to create a middleware system for your library, you might follow |
| 63 | +something along these lines (replacing `libraryName` with the name of your |
| 64 | +library): |
| 65 | + |
| 66 | +```ts title="src/interfaces.ts" |
| 67 | +// Define the middlewares that you support, their event type and their return type |
| 68 | +export interface MyMiddleware { |
| 69 | + someAction(event: SomeActionEvent): PromiseOrDirect<SomeActionResult>; |
| 70 | +} |
| 71 | +interface SomeActionEvent { |
| 72 | + someParameter: number; |
| 73 | + /* |
| 74 | + * Use a per-middleware-method interface to define the various pieces of |
| 75 | + * data relevant to this event. **ALWAYS** use the event as an abstraction |
| 76 | + * so that new information can be added in future without causing any |
| 77 | + * knock-on consequences. Note that these parameters of the event may be |
| 78 | + * mutated. The values here can be anything, they don't need to be simple |
| 79 | + * values. |
| 80 | + */ |
| 81 | +} |
| 82 | +// Middleware wraps a function call; this represents whatever the function returns |
| 83 | +type SomeActionResult = number; |
| 84 | + |
| 85 | +export type PromiseOrDirect<T> = Promise<T> | T; |
| 86 | +``` |
| 87 | + |
| 88 | +```ts title="src/index.ts" |
| 89 | +import type { MiddlewareHandlers } from "graphile-config"; |
| 90 | + |
| 91 | +// Extend Plugin with support for registering handlers for the middleware activities: |
| 92 | +declare global { |
| 93 | + namespace GraphileConfig { |
| 94 | + interface Plugin { |
| 95 | + libraryName?: { |
| 96 | + middleware?: MiddlewareHandlers<MyMiddleware>; |
| 97 | + }; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +```ts title="src/getMiddleware.ts" |
| 104 | +import { Middleware, orderedApply, resolvePreset } from "graphile-config"; |
| 105 | + |
| 106 | +export function getMiddleware(resolvedPreset: GraphileConfig.ResolvedPreset) { |
| 107 | + // Create your middleware instance. The generic describes the events supported |
| 108 | + const middleware = new Middleware<MyMiddleware>(); |
| 109 | + // Now apply the relevant middlewares registered by each plugin (if any) to the |
| 110 | + // Middleware instance |
| 111 | + orderedApply( |
| 112 | + resolvedPreset.plugins, |
| 113 | + (plugin) => plugin.libraryName?.middleware, |
| 114 | + (name, fn, _plugin) => { |
| 115 | + middleware.register(name, fn as any); |
| 116 | + }, |
| 117 | + ); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +```ts title="src/main.ts" |
| 122 | +// Get the user's Graphile Config from somewhere, e.g. |
| 123 | +import config from "./graphile.config.js"; |
| 124 | + |
| 125 | +// Resolve the above config, recursively applying all the presets it extends from |
| 126 | +const resolvedPreset = resolvePreset(config); |
| 127 | + |
| 128 | +// Get the middleware for this preset |
| 129 | +const middleware = getMiddleware(resolvedPreset); |
| 130 | + |
| 131 | +// Then in the relevant place in your code, call the middleware around the |
| 132 | +// relevant functionality |
| 133 | +const result = await middleware.run( |
| 134 | + "someAction", |
| 135 | + { someParameter: 42 }, // < `event` object |
| 136 | + async (event) => { |
| 137 | + // Note: `event` will be the same object as above, but its contents may |
| 138 | + // have been modified by middlewares. |
| 139 | + const { someParameter } = event; |
| 140 | + |
| 141 | + // Call the underlying method to perform the action. |
| 142 | + return await someAction(someParameter); |
| 143 | + }, |
| 144 | +); |
| 145 | +// The value of `result` should match the return value of `someAction(...)` |
| 146 | +// (unless a middleware tweaked or replaced it, of course!) |
| 147 | + |
| 148 | +// This is the thing that your middleware wraps. It can do anything, it's just |
| 149 | +// an arbitrary JavaScript function. |
| 150 | +function someAction(someParameter: number): PromiseOrDirect<SomeActionResult> { |
| 151 | + // Do something here... |
| 152 | + if (Math.random() < 0.5) { |
| 153 | + return someParameter; |
| 154 | + } else { |
| 155 | + return sleep(200).then(() => someParameter); |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
0 commit comments