You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transform Uploads to Streams before calling action (#71)
Update the upload feature to transform object resolved from GraphQL upload to a stream before calling the resolution action. This allows uploads to work properly across the transporter.
Copy file name to clipboardExpand all lines: README.md
+104Lines changed: 104 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -258,6 +258,107 @@ module.exports = {
258
258
};
259
259
```
260
260
261
+
### File Uploads
262
+
moleculer-apollo-server supports file uploads through the [GraphQL multipart request specification](https://github.com/jaydenseric/graphql-multipart-request-spec).
263
+
264
+
To enable uploads, the Upload scalar must be added to the Gateway:
Then a mutation can be created which accepts an Upload argument. The `fileUploadArg` property must be set to the mutation's argument name so that moleculer-apollo-server knows where to expect a file upload. When the mutation's action handler is called, `ctx.params` will be a [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams) which can be used to read the contents of the uploaded file (or pipe the contents into a Writable Stream). File metadata will be made available in `ctx.meta.$fileInfo`.
312
+
313
+
**files.service.js**
314
+
```js
315
+
module.exports= {
316
+
name:"files",
317
+
settings: {
318
+
graphql: {
319
+
type:`
320
+
"""
321
+
This type describes a File entity.
322
+
"""
323
+
type File {
324
+
filename: String!
325
+
encoding: String!
326
+
mimetype: String!
327
+
}
328
+
`
329
+
}
330
+
},
331
+
actions: {
332
+
uploadFile: {
333
+
graphql: {
334
+
mutation:"uploadFile(file: Upload!): File!",
335
+
fileUploadArg:"file",
336
+
},
337
+
asynchandler(ctx) {
338
+
constfileChunks= [];
339
+
forawait (constchunkofctx.params) {
340
+
fileChunks.push(chunk);
341
+
}
342
+
constfileContents=Buffer.concat(fileChunks);
343
+
// Do something with file contents
344
+
returnctx.meta.$fileInfo;
345
+
}
346
+
}
347
+
}
348
+
};
349
+
```
350
+
351
+
To accept multiple uploaded files in a single request, the mutation can be changed to accept an array of `Upload`s and return an array of results. The action handler will then be called once for each uploaded file, and the results will be combined into an array automatically with results in the same order as the provided files.
352
+
353
+
```js
354
+
...
355
+
graphql: {
356
+
mutation:"upload(file: [Upload!]!): [File!]!",
357
+
fileUploadArg:"file"
358
+
}
359
+
...
360
+
```
361
+
261
362
### Dataloader
262
363
moleculer-apollo-server supports [DataLoader](https://github.com/graphql/dataloader) via configuration in the resolver definition.
263
364
The called action must be compatible with DataLoader semantics -- that is, it must accept params with an array property and return an array of the same size,
@@ -312,6 +413,9 @@ It is unlikely that setting any of the options which accept a function will work
312
413
313
414
- [Simple](examples/simple/index.js)
314
415
- `npm run dev`
416
+
- [File Upload](examples/upload/index.js)
417
+
- `npm run dev upload`
418
+
- See [here](https://github.com/jaydenseric/graphql-multipart-request-spec#curl-request) for information about how to create a file upload request
0 commit comments