Skip to content

Commit 3df358e

Browse files
committed
feat(visitAllArgs): further generalization to all field arguments
Middleware returned from visitAllArgs({ visitor: (value: any): any }) will process all field args with a visitor function. My use case is for pre-fetch and authorization of backend models prior (especially) to mutation processing.
1 parent ba6843a commit 3df358e

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ node_modules
22
dist
33

44
.DS_Store
5-
*.log*.npmrc
5+
*.log*
6+
*.code-workspace
7+
.npmrc

src/index.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ export function processTypeArgs<V, T>({
185185
return (resolve, parent, args, ctx, info) => {
186186
const argDefs = findArgumentsOfType(type, info)
187187
if (argDefs.length) {
188-
// apply argument transform function to arguments of the selected type
189-
// the caller's transform function is applied to values that may be embedded in lists and promises
190-
// finally the resolver is called with transformed argument values
188+
// Apply argument transform function to arguments of the selected type
189+
// The caller's transform function is applied to values that may be embedded
190+
// in lists and promises, but not to null or undefined values
191+
// Finally the resolver is called with transformed argument values
191192
return (
192193
Promise.all(
193194
filterMap(
@@ -209,3 +210,55 @@ export function processTypeArgs<V, T>({
209210
return resolve(parent, args, ctx, info)
210211
}
211212
}
213+
214+
export function processArgs({ visitor }): IMiddlewareFunction {
215+
return (resolve, parent, args, ctx, info) => {
216+
const argDefs = getFieldArguments(info)
217+
if (argDefs.length) {
218+
// Apply argument transform function to all arguments
219+
// The caller's visitor function is applied to values that may be embedded
220+
// in lists and promises, but not to null or undefined values
221+
// Finally the resolver is called with transformed argument values
222+
return (
223+
Promise.all(
224+
filterMap(
225+
makeArgumentTransform(visitor, parent, args, ctx, info),
226+
argDefs,
227+
),
228+
)
229+
// substitute the transformed values into the args object
230+
.then(result =>
231+
result.reduce(
232+
(args, [name, newValue]) => (args[name] = newValue),
233+
args,
234+
),
235+
)
236+
.then(newArgs => resolve(parent, newArgs, ctx, info))
237+
)
238+
}
239+
240+
return resolve(parent, args, ctx, info)
241+
}
242+
}
243+
244+
// Preceding revision as a special case --------------------------------------
245+
246+
// this object shape is defined by Apollo Upload Server v5.0.0
247+
export interface IUploadFile {
248+
stream
249+
filename
250+
mimetype
251+
encoding
252+
}
253+
254+
declare type IUploadHandler<T> = (upload: IUploadFile) => Promise<T>
255+
256+
export interface IUploadConfig<T> {
257+
uploadHandler: IUploadHandler<T>
258+
}
259+
260+
export function update<T>({
261+
uploadHandler,
262+
}: IUploadConfig<T>): IMiddlewareFunction {
263+
return processTypeArgs({ type: 'Upload', transform: uploadHandler })
264+
}

0 commit comments

Comments
 (0)