Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

jaydenseric/graphql-upload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-upload logo

graphql-upload

Middleware and a scalarUpload to add support forGraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.

Clients implementing the GraphQL multipart request spec upload files asUpload 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

Tip

First, check if there areGraphQL multipart request spec server implementations (most for Node.js integrategraphql-upload) that are more suitable for your environment than a manual setup.

To installgraphql-upload and its peer dependencygraphql withnpm, run:

npm install graphql-upload graphql

Use the middlewaregraphqlUploadKoa orgraphqlUploadExpress just before GraphQL middleware. Alternatively, use the functionprocessRequest to create custom middleware.

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

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

Examples

Tips

  • The process must have both read and write access to the directory identified byos.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 withPromise.all or a more flexible solution such asPromise.allSettled where an error in one does not reject them all.
  • Only use the functioncreateReadStreambefore 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.
  • Usestream.destroy() when an incomplete stream is no longer needed, or temporary files may not get cleaned up.

Architecture

TheGraphQL 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 theoperations andmap fields have been parsed, scalarUpload 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 functioncreateReadStream. 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^18.18.0 || ^20.9.0 || >=22.0.0.

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

Exports

Thenpm packagegraphql-upload featuresoptimal JavaScript module design. It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via thepackage.json fieldexports:

About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp