Skip to content

Commit

Permalink
Error on unreplaced Upload scalar
Browse files Browse the repository at this point in the history
This fixes #175.
  • Loading branch information
mike-marcacci committed Dec 23, 2019
1 parent 1146cf4 commit 13efccd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
5 changes: 4 additions & 1 deletion GraphQLUpload.js
Expand Up @@ -57,7 +57,10 @@ const { GraphQLScalarType } = require('graphql')
module.exports = new GraphQLScalarType({
name: 'Upload',
description: 'The `Upload` scalar type represents a file upload.',
parseValue: value => value,
parseValue: () =>
Promise.reject(
new Error('`Upload` scalars must have a corresponding multipart file.')
),
parseLiteral() {
throw new Error('‘Upload’ scalar literal unsupported.')
},
Expand Down
80 changes: 80 additions & 0 deletions GraphQLUpload.test.js
@@ -0,0 +1,80 @@
const {
GraphQLString,
GraphQLSchema,
GraphQLObjectType,
GraphQLBoolean,
execute,
parse
} = require('graphql')
const t = require('tap')
const GraphQLUpload = require('./GraphQLUpload')
const snapshotError = require('./test-helpers/snapshotError')

t.test('Missing entry for upload scalar.', async t => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
image: {
description: 'Uploads an image.',
type: GraphQLString,
args: {
name: {
description: 'Image name.',
type: GraphQLString
}
},
async resolve(parent, { image }) {
t.type(image, Promise, 'Upload must be a Promise')
try {
await image
throw new Error('Upload must throw!')
} catch (error) {
t.matchSnapshot(snapshotError(error), 'Middleware throws.')
}

return true
}
}
}
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
uploadImage: {
description: 'Uploads an image.',
type: GraphQLBoolean,
args: {
image: {
description: 'Image file.',
type: GraphQLUpload
}
},
async resolve(parent, { image }) {
t.type(image, Promise, 'Upload must be a Promise')
try {
await image
throw new Error('Upload must throw!')
} catch (error) {
t.matchSnapshot(snapshotError(error), 'Middleware throws.')
}

return true
}
}
}
})
})

await execute({
schema,
document: parse(
`mutation ($image: Upload!) {
uploadImage(image: $image)
}`
),
rootValue: {},
contextValue: {},
variableValues: { image: 'this is invalid' }
})
})
13 changes: 13 additions & 0 deletions tap-snapshots/GraphQLUpload.test.js-TAP.test.js
@@ -0,0 +1,13 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`GraphQLUpload.test.js TAP Missing entry for upload scalar. > Middleware throws. 1`] = `
{
"name": "Error",
"message": "\`Upload\` scalars must have a corresponding multipart file."
}
`

0 comments on commit 13efccd

Please sign in to comment.