Skip to content

Commit 7d7b025

Browse files
committed
fix(visitAllArgs): resolve serious issue reconstructing args
update internal detail to be less surpising: use object rather than two-element array update tests improve package.json dependencies
1 parent 39b0bff commit 7d7b025

File tree

7 files changed

+98
-73
lines changed

7 files changed

+98
-73
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
{ "type": "node",
7+
{
8+
"type": "node",
89
"request": "launch",
910
"name": "Run AVA test",
1011
"program": "${workspaceRoot}/node_modules/ava/profile.js",

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@
1414
"author": "Tim Gerk <[email protected]>, Matic Zavadlal <[email protected]>",
1515
"scripts": {
1616
"prepublish": "npm run test",
17-
"build": "rm -rf dist && tsc -d",
18-
"lint": "tslint --project tsconfig.json {src}/**/*.ts && prettier-check --ignore-path .gitignore {src,.}/{*.ts,*.js}",
1917
"test": "npm run lint && npm run build && npm run test-ava",
18+
"lint": "tslint --project tsconfig.json {src}/**/*.ts && prettier-check --ignore-path .gitignore src{,/**}/{*.ts,*.js}",
19+
"build": "rm -rf dist && tsc -d",
2020
"test-ava": "ava --verbose",
2121
"semantic-release": "semantic-release",
22-
"pretty": "npx prettier --write --ignore-path .gitignore {src,.}/{*.ts,*.js}"
22+
"pretty": "npx prettier --ignore-path .gitignore src{,/**}/{*.ts,*.js} --write"
2323
},
2424
"dependencies": {
25-
"graphql-middleware": "^1.4.2"
25+
"graphql-middleware": "1.7.8",
26+
"graphql-upload": "^8.0.1"
2627
},
2728
"devDependencies": {
2829
"@types/graphql": "0.13.4",
2930
"ava": "0.25.0",
30-
"graphql": "0.13.2",
31-
"graphql-middleware": "1.7.8",
32-
"graphql-tools": "4.0.2",
31+
"graphql": "^0.13.2",
3332
"prettier": "1.14.3",
3433
"prettier-check": "2.0.0",
3534
"semantic-release": "15.8.1",
@@ -40,7 +39,6 @@
4039
"typescript": "2.9.2"
4140
},
4241
"peerDependencies": {
43-
"apollo-upload-server": "^5.0.0",
4442
"graphql": "^0.13.2"
4543
},
4644
"release": {

src/index.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function getArgumentValue(
9090

9191
// Helper --------------------------------------------------------------------
9292

93-
declare type IHandledArg<T> = [string, T | T[]]
93+
declare type IHandledArg<T> = { [key: string]: T | T[] }
9494

9595
/**
9696
*
@@ -137,19 +137,19 @@ export function makeArgumentTransform<T, V>(
137137
},
138138
[],
139139
),
140-
).then((res: T[]): IHandledArg<T> => [argumentName, res])
140+
).then((newValue: T[]): IHandledArg<T> => ({ [argumentName]: newValue }))
141141
}
142142

143143
if (argumentValue !== undefined && argumentValue !== null) {
144144
if (argumentValue instanceof Promise) {
145145
return argumentValue
146146
.then(e => transform(e, parent, args, ctx, info))
147-
.then((res: T): IHandledArg<T> => [argumentName, res])
147+
.then((newValue: T): IHandledArg<T> => ({ [argumentName]: newValue }))
148148
}
149149

150150
return Promise.resolve(
151151
transform(argumentValue, parent, args, ctx, info),
152-
).then((res: T): IHandledArg<T> => [argumentName, res])
152+
).then((newValue: T): IHandledArg<T> => ({ [argumentName]: newValue }))
153153
}
154154

155155
return null // exclude arguments when no value provided
@@ -199,11 +199,8 @@ export function processTypeArgs<V, T>({
199199
),
200200
)
201201
// substitute the transformed values into the args object
202-
.then(result =>
203-
result.reduce(
204-
(args, [name, newValue]) => { args[name] = newValue; return args },
205-
args,
206-
),
202+
.then(newArgs =>
203+
newArgs.reduce((args, newArg) => ({ ...args, ...newArg }), args),
207204
)
208205
.then(newArgs => resolve(parent, newArgs, ctx, info))
209206
)
@@ -213,27 +210,24 @@ export function processTypeArgs<V, T>({
213210
}
214211
}
215212

216-
export function visitAllArgs({ visitor }): IMiddlewareFunction {
213+
export function visitAllArgs({ transform }): IMiddlewareFunction {
217214
return (resolve, parent, args, ctx, info) => {
218215
const argDefs = getFieldArguments(info)
219216
if (argDefs.length) {
220217
// Apply argument transform function to all arguments
221-
// The caller's visitor function is applied to values that may be embedded
218+
// The caller's transform function is applied to values that may be embedded
222219
// in lists and promises, but not to null or undefined values
223220
// Finally the resolver is called with transformed argument values
224221
return (
225222
Promise.all(
226223
filterMap(
227-
makeArgumentTransform(visitor, parent, args, ctx, info),
224+
makeArgumentTransform(transform, parent, args, ctx, info),
228225
argDefs,
229226
),
230227
)
231228
// substitute the transformed values into the args object
232-
.then(result =>
233-
result.reduce(
234-
(args, [name, newValue]) => (args[name] = newValue),
235-
args,
236-
),
229+
.then(newArgs =>
230+
newArgs.reduce((args, newArg) => ({ ...args, ...newArg }), args),
237231
)
238232
.then(newArgs => resolve(parent, newArgs, ctx, info))
239233
)

src/test/index.test.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava'
22
import { makeExecutableSchema } from 'graphql-tools'
33
import { applyMiddleware } from 'graphql-middleware'
4-
import { GraphQLUpload } from 'apollo-upload-server'
4+
import { GraphQLUpload } from 'graphql-upload'
55
import { graphql, GraphQLList, GraphQLString, GraphQLScalarType } from 'graphql'
66
import {
77
findArgumentsOfType,
@@ -316,14 +316,14 @@ test('Processor handles single file correctly', async t => {
316316
let args = {
317317
test: new Promise(resolve =>
318318
resolve({ stream: 's', filename: 'f', mimetype: 'm', encoding: 'e' }),
319-
)
319+
),
320320
}
321321
const res = await makeArgumentTransform(uploadHandler, {}, args, {}, {})({
322322
name: 'test',
323323
type: GraphQLUpload,
324324
})
325325

326-
t.deepEqual(res, ['test', 'sfme'])
326+
t.deepEqual(res, { test: 'sfme' })
327327
})
328328

329329
test('Processor handles multiple files correctly', async t => {
@@ -346,14 +346,14 @@ test('Processor handles multiple files correctly', async t => {
346346
)
347347

348348
const args = {
349-
test: [file(1), file(2)]
349+
test: [file(1), file(2)],
350350
}
351351
const res = await makeArgumentTransform(uploadHandler, {}, args, {}, {})({
352352
name: 'test',
353353
type: new GraphQLList(GraphQLUpload),
354354
})
355355

356-
t.deepEqual(res, ['test', ['s1f1m1e1', 's2f2m2e2']])
356+
t.deepEqual(res, { test: ['s1f1m1e1', 's2f2m2e2'] })
357357
})
358358

359359
test('Processor handles empty files correctly', async t => {
@@ -376,14 +376,14 @@ test('Processor handles empty files correctly', async t => {
376376
)
377377

378378
const args = {
379-
test: [file(1), null, undefined]
379+
test: [file(1), null, undefined],
380380
}
381381
const res = await makeArgumentTransform(uploadHandler, {}, args, {}, {})({
382382
name: 'test',
383383
type: new GraphQLList(GraphQLUpload),
384384
})
385385

386-
t.deepEqual(res, ['test', ['s1f1m1e1']])
386+
t.deepEqual(res, { test: ['s1f1m1e1'] })
387387
})
388388

389389
test('Processor handles no file correctly', async t => {
@@ -395,7 +395,7 @@ test('Processor handles no file correctly', async t => {
395395
),
396396
)
397397
const args = {
398-
test: null
398+
test: null,
399399
}
400400
const res = await makeArgumentTransform(uploadHandler, {}, args, {}, {})({
401401
name: 'test',
@@ -405,11 +405,11 @@ test('Processor handles no file correctly', async t => {
405405
t.is(res, null)
406406
})
407407

408-
test('processTypeArgs applies a transformations correctly', async t => {
408+
test('processTypeArgs applies a transformation correctly', async t => {
409409
// write a schema, an arg transform, and a query
410410
t.plan(5)
411411

412-
let theValue = "some-random-string"
412+
let theValue = 'some-random-string'
413413

414414
const typeDefs = `
415415
scalar Custom
@@ -422,26 +422,30 @@ test('processTypeArgs applies a transformations correctly', async t => {
422422
const resolvers = {
423423
Query: {
424424
test: (parent, args, ctx, infp) => {
425-
t.deepEqual(args, { pass: String('-'+theValue+'-').toUpperCase() })
425+
t.deepEqual(args, { pass: String('-' + theValue + '-').toUpperCase() })
426426
return String(args.pass).split('-')
427-
}
427+
},
428428
},
429429
Custom: new GraphQLScalarType({
430430
name: 'Custom',
431431
serialize: x => x,
432432
parseValue: x => x,
433433
parseLiteral: x => {
434-
t.is(x['value'], theValue); return `-${x['value']}-`
435-
}
434+
t.is(x['value'], theValue)
435+
return `-${x['value']}-`
436+
},
436437
}),
437438
}
438439

439440
const middleware = {
440441
Query: {
441-
test: processTypeArgs({ type: 'Custom', transform: x=> {
442-
t.is(x, '-'+theValue+'-');
443-
return Promise.resolve(String(x).toUpperCase())
444-
}})
442+
test: processTypeArgs({
443+
type: 'Custom',
444+
transform: x => {
445+
t.is(x, '-' + theValue + '-')
446+
return Promise.resolve(String(x).toUpperCase())
447+
},
448+
}),
445449
},
446450
}
447451

@@ -456,6 +460,10 @@ test('processTypeArgs applies a transformations correctly', async t => {
456460

457461
// Execution
458462
const res = await graphql(schema, query)
459-
console.log(res)
460-
t.deepEqual(res.data.test, String('-'+theValue+'-').toUpperCase().split('-'))
463+
t.deepEqual(
464+
res.data.test,
465+
String('-' + theValue + '-')
466+
.toUpperCase()
467+
.split('-'),
468+
)
461469
})

src/upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLUpload } from 'apollo-upload-server'
1+
import { GraphQLUpload } from 'graphql-upload'
22
import { processTypeArgs } from './index'
33
import { IMiddlewareFunction } from 'graphql-middleware'
44

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"outDir": "./dist",
88
"sourceMap": true,
99
"lib": ["dom", "es2017", "esnext.asynciterable"],
10-
"experimentalDecorators": true,
11-
"watch": true
10+
"experimentalDecorators": true
1211
},
1312
"exclude": ["node_modules"]
1413
}

0 commit comments

Comments
 (0)