gum

graphql-upload-minimal

Minimalistic 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.

Showing:

Popularity

Downloads/wk

238

GitHub Stars

13

Maintenance

Last Commit

1mo ago

Contributors

11

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

graphql-upload-minimal

npm version CI status

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.

Acknowledgements

This module was ⚠️ forked from the amazing graphql-upload. The original module is exceptionally well documented and well written. It was very easy to fork and amend. Thanks Jayden!

I needed something simpler which won't attempt doing any disk I/O. There were no server-side JavaScript alternative modules for GraphQL file uploads. Thus, this fork was born.

Differences to graphql-upload

Single production dependency - busboy

  • Results in 9 less production dependencies.
  • And 6 less MB in your node_modules.
  • And using a bit less memory.
  • And a bit faster.
  • Most importantly, less risk that one of the dependencies would break your server.

More standard and developer friendly exception messages

Using ASCII-only text. Direct developers to resolve common mistakes.

Does not create any temporary files on disk

  • Thus works faster.
  • Does not have a risk of clogging your file system. Even on high load.
  • No need to manually destroy the programmatically aborted streams.

Does not follow strict specification

You can't have same file referenced twice in a GraphQL query/mutation.

API changes comparing to the original graphql-upload

  • Does not accept any arguments to createReadStream(). Will throw if any provided.
  • Calling createReadStream() more than once per file is not allowed. Will throw.

Otherwise, this module is a drop-in replacement for the graphql-upload.

Support

The following environments are known to be compatible:

See also GraphQL multipart request spec server implementations.

Setup

To install graphql-upload-minimal and the graphql peer dependency from npm run:

npm install graphql-upload-minimal 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.

Usage

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.

Express.js

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!

const express = require("express");
const expressGraphql = require("express-graphql");
const { graphqlUploadExpress } = require("graphql-upload-minimal");

express()
  .use(
    "/graphql",
    graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
    expressGraphql({ schema: require("./my-schema") })
  )
  .listen(3000);

GraphQL schema:

scalar Upload
input DocumentUploadInput {
  docType: String!
  file: Upload!
}

type SuccessResult {
  success: Boolean!
  message: String
}
type Mutations {
  uploadDocuments(docs: [DocumentUploadInput!]!): SuccessResult
}

GraphQL resolvers:

const { S3 } = require("aws-sdk"); 

const resolvers = {
  Upload: require("graphql-upload-minimal").GraphQLUpload,

  Mutations: {
    async uploadDocuments(root, { docs }, ctx) {
      try {
        const s3 = new S3({ apiVersion: "2006-03-01", params: { Bucket: "my-bucket" } });

        for (const doc of docs) {
          const { createReadStream, filename /*, mimetype, encoding */ } = await doc.file;
          const Key = `${ctx.user.id}/${doc.docType}-${filename}`;
          await s3.upload({ Key, Body: createReadStream() }).promise();
        }

        return { success: true };
      } catch (error) {
        console.log("File upload failed", error);
        return { success: false, message: error.message };
      }
    }
  }
};

Koa

See the example Koa server and client.

AWS Lambda

Reported to be working.

const { processRequest } = require("graphql-upload-minimal");

module.exports.processRequest = function (event) {
  return processRequest(event, null, { environment: "lambda" });
};

Google Cloud Functions (GCF)

Possible example. Experimental. Untested.

const { processRequest } = require("graphql-upload-minimal");

exports.uploadFile = function (req, res) {
  return processRequest(req, res, { environment: "gcf" });
};

Azure Functions

Possible example. Working.

const { processRequest } = require("graphql-upload-minimal");

exports.uploadFile = function (context, req) {
  return processRequest(context, req, { environment: "azure" });
};

Tips

  • Only use createReadStream() before the resolver returns; late calls (e.g. in an unawaited async function or callback) throw an error.

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.

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.

API

Table of contents

class GraphQLUpload

A GraphQL Upload scalar that can be used in a GraphQLSchema. It's value in resolvers is a promise that resolves file upload details for processing and storage.

Examples

Ways to import.

import { GraphQLUpload } from 'graphql-upload-minimal';
import GraphQLUpload from 'graphql-upload-minimal/public/GraphQLUpload.js';

Ways to require.

const { GraphQLUpload } = require('graphql-upload-minimal');
const GraphQLUpload = require('graphql-upload-minimal/public/GraphQLUpload');

Setup for a schema built with makeExecutableSchema.

const { makeExecutableSchema } = require('graphql-tools');
const { GraphQLUpload } = require('graphql-upload-minimal');

const schema = makeExecutableSchema({
  typeDefs: /* GraphQL */ `
    scalar Upload
  `,
  resolvers: {
    Upload: GraphQLUpload,
  },
});

A manually constructed schema with an image upload mutation.

const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLBoolean,
} = require('graphql');
const { GraphQLUpload } = require('graphql-upload-minimal');

const schema = new GraphQLSchema({
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      uploadImage: {
        description: 'Uploads an image.',
        type: GraphQLBoolean,
        args: {
          image: {
            description: 'Image file.',
            type: GraphQLUpload,
          },
        },
        async resolve(parent, { image }) {
          const { filename, mimetype, createReadStream } = await image;
          const stream = createReadStream();
          // Promisify the stream and store the file, then…
          return true;
        },
      },
    },
  }),
});

class Upload

A file expected to be uploaded as it has been declared in the map field of a GraphQL multipart request. The processRequest function places references to an instance of this class wherever the file is expected in the GraphQL operation. The Upload scalar derives it's value from the promise property.

Examples

Ways to import.

import { Upload } from 'graphql-upload-minimal';
import Upload from 'graphql-upload-minimal/public/Upload.js';

Ways to require.

const { Upload } = require('graphql-upload-minimal');
const Upload = require('graphql-upload-minimal/public/Upload');

Upload instance method reject

Rejects the upload promise with an error. This should only be utilized by processRequest.

ParameterTypeDescription
errorobjectError instance.

Upload instance method resolve

Resolves the upload promise with the file upload details. This should only be utilized by processRequest.

ParameterTypeDescription
fileFileUploadFile upload details.

Upload instance property file

The file upload details, available when the upload promise resolves. This should only be utilized by processRequest.

Type: undefined | FileUpload

Upload instance property promise

Promise that resolves file upload details. This should only be utilized by GraphQLUpload.

Type: Promise<FileUpload>


function graphqlUploadExpress

Creates Express middleware that processes GraphQL multipart requests using processRequest, ignoring non-multipart requests. It sets the request body to be similar to a conventional GraphQL POST request for following GraphQL middleware to consume.

ParameterTypeDescription
optionsProcessRequestOptionsMiddleware options. Any ProcessRequestOptions can be used.
options.processRequestProcessRequestFunction? = processRequestUsed to process GraphQL multipart requests.

Returns: Function — Express middleware.

Examples

Ways to import.

import { graphqlUploadExpress } from 'graphql-upload-minimal';
import graphqlUploadExpress from 'graphql-upload-minimal/public/graphqlUploadExpress.js';

Ways to require.

const { graphqlUploadExpress } = require('graphql-upload-minimal');
const graphqlUploadExpress = require('graphql-upload-minimal/public/graphqlUploadExpress');

Basic express-graphql setup.

const express = require('express');
const graphqlHTTP = require('express-graphql');
const { graphqlUploadExpress } = require('graphql-upload-minimal');
const schema = require('./schema');

express()
  .use(
    '/graphql',
    graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
    graphqlHTTP({ schema })
  )
  .listen(3000);

function graphqlUploadKoa

Creates Koa middleware that processes GraphQL multipart requests using processRequest, ignoring non-multipart requests. It sets the request body to be similar to a conventional GraphQL POST request for following GraphQL middleware to consume.

ParameterTypeDescription
optionsProcessRequestOptionsMiddleware options. Any ProcessRequestOptions can be used.
options.processRequestProcessRequestFunction? = processRequestUsed to process GraphQL multipart requests.

Returns: Function — Koa middleware.

Examples

Ways to import.

import { graphqlUploadKoa } from 'graphql-upload-minimal';
import graphqlUploadKoa from 'graphql-upload-minimal/public/graphqlUploadKoa.js';

Ways to require.

const { graphqlUploadKoa } = require('graphql-upload-minimal');
const graphqlUploadKoa = require('graphql-upload-minimal/public/graphqlUploadKoa');

Basic graphql-api-koa setup.

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const { errorHandler, execute } = require('graphql-api-koa');
const { graphqlUploadKoa } = require('graphql-upload-minimal');
const schema = require('./schema');

new Koa()
  .use(errorHandler())
  .use(bodyParser())
  .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
  .use(execute({ schema }))
  .listen(3000);

function processRequest

Processes a GraphQL multipart request. It parses the operations and map fields to create an Upload instance for each expected file upload, placing references wherever the file is expected in the GraphQL operation for the Upload scalar to derive its value. Error objects have HTTP status property and an appropriate HTTP error name property.

Type: ProcessRequestFunction

Examples

Ways to import.

import { processRequest } from 'graphql-upload-minimal';
import processRequest from 'graphql-upload-minimal/public/processRequest.js';

Ways to require.

const { processRequest } = require('graphql-upload-minimal');
const processRequest = require('graphql-upload-minimal/public/processRequest');

type FileUpload

File upload details that are only available after the file's field in the GraphQL multipart request has begun streaming in.

Type: object

PropertyTypeDescription
filenamestringFile name.
mimetypestringFile MIME type. Provided by the client and can't be trusted.
encodingstringFile stream transfer encoding.
createReadStreamFileUploadCreateReadStreamCreates a Node.js readable stream of the file's contents, for processing and storage.

type FileUploadCreateReadStream

Creates a Node.js readable stream of an uploading file's contents, for processing and storage. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request.

Type: Function

Returns: Readable — Node.js readable stream of the file's contents.

See


type GraphQLOperation

A GraphQL operation object in a shape that can be consumed and executed by most GraphQL servers.

Type: object

PropertyTypeDescription
querystringGraphQL document containing queries and fragments.
operationNamestring | null?GraphQL document operation name to execute.
variablesobject | null?GraphQL document operation variables and values map.

See


type ProcessRequestFunction

Processes a GraphQL multipart request.

Type: Function

ParameterTypeDescription
reqIncomingMessageNode.js HTTP server request instance.
resServerResponseNode.js HTTP server response instance.
optionsProcessRequestOptions?Options for processing the request.

Returns: Promise<GraphQLOperation | Array<GraphQLOperation>> — GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).

See


type ProcessRequestOptions

Options for processing a GraphQL multipart request.

Type: object

PropertyTypeDescription
maxFieldSizenumber? = 1000000Maximum allowed non-file multipart form field size in bytes; enough for your queries.
maxFileSizenumber? = InfinityMaximum allowed file size in bytes.
maxFilesnumber? = InfinityMaximum allowed number of files.
environmentstring?Valid value are: "lambda" (AWS Lambda), "gcf" (Google Cloud Functions), "azure" (Azure Functions). Set this if you are running the file uploads in serverless environment.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100