-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathGraphQLUpload.js
67 lines (66 loc) · 2.04 KB
/
GraphQLUpload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { GraphQLScalarType } = require('graphql')
/**
* A GraphQL `Upload` scalar that can be used in a [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema).
* It’s value in resolvers is a promise that resolves [file upload details]{@link FileUpload}
* for processing and storage.
* @kind class
* @name GraphQLUpload
* @example <caption>Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema).</caption>
* ```js
* const { makeExecutableSchema } = require('graphql-tools')
* const { GraphQLUpload } = require('graphql-upload')
*
* const schema = makeExecutableSchema({
* typeDefs: /* GraphQL *\/ `
* scalar Upload
* `,
* resolvers: {
* Upload: GraphQLUpload
* }
* })
* ```
* @example <caption>A manually constructed schema with an image upload mutation.</caption>
* ```js
* const {
* GraphQLSchema,
* GraphQLObjectType,
* GraphQLBoolean
* } = require('graphql')
* const { GraphQLUpload } = require('graphql-upload')
*
* const schema = new GraphQLSchema({
* mutation: new GraphQLObjectType({
* name: 'Mutation',
* fields: {
* uploadImage: {
* description: 'Uploads an image.',
* type: GraphQLBoolean,
* args: {
* image: {
* description: 'Image file.',
* type: GraphQLUpload
* }
* },
* async resolve(parent, { image }) {
* const { filename, mimetype, createReadStream } = await image
* const stream = createReadStream()
* // Promisify the stream and store the file, then…
* return true
* }
* }
* }
* })
* })
* ```
*/
module.exports = new GraphQLScalarType({
name: 'Upload',
description: 'The `Upload` scalar type represents a file upload.',
parseValue: value => value,
parseLiteral() {
throw new Error('‘Upload’ scalar literal unsupported.')
},
serialize() {
throw new Error('‘Upload’ scalar serialization unsupported.')
}
})