graphql-upload 8.1.0 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +119 -1
- package/package.json +45 -43
- package/private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js +12 -0
- package/private/ignoreStream.js +16 -0
- package/public/GraphQLUpload.js +90 -0
- package/public/Upload.js +75 -0
- package/public/graphqlUploadExpress.js +76 -0
- package/public/graphqlUploadKoa.js +69 -0
- package/public/index.js +75 -0
- package/public/index.mjs +5 -0
- package/public/processRequest.js +361 -0
- package/readme.md +218 -69
- package/lib/GraphQLUpload.js +0 -21
- package/lib/GraphQLUpload.mjs +0 -14
- package/lib/constants.js +0 -6
- package/lib/constants.mjs +0 -2
- package/lib/graphqlUploadExpress.js +0 -34
- package/lib/graphqlUploadExpress.mjs +0 -26
- package/lib/graphqlUploadKoa.js +0 -27
- package/lib/graphqlUploadKoa.mjs +0 -19
- package/lib/ignoreStream.js +0 -11
- package/lib/ignoreStream.mjs +0 -4
- package/lib/index.js +0 -20
- package/lib/index.mjs +0 -4
- package/lib/isEnumerableObject.js +0 -9
- package/lib/isEnumerableObject.mjs +0 -2
- package/lib/processRequest.js +0 -318
- package/lib/processRequest.mjs +0 -286
package/readme.md
CHANGED
|
@@ -4,44 +4,25 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://npm.im/graphql-upload) [](https://github.com/jaydenseric/graphql-upload/actions)
|
|
6
6
|
|
|
7
|
-
Middleware and an [`Upload`
|
|
8
|
-
|
|
9
|
-
⚠️ Previously published as [`apollo-upload-server`](https://npm.im/apollo-upload-server).
|
|
10
|
-
|
|
11
|
-
## Support
|
|
12
|
-
|
|
13
|
-
The following environments are known to be compatible:
|
|
14
|
-
|
|
15
|
-
- Node.js v8.5+
|
|
16
|
-
- CJS
|
|
17
|
-
- Native ESM with [`--experimental-modules`](https://nodejs.org/api/esm.html#esm_enabling)
|
|
18
|
-
- [Koa](https://koajs.com)
|
|
19
|
-
- [`graphql-api-koa`](https://npm.im/graphql-api-koa)
|
|
20
|
-
- [`koa-graphql`](https://npm.im/koa-graphql)
|
|
21
|
-
- [`apollo-server-koa`](https://npm.im/apollo-server-koa) (inbuilt)
|
|
22
|
-
- [Express](https://expressjs.com)
|
|
23
|
-
- [`express-graphql`](https://npm.im/express-graphql)
|
|
24
|
-
- [`apollo-server-express`](https://npm.im/apollo-server-express) (inbuilt)
|
|
25
|
-
|
|
26
|
-
See also [GraphQL multipart request spec server implementations](https://github.com/jaydenseric/graphql-multipart-request-spec#server).
|
|
7
|
+
Middleware and an [`Upload`](#class-graphqlupload) scalar to add support for [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) (file uploads via queries and mutations) to various Node.js GraphQL servers.
|
|
27
8
|
|
|
28
9
|
## Setup
|
|
29
10
|
|
|
30
|
-
|
|
11
|
+
First, check if there are [GraphQL multipart request spec server implementations](https://github.com/jaydenseric/graphql-multipart-request-spec#server) (most for Node.js integrate [`graphql-upload`](https://npm.im/graphql-upload)) that are more suitable for your environment than a manual setup.
|
|
31
12
|
|
|
32
|
-
To install [`graphql-upload`](https://npm.im/graphql-upload) and the [`graphql`](https://npm.im/graphql) peer dependency
|
|
13
|
+
To install [`graphql-upload`](https://npm.im/graphql-upload) and the [`graphql`](https://npm.im/graphql) peer dependency with [npm](https://npmjs.com/get-npm), run:
|
|
33
14
|
|
|
34
|
-
```
|
|
15
|
+
```sh
|
|
35
16
|
npm install graphql-upload graphql
|
|
36
17
|
```
|
|
37
18
|
|
|
38
19
|
Use the [`graphqlUploadKoa`](#function-graphqluploadkoa) or [`graphqlUploadExpress`](#function-graphqluploadexpress) middleware just before GraphQL middleware. Alternatively, use [`processRequest`](#function-processrequest) to create custom middleware.
|
|
39
20
|
|
|
40
|
-
A schema built with separate SDL and resolvers (e.g. using [`makeExecutableSchema`](https://
|
|
21
|
+
A schema built with separate SDL and resolvers (e.g. using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema)) requires the [`Upload`](#class-graphqlupload) scalar to be setup.
|
|
41
22
|
|
|
42
23
|
## Usage
|
|
43
24
|
|
|
44
|
-
[Clients implementing the GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec#client) upload files as [`Upload`
|
|
25
|
+
[Clients implementing the GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec#client) upload files as [`Upload`](#class-graphqlupload) scalar query or mutation variables. Their resolver values are promises that resolve [file upload details](#type-fileupload) for processing and storage. Files are typically streamed into cloud storage but may also be stored in the filesystem.
|
|
45
26
|
|
|
46
27
|
See the [example API and client](https://github.com/jaydenseric/apollo-upload-examples).
|
|
47
28
|
|
|
@@ -51,7 +32,7 @@ See the [example API and client](https://github.com/jaydenseric/apollo-upload-ex
|
|
|
51
32
|
- The device requires sufficient disk space to buffer the expected number of concurrent upload requests.
|
|
52
33
|
- Promisify and await file upload streams in resolvers or the server will send a response to the client before uploads are complete, causing a disconnect.
|
|
53
34
|
- Handle file upload promise rejection and stream errors; uploads sometimes fail due to network connectivity issues or impatient users disconnecting.
|
|
54
|
-
- Process multiple uploads asynchronously with [`Promise.all`](https://developer.mozilla.org/docs/web/javascript/reference/global_objects/promise/all) or a more flexible solution where an error in one does not reject them all.
|
|
35
|
+
- Process multiple uploads asynchronously with [`Promise.all`](https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/promise/all) or a more flexible solution such as [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) where an error in one does not reject them all.
|
|
55
36
|
- Only use [`createReadStream()`](#type-fileupload) _before_ the resolver returns; late calls (e.g. in an unawaited async function or callback) throw an error. Existing streams can still be used after a response is sent, although there are few valid reasons for not awaiting their completion.
|
|
56
37
|
- Use [`stream.destroy()`](https://nodejs.org/api/stream.html#stream_readable_destroy_error) when an incomplete stream is no longer needed, or temporary files may not get cleaned up.
|
|
57
38
|
|
|
@@ -59,19 +40,29 @@ See the [example API and client](https://github.com/jaydenseric/apollo-upload-ex
|
|
|
59
40
|
|
|
60
41
|
The [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec) allows a file to be used for multiple query or mutation variables (file deduplication), and for variables to be used in multiple places. GraphQL resolvers need to be able to manage independent file streams. As resolvers are executed asynchronously, it’s possible they will try to process files in a different order than received in the multipart request.
|
|
61
42
|
|
|
62
|
-
[`busboy`](https://npm.im/busboy) parses multipart request streams. Once the `operations` and `map` fields have been parsed, [`Upload`
|
|
43
|
+
[`busboy`](https://npm.im/busboy) parses multipart request streams. Once the `operations` and `map` fields have been parsed, [`Upload`](#class-graphqlupload) scalar values in the GraphQL operations are populated with promises, and the operations are passed down the middleware chain to GraphQL resolvers.
|
|
63
44
|
|
|
64
45
|
[`fs-capacitor`](https://npm.im/fs-capacitor) is used to buffer file uploads to the filesystem and coordinate simultaneous reading and writing. As soon as a file upload’s contents begins streaming, its data begins buffering to the filesystem and its associated promise resolves. GraphQL resolvers can then create new streams from the buffer by calling [`createReadStream()`](#type-fileupload). The buffer is destroyed once all streams have ended or closed and the server has responded to the request. Any remaining buffer files will be cleaned when the process exits.
|
|
65
46
|
|
|
47
|
+
## Support
|
|
48
|
+
|
|
49
|
+
- [Node.js](https://nodejs.org): `^12.20 || >= 14.13`
|
|
50
|
+
|
|
66
51
|
## API
|
|
67
52
|
|
|
68
53
|
### Table of contents
|
|
69
54
|
|
|
70
55
|
- [class GraphQLUpload](#class-graphqlupload)
|
|
56
|
+
- [class Upload](#class-upload)
|
|
57
|
+
- [Upload instance method reject](#upload-instance-method-reject)
|
|
58
|
+
- [Upload instance method resolve](#upload-instance-method-resolve)
|
|
59
|
+
- [Upload instance property file](#upload-instance-property-file)
|
|
60
|
+
- [Upload instance property promise](#upload-instance-property-promise)
|
|
71
61
|
- [function graphqlUploadExpress](#function-graphqluploadexpress)
|
|
72
62
|
- [function graphqlUploadKoa](#function-graphqluploadkoa)
|
|
73
63
|
- [function processRequest](#function-processrequest)
|
|
74
64
|
- [type FileUpload](#type-fileupload)
|
|
65
|
+
- [type FileUploadCreateReadStream](#type-fileuploadcreatereadstream)
|
|
75
66
|
- [type GraphQLOperation](#type-graphqloperation)
|
|
76
67
|
- [type ProcessRequestFunction](#type-processrequestfunction)
|
|
77
68
|
- [type ProcessRequestOptions](#type-processrequestoptions)
|
|
@@ -82,30 +73,53 @@ A GraphQL `Upload` scalar that can be used in a [`GraphQLSchema`](https://graphq
|
|
|
82
73
|
|
|
83
74
|
#### Examples
|
|
84
75
|
|
|
85
|
-
|
|
76
|
+
_Ways to `import`._
|
|
86
77
|
|
|
87
78
|
> ```js
|
|
88
|
-
> import {
|
|
89
|
-
>
|
|
79
|
+
> import { GraphQLUpload } from 'graphql-upload';
|
|
80
|
+
> ```
|
|
90
81
|
>
|
|
91
|
-
>
|
|
92
|
-
>
|
|
93
|
-
>
|
|
82
|
+
> ```js
|
|
83
|
+
> import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
|
|
84
|
+
> ```
|
|
85
|
+
|
|
86
|
+
_Ways to `require`._
|
|
87
|
+
|
|
88
|
+
> ```js
|
|
89
|
+
> const { GraphQLUpload } = require('graphql-upload');
|
|
90
|
+
> ```
|
|
94
91
|
>
|
|
95
|
-
>
|
|
96
|
-
>
|
|
97
|
-
>
|
|
92
|
+
> ```js
|
|
93
|
+
> const GraphQLUpload = require('graphql-upload/public/GraphQLUpload.js');
|
|
94
|
+
> ```
|
|
95
|
+
|
|
96
|
+
_A schema built using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema)._
|
|
97
|
+
|
|
98
|
+
> ```js
|
|
99
|
+
> const { makeExecutableSchema } = require('@graphql-tools/schema');
|
|
100
|
+
> const { GraphQLUpload } = require('graphql-upload');
|
|
98
101
|
>
|
|
99
|
-
>
|
|
102
|
+
> const schema = makeExecutableSchema({
|
|
103
|
+
> typeDefs: /* GraphQL */ `
|
|
104
|
+
> scalar Upload
|
|
105
|
+
> `,
|
|
106
|
+
> resolvers: {
|
|
107
|
+
> Upload: GraphQLUpload,
|
|
108
|
+
> },
|
|
109
|
+
> });
|
|
100
110
|
> ```
|
|
101
111
|
|
|
102
112
|
_A manually constructed schema with an image upload mutation._
|
|
103
113
|
|
|
104
114
|
> ```js
|
|
105
|
-
>
|
|
106
|
-
>
|
|
115
|
+
> const {
|
|
116
|
+
> GraphQLSchema,
|
|
117
|
+
> GraphQLObjectType,
|
|
118
|
+
> GraphQLBoolean,
|
|
119
|
+
> } = require('graphql');
|
|
120
|
+
> const { GraphQLUpload } = require('graphql-upload');
|
|
107
121
|
>
|
|
108
|
-
>
|
|
122
|
+
> const schema = new GraphQLSchema({
|
|
109
123
|
> mutation: new GraphQLObjectType({
|
|
110
124
|
> name: 'Mutation',
|
|
111
125
|
> fields: {
|
|
@@ -115,21 +129,77 @@ _A manually constructed schema with an image upload mutation._
|
|
|
115
129
|
> args: {
|
|
116
130
|
> image: {
|
|
117
131
|
> description: 'Image file.',
|
|
118
|
-
> type: GraphQLUpload
|
|
119
|
-
> }
|
|
132
|
+
> type: GraphQLUpload,
|
|
133
|
+
> },
|
|
120
134
|
> },
|
|
121
135
|
> async resolve(parent, { image }) {
|
|
122
|
-
> const { filename, mimetype, createReadStream } = await image
|
|
123
|
-
> const stream = createReadStream()
|
|
136
|
+
> const { filename, mimetype, createReadStream } = await image;
|
|
137
|
+
> const stream = createReadStream();
|
|
124
138
|
> // Promisify the stream and store the file, then…
|
|
125
|
-
> return true
|
|
126
|
-
> }
|
|
127
|
-
> }
|
|
128
|
-
> }
|
|
129
|
-
> })
|
|
130
|
-
> })
|
|
139
|
+
> return true;
|
|
140
|
+
> },
|
|
141
|
+
> },
|
|
142
|
+
> },
|
|
143
|
+
> }),
|
|
144
|
+
> });
|
|
145
|
+
> ```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
### class Upload
|
|
150
|
+
|
|
151
|
+
A file expected to be uploaded as it has been declared in the `map` field of a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). The [`processRequest`](#function-processrequest) function places references to an instance of this class wherever the file is expected in the [GraphQL operation](#type-graphqloperation). The [`Upload`](#class-graphqlupload) scalar derives it’s value from the [`promise`](#upload-instance-property-promise) property.
|
|
152
|
+
|
|
153
|
+
#### Examples
|
|
154
|
+
|
|
155
|
+
_Ways to `import`._
|
|
156
|
+
|
|
157
|
+
> ```js
|
|
158
|
+
> import { Upload } from 'graphql-upload';
|
|
159
|
+
> ```
|
|
160
|
+
>
|
|
161
|
+
> ```js
|
|
162
|
+
> import Upload from 'graphql-upload/public/Upload.js';
|
|
131
163
|
> ```
|
|
132
164
|
|
|
165
|
+
_Ways to `require`._
|
|
166
|
+
|
|
167
|
+
> ```js
|
|
168
|
+
> const { Upload } = require('graphql-upload');
|
|
169
|
+
> ```
|
|
170
|
+
>
|
|
171
|
+
> ```js
|
|
172
|
+
> const Upload = require('graphql-upload/public/Upload.js');
|
|
173
|
+
> ```
|
|
174
|
+
|
|
175
|
+
#### Upload instance method reject
|
|
176
|
+
|
|
177
|
+
Rejects the upload promise with an error. This should only be utilized by [`processRequest`](#function-processrequest).
|
|
178
|
+
|
|
179
|
+
| Parameter | Type | Description |
|
|
180
|
+
| :-------- | :----- | :-------------- |
|
|
181
|
+
| `error` | object | Error instance. |
|
|
182
|
+
|
|
183
|
+
#### Upload instance method resolve
|
|
184
|
+
|
|
185
|
+
Resolves the upload promise with the file upload details. This should only be utilized by [`processRequest`](#function-processrequest).
|
|
186
|
+
|
|
187
|
+
| Parameter | Type | Description |
|
|
188
|
+
| :-------- | :----------------------------- | :------------------- |
|
|
189
|
+
| `file` | [FileUpload](#type-fileupload) | File upload details. |
|
|
190
|
+
|
|
191
|
+
#### Upload instance property file
|
|
192
|
+
|
|
193
|
+
The file upload details, available when the [upload promise](#upload-instance-property-promise) resolves. This should only be utilized by [`processRequest`](#function-processrequest).
|
|
194
|
+
|
|
195
|
+
**Type:** `undefined` | [FileUpload](#type-fileupload)
|
|
196
|
+
|
|
197
|
+
#### Upload instance property promise
|
|
198
|
+
|
|
199
|
+
Promise that resolves file upload details. This should only be utilized by [`GraphQLUpload`](#class-graphqlupload).
|
|
200
|
+
|
|
201
|
+
**Type:** Promise<[FileUpload](#type-fileupload)>
|
|
202
|
+
|
|
133
203
|
---
|
|
134
204
|
|
|
135
205
|
### function graphqlUploadExpress
|
|
@@ -145,13 +215,33 @@ Creates [Express](https://expressjs.com) middleware that processes [GraphQL mult
|
|
|
145
215
|
|
|
146
216
|
#### Examples
|
|
147
217
|
|
|
218
|
+
_Ways to `import`._
|
|
219
|
+
|
|
220
|
+
> ```js
|
|
221
|
+
> import { graphqlUploadExpress } from 'graphql-upload';
|
|
222
|
+
> ```
|
|
223
|
+
>
|
|
224
|
+
> ```js
|
|
225
|
+
> import graphqlUploadExpress from 'graphql-upload/public/graphqlUploadExpress.js';
|
|
226
|
+
> ```
|
|
227
|
+
|
|
228
|
+
_Ways to `require`._
|
|
229
|
+
|
|
230
|
+
> ```js
|
|
231
|
+
> const { graphqlUploadExpress } = require('graphql-upload');
|
|
232
|
+
> ```
|
|
233
|
+
>
|
|
234
|
+
> ```js
|
|
235
|
+
> const graphqlUploadExpress = require('graphql-upload/public/graphqlUploadExpress.js');
|
|
236
|
+
> ```
|
|
237
|
+
|
|
148
238
|
_Basic [`express-graphql`](https://npm.im/express-graphql) setup._
|
|
149
239
|
|
|
150
240
|
> ```js
|
|
151
|
-
>
|
|
152
|
-
>
|
|
153
|
-
>
|
|
154
|
-
>
|
|
241
|
+
> const express = require('express');
|
|
242
|
+
> const graphqlHTTP = require('express-graphql');
|
|
243
|
+
> const { graphqlUploadExpress } = require('graphql-upload');
|
|
244
|
+
> const schema = require('./schema');
|
|
155
245
|
>
|
|
156
246
|
> express()
|
|
157
247
|
> .use(
|
|
@@ -159,7 +249,7 @@ _Basic [`express-graphql`](https://npm.im/express-graphql) setup._
|
|
|
159
249
|
> graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
|
|
160
250
|
> graphqlHTTP({ schema })
|
|
161
251
|
> )
|
|
162
|
-
> .listen(3000)
|
|
252
|
+
> .listen(3000);
|
|
163
253
|
> ```
|
|
164
254
|
|
|
165
255
|
---
|
|
@@ -177,44 +267,78 @@ Creates [Koa](https://koajs.com) middleware that processes [GraphQL multipart re
|
|
|
177
267
|
|
|
178
268
|
#### Examples
|
|
179
269
|
|
|
270
|
+
_Ways to `import`._
|
|
271
|
+
|
|
272
|
+
> ```js
|
|
273
|
+
> import { graphqlUploadKoa } from 'graphql-upload';
|
|
274
|
+
> ```
|
|
275
|
+
>
|
|
276
|
+
> ```js
|
|
277
|
+
> import graphqlUploadKoa from 'graphql-upload/public/graphqlUploadKoa.js';
|
|
278
|
+
> ```
|
|
279
|
+
|
|
280
|
+
_Ways to `require`._
|
|
281
|
+
|
|
282
|
+
> ```js
|
|
283
|
+
> const { graphqlUploadKoa } = require('graphql-upload');
|
|
284
|
+
> ```
|
|
285
|
+
>
|
|
286
|
+
> ```js
|
|
287
|
+
> const graphqlUploadKoa = require('graphql-upload/public/graphqlUploadKoa.js');
|
|
288
|
+
> ```
|
|
289
|
+
|
|
180
290
|
_Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup._
|
|
181
291
|
|
|
182
292
|
> ```js
|
|
183
|
-
>
|
|
184
|
-
>
|
|
185
|
-
>
|
|
186
|
-
>
|
|
187
|
-
>
|
|
293
|
+
> const Koa = require('koa');
|
|
294
|
+
> const bodyParser = require('koa-bodyparser');
|
|
295
|
+
> const { errorHandler, execute } = require('graphql-api-koa');
|
|
296
|
+
> const { graphqlUploadKoa } = require('graphql-upload');
|
|
297
|
+
> const schema = require('./schema');
|
|
188
298
|
>
|
|
189
299
|
> new Koa()
|
|
190
300
|
> .use(errorHandler())
|
|
191
301
|
> .use(bodyParser())
|
|
192
302
|
> .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
|
|
193
303
|
> .use(execute({ schema }))
|
|
194
|
-
> .listen(3000)
|
|
304
|
+
> .listen(3000);
|
|
195
305
|
> ```
|
|
196
306
|
|
|
197
307
|
---
|
|
198
308
|
|
|
199
309
|
### function processRequest
|
|
200
310
|
|
|
201
|
-
Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). Errors are created with [`http-errors`](https://npm.im/http-errors) to assist in sending responses with appropriate HTTP status codes. Used in [`graphqlUploadExpress`](#function-graphqluploadexpress) and [`graphqlUploadKoa`](#function-graphqluploadkoa) and can be used to create custom middleware.
|
|
311
|
+
Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). It parses the `operations` and `map` fields to create an [`Upload`](#class-upload) instance for each expected file upload, placing references wherever the file is expected in the [GraphQL operation](#type-graphqloperation) for the [`Upload`](#class-graphqlupload) scalar to derive it’s value. Errors are created with [`http-errors`](https://npm.im/http-errors) to assist in sending responses with appropriate HTTP status codes. Used in [`graphqlUploadExpress`](#function-graphqluploadexpress) and [`graphqlUploadKoa`](#function-graphqluploadkoa) and can be used to create custom middleware.
|
|
202
312
|
|
|
203
313
|
**Type:** [ProcessRequestFunction](#type-processrequestfunction)
|
|
204
314
|
|
|
205
315
|
#### Examples
|
|
206
316
|
|
|
207
|
-
|
|
317
|
+
_Ways to `import`._
|
|
318
|
+
|
|
319
|
+
> ```js
|
|
320
|
+
> import { processRequest } from 'graphql-upload';
|
|
321
|
+
> ```
|
|
322
|
+
>
|
|
323
|
+
> ```js
|
|
324
|
+
> import processRequest from 'graphql-upload/public/processRequest.js';
|
|
325
|
+
> ```
|
|
326
|
+
|
|
327
|
+
_Ways to `require`._
|
|
208
328
|
|
|
209
329
|
> ```js
|
|
210
|
-
>
|
|
330
|
+
> const { processRequest } = require('graphql-upload');
|
|
331
|
+
> ```
|
|
332
|
+
>
|
|
333
|
+
> ```js
|
|
334
|
+
> const processRequest = require('graphql-upload/public/processRequest.js');
|
|
211
335
|
> ```
|
|
212
336
|
|
|
213
337
|
---
|
|
214
338
|
|
|
215
339
|
### type FileUpload
|
|
216
340
|
|
|
217
|
-
File upload details
|
|
341
|
+
File upload details that are only available after the file’s field in the [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec) has begun streaming in.
|
|
218
342
|
|
|
219
343
|
**Type:** object
|
|
220
344
|
|
|
@@ -223,7 +347,28 @@ File upload details, resolved from an [`Upload` scalar](#class-graphqlupload) pr
|
|
|
223
347
|
| `filename` | string | File name. |
|
|
224
348
|
| `mimetype` | string | File MIME type. Provided by the client and can’t be trusted. |
|
|
225
349
|
| `encoding` | string | File stream transfer encoding. |
|
|
226
|
-
| `createReadStream` |
|
|
350
|
+
| `createReadStream` | [FileUploadCreateReadStream](#type-fileuploadcreatereadstream) | Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents, for processing and storage. |
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
### type FileUploadCreateReadStream
|
|
355
|
+
|
|
356
|
+
Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of an [uploading file’s](#type-fileupload) contents, for processing and storage. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request.
|
|
357
|
+
|
|
358
|
+
**Type:** Function
|
|
359
|
+
|
|
360
|
+
| Parameter | Type | Description |
|
|
361
|
+
| :-- | :-- | :-- |
|
|
362
|
+
| `options` | object? | [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions). |
|
|
363
|
+
| `options.encoding` | string? = `null` | Specify an encoding for the [`data`](https://nodejs.org/api/stream.html#stream_event_data) chunks to be strings (without splitting multi-byte characters across chunks) instead of Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) instances. Supported values depend on the [`Buffer` implementation](https://github.com/nodejs/node/blob/v13.7.0/lib/buffer.js#L587-L663) and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`, or `hex`. |
|
|
364
|
+
| `options.highWaterMark` | number? = `16384` | Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. |
|
|
365
|
+
|
|
366
|
+
**Returns:** Readable — [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.
|
|
367
|
+
|
|
368
|
+
#### See
|
|
369
|
+
|
|
370
|
+
- [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
|
|
371
|
+
- [Node.js stream backpressure guide](https://nodejs.org/en/docs/guides/backpressuring-in-streams).
|
|
227
372
|
|
|
228
373
|
---
|
|
229
374
|
|
|
@@ -241,8 +386,8 @@ A GraphQL operation object in a shape that can be consumed and executed by most
|
|
|
241
386
|
|
|
242
387
|
#### See
|
|
243
388
|
|
|
244
|
-
- [GraphQL over HTTP spec](https://github.com/
|
|
245
|
-
- [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests
|
|
389
|
+
- [GraphQL over HTTP spec](https://github.com/graphql/graphql-over-http).
|
|
390
|
+
- [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests/#post-requests).
|
|
246
391
|
|
|
247
392
|
---
|
|
248
393
|
|
|
@@ -258,7 +403,11 @@ Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-m
|
|
|
258
403
|
| `response` | ServerResponse | [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse). |
|
|
259
404
|
| `options` | [ProcessRequestOptions](#type-processrequestoptions)? | Options for processing the request. |
|
|
260
405
|
|
|
261
|
-
**Returns:** Promise
|
|
406
|
+
**Returns:** Promise<[GraphQLOperation](#type-graphqloperation) | Array<[GraphQLOperation](#type-graphqloperation)>> — GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).
|
|
407
|
+
|
|
408
|
+
#### See
|
|
409
|
+
|
|
410
|
+
- [`processRequest`](#function-processrequest).
|
|
262
411
|
|
|
263
412
|
---
|
|
264
413
|
|
package/lib/GraphQLUpload.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.GraphQLUpload = void 0
|
|
5
|
-
|
|
6
|
-
var _graphql = require('graphql')
|
|
7
|
-
|
|
8
|
-
const GraphQLUpload = new _graphql.GraphQLScalarType({
|
|
9
|
-
name: 'Upload',
|
|
10
|
-
description: 'The `Upload` scalar type represents a file upload.',
|
|
11
|
-
parseValue: value => value,
|
|
12
|
-
|
|
13
|
-
parseLiteral() {
|
|
14
|
-
throw new Error('‘Upload’ scalar literal unsupported.')
|
|
15
|
-
},
|
|
16
|
-
|
|
17
|
-
serialize() {
|
|
18
|
-
throw new Error('‘Upload’ scalar serialization unsupported.')
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
exports.GraphQLUpload = GraphQLUpload
|
package/lib/GraphQLUpload.mjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { GraphQLScalarType } from 'graphql'
|
|
2
|
-
export const GraphQLUpload = new GraphQLScalarType({
|
|
3
|
-
name: 'Upload',
|
|
4
|
-
description: 'The `Upload` scalar type represents a file upload.',
|
|
5
|
-
parseValue: value => value,
|
|
6
|
-
|
|
7
|
-
parseLiteral() {
|
|
8
|
-
throw new Error('‘Upload’ scalar literal unsupported.')
|
|
9
|
-
},
|
|
10
|
-
|
|
11
|
-
serialize() {
|
|
12
|
-
throw new Error('‘Upload’ scalar serialization unsupported.')
|
|
13
|
-
}
|
|
14
|
-
})
|
package/lib/constants.js
DELETED
package/lib/constants.mjs
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.graphqlUploadExpress = void 0
|
|
5
|
-
|
|
6
|
-
var _processRequest = require('./processRequest')
|
|
7
|
-
|
|
8
|
-
const graphqlUploadExpress = ({
|
|
9
|
-
processRequest = _processRequest.processRequest,
|
|
10
|
-
...processRequestOptions
|
|
11
|
-
} = {}) => (request, response, next) => {
|
|
12
|
-
if (!request.is('multipart/form-data')) return next()
|
|
13
|
-
const finished = new Promise(resolve => request.on('end', resolve))
|
|
14
|
-
const { send } = response
|
|
15
|
-
|
|
16
|
-
response.send = (...args) => {
|
|
17
|
-
finished.then(() => {
|
|
18
|
-
response.send = send
|
|
19
|
-
response.send(...args)
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
processRequest(request, response, processRequestOptions)
|
|
24
|
-
.then(body => {
|
|
25
|
-
request.body = body
|
|
26
|
-
next()
|
|
27
|
-
})
|
|
28
|
-
.catch(error => {
|
|
29
|
-
if (error.status && error.expose) response.status(error.status)
|
|
30
|
-
next(error)
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
exports.graphqlUploadExpress = graphqlUploadExpress
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { processRequest as defaultProcessRequest } from './processRequest'
|
|
2
|
-
export const graphqlUploadExpress = ({
|
|
3
|
-
processRequest = defaultProcessRequest,
|
|
4
|
-
...processRequestOptions
|
|
5
|
-
} = {}) => (request, response, next) => {
|
|
6
|
-
if (!request.is('multipart/form-data')) return next()
|
|
7
|
-
const finished = new Promise(resolve => request.on('end', resolve))
|
|
8
|
-
const { send } = response
|
|
9
|
-
|
|
10
|
-
response.send = (...args) => {
|
|
11
|
-
finished.then(() => {
|
|
12
|
-
response.send = send
|
|
13
|
-
response.send(...args)
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
processRequest(request, response, processRequestOptions)
|
|
18
|
-
.then(body => {
|
|
19
|
-
request.body = body
|
|
20
|
-
next()
|
|
21
|
-
})
|
|
22
|
-
.catch(error => {
|
|
23
|
-
if (error.status && error.expose) response.status(error.status)
|
|
24
|
-
next(error)
|
|
25
|
-
})
|
|
26
|
-
}
|
package/lib/graphqlUploadKoa.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.graphqlUploadKoa = void 0
|
|
5
|
-
|
|
6
|
-
var _processRequest = require('./processRequest')
|
|
7
|
-
|
|
8
|
-
const graphqlUploadKoa = ({
|
|
9
|
-
processRequest = _processRequest.processRequest,
|
|
10
|
-
...processRequestOptions
|
|
11
|
-
} = {}) => async (ctx, next) => {
|
|
12
|
-
if (!ctx.request.is('multipart/form-data')) return next()
|
|
13
|
-
const finished = new Promise(resolve => ctx.req.on('end', resolve))
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
ctx.request.body = await processRequest(
|
|
17
|
-
ctx.req,
|
|
18
|
-
ctx.res,
|
|
19
|
-
processRequestOptions
|
|
20
|
-
)
|
|
21
|
-
await next()
|
|
22
|
-
} finally {
|
|
23
|
-
await finished
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
exports.graphqlUploadKoa = graphqlUploadKoa
|
package/lib/graphqlUploadKoa.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { processRequest as defaultProcessRequest } from './processRequest'
|
|
2
|
-
export const graphqlUploadKoa = ({
|
|
3
|
-
processRequest = defaultProcessRequest,
|
|
4
|
-
...processRequestOptions
|
|
5
|
-
} = {}) => async (ctx, next) => {
|
|
6
|
-
if (!ctx.request.is('multipart/form-data')) return next()
|
|
7
|
-
const finished = new Promise(resolve => ctx.req.on('end', resolve))
|
|
8
|
-
|
|
9
|
-
try {
|
|
10
|
-
ctx.request.body = await processRequest(
|
|
11
|
-
ctx.req,
|
|
12
|
-
ctx.res,
|
|
13
|
-
processRequestOptions
|
|
14
|
-
)
|
|
15
|
-
await next()
|
|
16
|
-
} finally {
|
|
17
|
-
await finished
|
|
18
|
-
}
|
|
19
|
-
}
|
package/lib/ignoreStream.js
DELETED
package/lib/ignoreStream.mjs
DELETED
package/lib/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.graphqlUploadExpress = exports.graphqlUploadKoa = exports.processRequest = exports.GraphQLUpload = void 0
|
|
5
|
-
|
|
6
|
-
var _GraphQLUpload = require('./GraphQLUpload')
|
|
7
|
-
|
|
8
|
-
exports.GraphQLUpload = _GraphQLUpload.GraphQLUpload
|
|
9
|
-
|
|
10
|
-
var _processRequest = require('./processRequest')
|
|
11
|
-
|
|
12
|
-
exports.processRequest = _processRequest.processRequest
|
|
13
|
-
|
|
14
|
-
var _graphqlUploadKoa = require('./graphqlUploadKoa')
|
|
15
|
-
|
|
16
|
-
exports.graphqlUploadKoa = _graphqlUploadKoa.graphqlUploadKoa
|
|
17
|
-
|
|
18
|
-
var _graphqlUploadExpress = require('./graphqlUploadExpress')
|
|
19
|
-
|
|
20
|
-
exports.graphqlUploadExpress = _graphqlUploadExpress.graphqlUploadExpress
|
package/lib/index.mjs
DELETED