|
|
10 月之前 | |
|---|---|---|
| .. | ||
| GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js | 10 月之前 | |
| GraphQLUpload.js | 10 月之前 | |
| Upload.js | 10 月之前 | |
| graphqlUploadExpress.js | 10 月之前 | |
| graphqlUploadKoa.js | 10 月之前 | |
| ignoreStream.js | 10 月之前 | |
| license.md | 10 月之前 | |
| package.json | 10 月之前 | |
| processRequest.js | 10 月之前 | |
| readme.md | 10 月之前 | |
Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
First, check if there are GraphQL multipart request spec server implementations (most for Node.js integrate graphql-upload) that are more suitable for your environment than a manual setup.
To install graphql-upload and the graphql peer dependency with npm, run:
npm install graphql-upload graphql
Use the graphqlUploadKoa or graphqlUploadExpress middleware just before GraphQL middleware. Alternatively, use processRequest to create custom middleware.
A schema built with separate SDL and resolvers (e.g. using makeExecutableSchema from @graphql-tools/schema) requires the Upload scalar to be setup.
Clients implementing the GraphQL multipart request spec upload files as Upload 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.
See the example API and client.
os.tmpdir().Promise.all or a more flexible solution such as Promise.allSettled where an error in one does not reject them all.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.stream.destroy() when an incomplete stream is no longer needed, or temporary files may not get cleaned up.The 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.
busboy parses multipart request streams. Once the operations and map fields have been parsed, Upload scalar values in the GraphQL operations are populated with promises, and the operations are passed down the middleware chain to GraphQL resolvers.
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.
^14.17.0 || ^16.0.0 || >= 18.0.0These CommonJS modules are published to npm and exported via the package.json exports field: