16.0.2 • Published 2 years ago

graphql-upload v16.0.2

Weekly downloads
1,571,474
License
MIT
Repository
github
Last release
2 years ago

graphql-upload logo

graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.

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.

Installation

Note

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 its graphql peer dependency with npm, run:

npm install graphql-upload graphql

Use the middleware graphqlUploadKoa or graphqlUploadExpress just before GraphQL middleware. Alternatively, use the function processRequest to create custom middleware.

A schema built with separate SDL and resolvers (e.g. using the function makeExecutableSchema from @graphql-tools/schema) requires the Upload scalar to be setup.

Then, the Upload scalar can be used for query or mutation arguments. For how to use the scalar value in resolvers, see the documentation in the module GraphQLUpload.mjs.

Examples

Tips

  • The process must have both read and write access to the directory identified by os.tmpdir().
  • The device requires sufficient disk space to buffer the expected number of concurrent upload requests.
  • 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.
  • Handle file upload promise rejection and stream errors; uploads sometimes fail due to network connectivity issues or impatient users disconnecting.
  • Process multiple uploads asynchronously with Promise.all or a more flexible solution such as Promise.allSettled where an error in one does not reject them all.
  • 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.
  • Use stream.destroy() when an incomplete stream is no longer needed, or temporary files may not get cleaned up.

Architecture

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.

Requirements

Supported runtime environments:

  • Node.js versions ^14.17.0 || ^16.0.0 || >= 18.0.0.

Projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check comment:

Exports

The npm package graphql-upload features optimal JavaScript module design. It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the package.json field exports:

@ckmk/nest-library@pubngo-stack/platform-server@pubngo-stack/account-api-server@iharob/graphql-mesh-climoonpay-api@aifedespaix/fdp-api@nuragic/apollo-server-corepage-metrics-root@sjungling/apollo-federation-upload@kaviar/apollo-bundle@leapllc/parse-server@shattercms/files@picker-cc/coreberserk-plugin-graphql@infinitebrahmanuniverse/nolb-graphql-uadonis-graphql-apollo@bytebean/core@leonbobster/graphql-faker@uponcommerce/corenestjs-media-manager@everything-registry/sub-chunk-1786@itoa/app-graphql@itoa/itoa@itoa/keystone@iasiakencheng/vendure-core@launchql/explorer@launchql/server@latech/moleculer-apollo-server@landingexp/apollo-server-core@mapoio/moleculer-apollo-server@lenne.tech/nest-server@ltv/moleculer-apollo-server@k6js/contrib-fields-azure@k6js/contrib-fields-s3@k6js/contrib-fields-s3-images@k6js/core@k6js/ks-next@keystone-next/app-graphql-legacy@keystone-next/cloudinary@keystone-next/keystone@keystone-next/keystone-legacy@keystonejs/app-graphql@keystone-next-brick/keystone@keystone-6/core@keystonejs/keystone@keystone-6-master/cloudinary@keystone-6-master/core@ishop/core@jobhopin/graphql@jaymun723/apollo-server-vercel@lukinco/adonis-apollo-server@grainrigi/apollo-federation-upload@gramps/gramps@graphmarket/helpers@metahop/graphqlidler-openapi-to-graphql@things-factory/provider-base-gql@things-factory/shell@things-factory/shell-service@unchainedshop/api@simbi/parse-server@sights/core@sigmast/core@things-real/shellinstashop-parse-server@tmatthias/boilerplate-api@toanz/strapi-plugin-graphql@things-shell/server@things-shell/board-api-gql@onemind/apiexample-complete-fastifyesperiroadmanfastgraph-nodefastify-gql-uploadfastify-gql-upload-ts@palpinter/moleculer-apollo-server@ocopjs/app-admin-ui@ocopjs/app-graphql@ocopjs/ocop@wwerner/openapi-to-graphql@x-team/strapi-plugin-graphql@workerhive/server@vendure/core@vmiroshnikov/cpc-utilskeystone-micro@wepublish/apivbcs-cloud-server@weave-js/apollo-graphql@ysos/parse-serverlee-fusion-plugin-apolloa-server-1-riga-server-test-rigwebql-tools-url-loaderacc-strapi-plugin-graphql@the_hashtag/common@tripzapp/shared-typingstraverse-apollo-server-core@tkuminecz/apollo-server-next@terion/easy-gql
16.0.2

2 years ago

15.0.2

2 years ago

15.0.0

2 years ago

15.0.1

2 years ago

14.0.0

2 years ago

16.0.1

2 years ago

16.0.0

2 years ago

13.0.0

2 years ago

12.0.0

3 years ago

11.0.0

4 years ago

10.0.0

4 years ago

9.0.0

4 years ago

8.1.0

4 years ago

8.0.7

5 years ago

8.0.6

5 years ago

8.0.5

5 years ago

8.0.4

5 years ago

8.0.3

5 years ago

8.0.2

5 years ago

8.0.1

5 years ago

8.0.0

6 years ago