graphql-upload 12.0.0 → 15.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/GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js +12 -0
- package/GraphQLUpload.js +80 -0
- package/Upload.js +55 -0
- package/changelog.md +76 -0
- package/graphqlUploadExpress.js +80 -0
- package/graphqlUploadKoa.js +69 -0
- package/ignoreStream.js +17 -0
- package/license.md +9 -0
- package/package.json +51 -40
- package/processRequest.js +427 -0
- package/readme.md +20 -385
- package/private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js +0 -12
- package/private/ignoreStream.js +0 -16
- package/public/GraphQLUpload.js +0 -90
- package/public/Upload.js +0 -75
- package/public/graphqlUploadExpress.js +0 -76
- package/public/graphqlUploadKoa.js +0 -69
- package/public/index.js +0 -75
- package/public/index.mjs +0 -5
- package/public/processRequest.js +0 -361
package/readme.md
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://npm.im/graphql-upload) [](https://github.com/jaydenseric/graphql-upload/actions)
|
|
6
6
|
|
|
7
|
-
Middleware and an [`Upload`](
|
|
7
|
+
Middleware and an [`Upload`](./GraphQLUpload.js) 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.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Installation
|
|
10
10
|
|
|
11
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.
|
|
12
12
|
|
|
@@ -16,409 +16,44 @@ To install [`graphql-upload`](https://npm.im/graphql-upload) and the [`graphql`]
|
|
|
16
16
|
npm install graphql-upload graphql
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
Use the [`graphqlUploadKoa`](
|
|
19
|
+
Use the [`graphqlUploadKoa`](./graphqlUploadKoa.js) or [`graphqlUploadExpress`](./graphqlUploadExpress.js) middleware just before GraphQL middleware. Alternatively, use [`processRequest`](./processRequest.js) to create custom middleware.
|
|
20
20
|
|
|
21
|
-
A schema built with separate SDL and resolvers (e.g. using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/
|
|
21
|
+
A schema built with separate SDL and resolvers (e.g. using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema_src#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema)) requires the [`Upload`](./GraphQLUpload.js) scalar to be setup.
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
-
[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`](./GraphQLUpload.js) scalar query or mutation variables. Their resolver values are promises that resolve file upload details for processing and storage. Files are typically streamed into cloud storage but may also be stored in the filesystem.
|
|
26
26
|
|
|
27
27
|
See the [example API and client](https://github.com/jaydenseric/apollo-upload-examples).
|
|
28
28
|
|
|
29
29
|
### Tips
|
|
30
30
|
|
|
31
|
-
- The process must have both read and write access to the directory identified by [`os.tmpdir()`](https://nodejs.org/api/os.html#
|
|
31
|
+
- The process must have both read and write access to the directory identified by [`os.tmpdir()`](https://nodejs.org/api/os.html#ostmpdir).
|
|
32
32
|
- The device requires sufficient disk space to buffer the expected number of concurrent upload requests.
|
|
33
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.
|
|
34
34
|
- Handle file upload promise rejection and stream errors; uploads sometimes fail due to network connectivity issues or impatient users disconnecting.
|
|
35
|
-
- Process multiple uploads asynchronously with [`Promise.all`](https://developer.mozilla.org/en-US/docs/
|
|
36
|
-
- Only use
|
|
37
|
-
- Use [`stream.destroy()`](https://nodejs.org/api/stream.html#
|
|
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.
|
|
36
|
+
- Only use the function `createReadStream` _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.
|
|
37
|
+
- Use [`stream.destroy()`](https://nodejs.org/api/stream.html#readabledestroyerror) when an incomplete stream is no longer needed, or temporary files may not get cleaned up.
|
|
38
38
|
|
|
39
39
|
## Architecture
|
|
40
40
|
|
|
41
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.
|
|
42
42
|
|
|
43
|
-
[`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`](./GraphQLUpload.js) scalar values in the GraphQL operations are populated with promises, and the operations are passed down the middleware chain to GraphQL resolvers.
|
|
44
44
|
|
|
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
|
|
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 the function `createReadStream`. 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.
|
|
46
46
|
|
|
47
|
-
##
|
|
47
|
+
## Requirements
|
|
48
48
|
|
|
49
|
-
- [Node.js](https://nodejs.org): `^
|
|
49
|
+
- [Node.js](https://nodejs.org): `^14.17.0 || ^16.0.0 || >= 18.0.0`
|
|
50
50
|
|
|
51
|
-
##
|
|
51
|
+
## Exports
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
These CommonJS modules are published to [npm](https://npmjs.com) and exported via the [`package.json`](./package.json) `exports` field:
|
|
54
54
|
|
|
55
|
-
- [
|
|
56
|
-
- [
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
- [Upload instance property promise](#upload-instance-property-promise)
|
|
61
|
-
- [function graphqlUploadExpress](#function-graphqluploadexpress)
|
|
62
|
-
- [function graphqlUploadKoa](#function-graphqluploadkoa)
|
|
63
|
-
- [function processRequest](#function-processrequest)
|
|
64
|
-
- [type FileUpload](#type-fileupload)
|
|
65
|
-
- [type FileUploadCreateReadStream](#type-fileuploadcreatereadstream)
|
|
66
|
-
- [type GraphQLOperation](#type-graphqloperation)
|
|
67
|
-
- [type ProcessRequestFunction](#type-processrequestfunction)
|
|
68
|
-
- [type ProcessRequestOptions](#type-processrequestoptions)
|
|
69
|
-
|
|
70
|
-
### class GraphQLUpload
|
|
71
|
-
|
|
72
|
-
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](#type-fileupload) for processing and storage.
|
|
73
|
-
|
|
74
|
-
#### Examples
|
|
75
|
-
|
|
76
|
-
_Ways to `import`._
|
|
77
|
-
|
|
78
|
-
> ```js
|
|
79
|
-
> import { GraphQLUpload } from 'graphql-upload';
|
|
80
|
-
> ```
|
|
81
|
-
>
|
|
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
|
-
> ```
|
|
91
|
-
>
|
|
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');
|
|
101
|
-
>
|
|
102
|
-
> const schema = makeExecutableSchema({
|
|
103
|
-
> typeDefs: /* GraphQL */ `
|
|
104
|
-
> scalar Upload
|
|
105
|
-
> `,
|
|
106
|
-
> resolvers: {
|
|
107
|
-
> Upload: GraphQLUpload,
|
|
108
|
-
> },
|
|
109
|
-
> });
|
|
110
|
-
> ```
|
|
111
|
-
|
|
112
|
-
_A manually constructed schema with an image upload mutation._
|
|
113
|
-
|
|
114
|
-
> ```js
|
|
115
|
-
> const {
|
|
116
|
-
> GraphQLSchema,
|
|
117
|
-
> GraphQLObjectType,
|
|
118
|
-
> GraphQLBoolean,
|
|
119
|
-
> } = require('graphql');
|
|
120
|
-
> const { GraphQLUpload } = require('graphql-upload');
|
|
121
|
-
>
|
|
122
|
-
> const schema = new GraphQLSchema({
|
|
123
|
-
> mutation: new GraphQLObjectType({
|
|
124
|
-
> name: 'Mutation',
|
|
125
|
-
> fields: {
|
|
126
|
-
> uploadImage: {
|
|
127
|
-
> description: 'Uploads an image.',
|
|
128
|
-
> type: GraphQLBoolean,
|
|
129
|
-
> args: {
|
|
130
|
-
> image: {
|
|
131
|
-
> description: 'Image file.',
|
|
132
|
-
> type: GraphQLUpload,
|
|
133
|
-
> },
|
|
134
|
-
> },
|
|
135
|
-
> async resolve(parent, { image }) {
|
|
136
|
-
> const { filename, mimetype, createReadStream } = await image;
|
|
137
|
-
> const stream = createReadStream();
|
|
138
|
-
> // Promisify the stream and store the file, then…
|
|
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';
|
|
163
|
-
> ```
|
|
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
|
-
|
|
203
|
-
---
|
|
204
|
-
|
|
205
|
-
### function graphqlUploadExpress
|
|
206
|
-
|
|
207
|
-
Creates [Express](https://expressjs.com) middleware that processes [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
|
|
208
|
-
|
|
209
|
-
| Parameter | Type | Description |
|
|
210
|
-
| :-- | :-- | :-- |
|
|
211
|
-
| `options` | [ProcessRequestOptions](#type-processrequestoptions) | Middleware options. Any [`ProcessRequestOptions`](#type-processrequestoptions) can be used. |
|
|
212
|
-
| `options.processRequest` | [ProcessRequestFunction](#type-processrequestfunction)? = [processRequest](#function-processrequest) | Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec). |
|
|
213
|
-
|
|
214
|
-
**Returns:** Function — Express middleware.
|
|
215
|
-
|
|
216
|
-
#### Examples
|
|
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
|
-
|
|
238
|
-
_Basic [`express-graphql`](https://npm.im/express-graphql) setup._
|
|
239
|
-
|
|
240
|
-
> ```js
|
|
241
|
-
> const express = require('express');
|
|
242
|
-
> const graphqlHTTP = require('express-graphql');
|
|
243
|
-
> const { graphqlUploadExpress } = require('graphql-upload');
|
|
244
|
-
> const schema = require('./schema');
|
|
245
|
-
>
|
|
246
|
-
> express()
|
|
247
|
-
> .use(
|
|
248
|
-
> '/graphql',
|
|
249
|
-
> graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
|
|
250
|
-
> graphqlHTTP({ schema })
|
|
251
|
-
> )
|
|
252
|
-
> .listen(3000);
|
|
253
|
-
> ```
|
|
254
|
-
|
|
255
|
-
---
|
|
256
|
-
|
|
257
|
-
### function graphqlUploadKoa
|
|
258
|
-
|
|
259
|
-
Creates [Koa](https://koajs.com) middleware that processes [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
|
|
260
|
-
|
|
261
|
-
| Parameter | Type | Description |
|
|
262
|
-
| :-- | :-- | :-- |
|
|
263
|
-
| `options` | [ProcessRequestOptions](#type-processrequestoptions) | Middleware options. Any [`ProcessRequestOptions`](#type-processrequestoptions) can be used. |
|
|
264
|
-
| `options.processRequest` | [ProcessRequestFunction](#type-processrequestfunction)? = [processRequest](#function-processrequest) | Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec). |
|
|
265
|
-
|
|
266
|
-
**Returns:** Function — Koa middleware.
|
|
267
|
-
|
|
268
|
-
#### Examples
|
|
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
|
-
|
|
290
|
-
_Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup._
|
|
291
|
-
|
|
292
|
-
> ```js
|
|
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');
|
|
298
|
-
>
|
|
299
|
-
> new Koa()
|
|
300
|
-
> .use(errorHandler())
|
|
301
|
-
> .use(bodyParser())
|
|
302
|
-
> .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
|
|
303
|
-
> .use(execute({ schema }))
|
|
304
|
-
> .listen(3000);
|
|
305
|
-
> ```
|
|
306
|
-
|
|
307
|
-
---
|
|
308
|
-
|
|
309
|
-
### function processRequest
|
|
310
|
-
|
|
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.
|
|
312
|
-
|
|
313
|
-
**Type:** [ProcessRequestFunction](#type-processrequestfunction)
|
|
314
|
-
|
|
315
|
-
#### Examples
|
|
316
|
-
|
|
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`._
|
|
328
|
-
|
|
329
|
-
> ```js
|
|
330
|
-
> const { processRequest } = require('graphql-upload');
|
|
331
|
-
> ```
|
|
332
|
-
>
|
|
333
|
-
> ```js
|
|
334
|
-
> const processRequest = require('graphql-upload/public/processRequest.js');
|
|
335
|
-
> ```
|
|
336
|
-
|
|
337
|
-
---
|
|
338
|
-
|
|
339
|
-
### type FileUpload
|
|
340
|
-
|
|
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.
|
|
342
|
-
|
|
343
|
-
**Type:** object
|
|
344
|
-
|
|
345
|
-
| Property | Type | Description |
|
|
346
|
-
| :-- | :-- | :-- |
|
|
347
|
-
| `filename` | string | File name. |
|
|
348
|
-
| `mimetype` | string | File MIME type. Provided by the client and can’t be trusted. |
|
|
349
|
-
| `encoding` | string | File stream transfer encoding. |
|
|
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).
|
|
372
|
-
|
|
373
|
-
---
|
|
374
|
-
|
|
375
|
-
### type GraphQLOperation
|
|
376
|
-
|
|
377
|
-
A GraphQL operation object in a shape that can be consumed and executed by most GraphQL servers.
|
|
378
|
-
|
|
379
|
-
**Type:** object
|
|
380
|
-
|
|
381
|
-
| Property | Type | Description |
|
|
382
|
-
| :-- | :-- | :-- |
|
|
383
|
-
| `query` | string | GraphQL document containing queries and fragments. |
|
|
384
|
-
| `operationName` | string \| `null`? | GraphQL document operation name to execute. |
|
|
385
|
-
| `variables` | object \| `null`? | GraphQL document operation variables and values map. |
|
|
386
|
-
|
|
387
|
-
#### See
|
|
388
|
-
|
|
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).
|
|
391
|
-
|
|
392
|
-
---
|
|
393
|
-
|
|
394
|
-
### type ProcessRequestFunction
|
|
395
|
-
|
|
396
|
-
Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
397
|
-
|
|
398
|
-
**Type:** Function
|
|
399
|
-
|
|
400
|
-
| Parameter | Type | Description |
|
|
401
|
-
| :-- | :-- | :-- |
|
|
402
|
-
| `request` | IncomingMessage | [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage). |
|
|
403
|
-
| `response` | ServerResponse | [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse). |
|
|
404
|
-
| `options` | [ProcessRequestOptions](#type-processrequestoptions)? | Options for processing the request. |
|
|
405
|
-
|
|
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).
|
|
411
|
-
|
|
412
|
-
---
|
|
413
|
-
|
|
414
|
-
### type ProcessRequestOptions
|
|
415
|
-
|
|
416
|
-
Options for processing a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec); mostly relating to security, performance and limits.
|
|
417
|
-
|
|
418
|
-
**Type:** object
|
|
419
|
-
|
|
420
|
-
| Property | Type | Description |
|
|
421
|
-
| :-- | :-- | :-- |
|
|
422
|
-
| `maxFieldSize` | number? = `1000000` | Maximum allowed non-file multipart form field size in bytes; enough for your queries. |
|
|
423
|
-
| `maxFileSize` | number? = Infinity | Maximum allowed file size in bytes. |
|
|
424
|
-
| `maxFiles` | number? = Infinity | Maximum allowed number of files. |
|
|
55
|
+
- [`GraphQLUpload.js`](./GraphQLUpload.js)
|
|
56
|
+
- [`graphqlUploadExpress.js`](./graphqlUploadExpress.js)
|
|
57
|
+
- [`graphqlUploadKoa.js`](./graphqlUploadKoa.js)
|
|
58
|
+
- [`processRequest.js`](./processRequest.js)
|
|
59
|
+
- [`Upload.js`](./Upload.js)
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
5
|
-
* URL. Useful for error messages, etc.
|
|
6
|
-
* @kind constant
|
|
7
|
-
* @name GRAPHQL_MULTIPART_REQUEST_SPEC_URL
|
|
8
|
-
* @type {string}
|
|
9
|
-
* @ignore
|
|
10
|
-
*/
|
|
11
|
-
module.exports =
|
|
12
|
-
'https://github.com/jaydenseric/graphql-multipart-request-spec';
|
package/private/ignoreStream.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Safely ignores a Node.js readable stream.
|
|
5
|
-
* @kind function
|
|
6
|
-
* @name ignoreStream
|
|
7
|
-
* @param {ReadableStream} stream Node.js readable stream.
|
|
8
|
-
* @ignore
|
|
9
|
-
*/
|
|
10
|
-
module.exports = function ignoreStream(stream) {
|
|
11
|
-
// Prevent an unhandled error from crashing the process.
|
|
12
|
-
stream.on('error', () => {});
|
|
13
|
-
|
|
14
|
-
// Waste the stream.
|
|
15
|
-
stream.resume();
|
|
16
|
-
};
|
package/public/GraphQLUpload.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// One of the JSDoc code examples contains `*/` escaped using `*\/`, see:
|
|
2
|
-
// https://github.com/gajus/eslint-plugin-jsdoc/issues/710
|
|
3
|
-
/* eslint-disable jsdoc/check-examples */
|
|
4
|
-
|
|
5
|
-
'use strict';
|
|
6
|
-
|
|
7
|
-
const { GraphQLScalarType, GraphQLError } = require('graphql');
|
|
8
|
-
const Upload = require('./Upload');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A GraphQL `Upload` scalar that can be used in a
|
|
12
|
-
* [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema).
|
|
13
|
-
* It’s value in resolvers is a promise that resolves
|
|
14
|
-
* [file upload details]{@link FileUpload} for processing and storage.
|
|
15
|
-
* @kind class
|
|
16
|
-
* @name GraphQLUpload
|
|
17
|
-
* @example <caption>Ways to `import`.</caption>
|
|
18
|
-
* ```js
|
|
19
|
-
* import { GraphQLUpload } from 'graphql-upload';
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* ```js
|
|
23
|
-
* import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
|
|
24
|
-
* ```
|
|
25
|
-
* @example <caption>Ways to `require`.</caption>
|
|
26
|
-
* ```js
|
|
27
|
-
* const { GraphQLUpload } = require('graphql-upload');
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* ```js
|
|
31
|
-
* const GraphQLUpload = require('graphql-upload/public/GraphQLUpload.js');
|
|
32
|
-
* ```
|
|
33
|
-
* @example <caption>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).</caption>
|
|
34
|
-
* ```js
|
|
35
|
-
* const { makeExecutableSchema } = require('@graphql-tools/schema');
|
|
36
|
-
* const { GraphQLUpload } = require('graphql-upload');
|
|
37
|
-
*
|
|
38
|
-
* const schema = makeExecutableSchema({
|
|
39
|
-
* typeDefs: /* GraphQL *\/ `
|
|
40
|
-
* scalar Upload
|
|
41
|
-
* `,
|
|
42
|
-
* resolvers: {
|
|
43
|
-
* Upload: GraphQLUpload,
|
|
44
|
-
* },
|
|
45
|
-
* });
|
|
46
|
-
* ```
|
|
47
|
-
* @example <caption>A manually constructed schema with an image upload mutation.</caption>
|
|
48
|
-
* ```js
|
|
49
|
-
* const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require('graphql');
|
|
50
|
-
* const { GraphQLUpload } = require('graphql-upload');
|
|
51
|
-
*
|
|
52
|
-
* const schema = new GraphQLSchema({
|
|
53
|
-
* mutation: new GraphQLObjectType({
|
|
54
|
-
* name: 'Mutation',
|
|
55
|
-
* fields: {
|
|
56
|
-
* uploadImage: {
|
|
57
|
-
* description: 'Uploads an image.',
|
|
58
|
-
* type: GraphQLBoolean,
|
|
59
|
-
* args: {
|
|
60
|
-
* image: {
|
|
61
|
-
* description: 'Image file.',
|
|
62
|
-
* type: GraphQLUpload,
|
|
63
|
-
* },
|
|
64
|
-
* },
|
|
65
|
-
* async resolve(parent, { image }) {
|
|
66
|
-
* const { filename, mimetype, createReadStream } = await image;
|
|
67
|
-
* const stream = createReadStream();
|
|
68
|
-
* // Promisify the stream and store the file, then…
|
|
69
|
-
* return true;
|
|
70
|
-
* },
|
|
71
|
-
* },
|
|
72
|
-
* },
|
|
73
|
-
* }),
|
|
74
|
-
* });
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
module.exports = new GraphQLScalarType({
|
|
78
|
-
name: 'Upload',
|
|
79
|
-
description: 'The `Upload` scalar type represents a file upload.',
|
|
80
|
-
parseValue(value) {
|
|
81
|
-
if (value instanceof Upload) return value.promise;
|
|
82
|
-
throw new GraphQLError('Upload value invalid.');
|
|
83
|
-
},
|
|
84
|
-
parseLiteral(ast) {
|
|
85
|
-
throw new GraphQLError('Upload literal unsupported.', ast);
|
|
86
|
-
},
|
|
87
|
-
serialize() {
|
|
88
|
-
throw new GraphQLError('Upload serialization unsupported.');
|
|
89
|
-
},
|
|
90
|
-
});
|
package/public/Upload.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A file expected to be uploaded as it has been declared in the `map` field of
|
|
5
|
-
* a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
6
|
-
* The [`processRequest`]{@link processRequest} function places references to an
|
|
7
|
-
* instance of this class wherever the file is expected in the
|
|
8
|
-
* [GraphQL operation]{@link GraphQLOperation}. The
|
|
9
|
-
* [`Upload`]{@link GraphQLUpload} scalar derives it’s value from the
|
|
10
|
-
* [`promise`]{@link Upload#promise} property.
|
|
11
|
-
* @kind class
|
|
12
|
-
* @name Upload
|
|
13
|
-
* @example <caption>Ways to `import`.</caption>
|
|
14
|
-
* ```js
|
|
15
|
-
* import { Upload } from 'graphql-upload';
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* ```js
|
|
19
|
-
* import Upload from 'graphql-upload/public/Upload.js';
|
|
20
|
-
* ```
|
|
21
|
-
* @example <caption>Ways to `require`.</caption>
|
|
22
|
-
* ```js
|
|
23
|
-
* const { Upload } = require('graphql-upload');
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* ```js
|
|
27
|
-
* const Upload = require('graphql-upload/public/Upload.js');
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
module.exports = class Upload {
|
|
31
|
-
constructor() {
|
|
32
|
-
/**
|
|
33
|
-
* Promise that resolves file upload details. This should only be utilized
|
|
34
|
-
* by [`GraphQLUpload`]{@link GraphQLUpload}.
|
|
35
|
-
* @kind member
|
|
36
|
-
* @name Upload#promise
|
|
37
|
-
* @type {Promise<FileUpload>}
|
|
38
|
-
*/
|
|
39
|
-
this.promise = new Promise((resolve, reject) => {
|
|
40
|
-
/**
|
|
41
|
-
* Resolves the upload promise with the file upload details. This should
|
|
42
|
-
* only be utilized by [`processRequest`]{@link processRequest}.
|
|
43
|
-
* @kind function
|
|
44
|
-
* @name Upload#resolve
|
|
45
|
-
* @param {FileUpload} file File upload details.
|
|
46
|
-
*/
|
|
47
|
-
this.resolve = (file) => {
|
|
48
|
-
/**
|
|
49
|
-
* The file upload details, available when the
|
|
50
|
-
* [upload promise]{@link Upload#promise} resolves. This should only be
|
|
51
|
-
* utilized by [`processRequest`]{@link processRequest}.
|
|
52
|
-
* @kind member
|
|
53
|
-
* @name Upload#file
|
|
54
|
-
* @type {undefined|FileUpload}
|
|
55
|
-
*/
|
|
56
|
-
this.file = file;
|
|
57
|
-
|
|
58
|
-
resolve(file);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Rejects the upload promise with an error. This should only be
|
|
63
|
-
* utilized by [`processRequest`]{@link processRequest}.
|
|
64
|
-
* @kind function
|
|
65
|
-
* @name Upload#reject
|
|
66
|
-
* @param {object} error Error instance.
|
|
67
|
-
*/
|
|
68
|
-
this.reject = reject;
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// Prevent errors crashing Node.js, see:
|
|
72
|
-
// https://github.com/nodejs/node/issues/20392
|
|
73
|
-
this.promise.catch(() => {});
|
|
74
|
-
}
|
|
75
|
-
};
|