Gathering detailed insights and metrics for @manifico/graphql-upload-ts-fixed
Gathering detailed insights and metrics for @manifico/graphql-upload-ts-fixed
Gathering detailed insights and metrics for @manifico/graphql-upload-ts-fixed
Gathering detailed insights and metrics for @manifico/graphql-upload-ts-fixed
Forked to fix error: graphql-upload-ts does not allow calling createReadStream() multiple times. Please, consume the previously returned stream. Make sure you're not referencing same file twice in your query.
npm install @manifico/graphql-upload-ts-fixed
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (96.45%)
JavaScript (3.55%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
893 Commits
2 Branches
1 Contributors
Updated on Jun 29, 2023
Latest Version
0.0.5
Package Id
@manifico/graphql-upload-ts-fixed@0.0.5
Unpacked Size
61.86 kB
Size
16.71 kB
File Count
27
NPM Version
8.11.0
Node Version
16.15.1
Published on
Jul 01, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
23
Minimalistic and developer friendly middleware and an Upload
scalar to add support for GraphQL multipart requests (file uploads via queries and
mutations) to various Node.js GraphQL servers.
This module was forked from the amazing graphql-upload-minimal
. The original module is exceptionally well documented and well written. It was very easy to fork and amend.
I needed to support typescript to use it properly in typescript projects.
busboy
node_modules
.Using ASCII-only text. Direct developers to resolve common mistakes.
You can't have same file referenced twice in a GraphQL query/mutation.
graphql-upload
createReadStream()
. Will throw if any provided.createReadStream()
more than once per file is not allowed. Will throw.Otherwise, this module is a drop-in replacement for the graphql-upload
.
The following environments are known to be compatible:
See also GraphQL multipart request spec server implementations.
To install graphql-upload-ts
and the graphql
peer dependency from npm run:
1npm install graphql-upload-ts graphql 2# or 3yarn add graphql-upload-ts graphql
Use the graphqlUploadKoa
or graphqlUploadExpress
middleware just before GraphQL middleware. Alternatively, use processRequest
to create a
custom middleware.
A schema built with separate SDL and resolvers (e.g. using makeExecutableSchema
) 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.
Minimalistic code example showing how to upload a file along with arbitrary GraphQL data and save it to an S3 bucket.
Express.js middleware. You must put it before the main GraphQL sever middleware. Also, make sure there is no other Express.js middleware which parses multipart/form-data
HTTP requests before the graphqlUploadExpress
middleware!
1const express = require('express'); 2const expressGraphql = require('express-graphql'); 3const { graphqlUploadExpress } = require('graphql-upload-ts'); 4 5express() 6 .use( 7 '/graphql', 8 graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }), 9 expressGraphql({ schema: require('./my-schema') }) 10 ) 11 .listen(3000);
GraphQL schema:
1scalar Upload 2input DocumentUploadInput { 3 docType: String! 4 file: Upload! 5} 6 7type SuccessResult { 8 success: Boolean! 9 message: String 10} 11type Mutations { 12 uploadDocuments(docs: [DocumentUploadInput!]!): SuccessResult 13}
GraphQL resolvers:
1const { S3 } = require('aws-sdk'); 2 3const resolvers = { 4 Upload: require('graphql-upload-ts').GraphQLUpload, 5 6 Mutations: { 7 async uploadDocuments(root, { docs }, ctx) { 8 try { 9 const s3 = new S3({ apiVersion: '2006-03-01', params: { Bucket: 'my-bucket' } }); 10 11 for (const doc of docs) { 12 const { createReadStream, filename /*, fieldName, mimetype, encoding */ } = await doc.file; 13 const Key = `${ctx.user.id}/${doc.docType}-${filename}`; 14 await s3.upload({ Key, Body: createReadStream() }).promise(); 15 } 16 17 return { success: true }; 18 } catch (error) { 19 console.log('File upload failed', error); 20 return { success: false, message: error.message }; 21 } 22 }, 23 }, 24};
See the example Koa server and client.
Reported to be working.
1const { processRequest } = require('graphql-upload-ts'); 2 3module.exports.processRequest = function (event) { 4 return processRequest(event, null, { environment: 'lambda' }); 5};
Possible example. Experimental. Untested.
1const { processRequest } = require('graphql-upload-ts'); 2 3exports.uploadFile = function (req, res) { 4 return processRequest(req, res, { environment: 'gcf' }); 5};
Possible example. Working.
1const { processRequest } = require('graphql-upload-ts'); 2 3exports.uploadFile = function (context, req) { 4 return processRequest(context, req, { environment: 'azure' }); 5};
When uploading multiple files you can make use of the fieldName
property to keep track of an identifier of the uploaded files. The fieldName is equal to the passed name
property of the file in the multipart/form-data
request. This can
be modified to contain an identifier (like a UUID), for example using the formDataAppendFile
in the commonly used apollo-upload-link
library.
GraphQL schema:
1scalar Upload 2input DocumentUploadInput { 3 docType: String! 4 files: [Upload!] 5} 6 7type SuccessResult { 8 success: Boolean! 9 message: String 10} 11type Mutations { 12 uploadDocuments(docs: [DocumentUploadInput!]!): SuccessResult 13}
GraphQL resolvers:
1const { S3 } = require('aws-sdk'); 2 3const resolvers = { 4 Upload: require('graphql-upload-ts').GraphQLUpload, 5 6 Mutations: { 7 async uploadDocuments(root, { docs }, ctx) { 8 try { 9 const s3 = new S3({ apiVersion: '2006-03-01', params: { Bucket: 'my-bucket' } }); 10 11 for (const doc of docs) { 12 // fieldName contains the "name" property from the multipart/form-data request. 13 // Use it to pass an identifier in order to store the file in a consistent manner. 14 const { createReadStream, filename, fieldName /*, mimetype, encoding */ } = await doc.file; 15 const Key = `${ctx.user.id}/${doc.docType}-${fieldName}`; 16 await s3.upload({ Key, Body: createReadStream() }).promise(); 17 } 18 19 return { success: true }; 20 } catch (error) { 21 console.log('File upload failed', error); 22 return { success: false, message: error.message }; 23 } 24 }, 25 }, 26};
createReadStream()
before the resolver returns; late calls (e.g. in an unawaited async function or callback) throw an error.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.
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.
No vulnerabilities found.
No security vulnerabilities found.