All Projects → jaydenseric → Graphql Upload

jaydenseric / 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.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Graphql Upload

Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+924%)
Mutual labels:  graphql, express, koa
Blog Service
blog service @nestjs
Stars: ✭ 188 (-82.45%)
Mutual labels:  graphql, express, koa
Apollo Server
🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
Stars: ✭ 12,145 (+1033.99%)
Mutual labels:  graphql, express, koa
Apollo Upload Client
A terminating Apollo Link for Apollo Client that allows FileList, File, Blob or ReactNativeFile instances within query or mutation variables and sends GraphQL multipart requests.
Stars: ✭ 1,176 (+9.8%)
Mutual labels:  graphql, apollo, featured
Graphql Apq
🎯 Automatic persisted queries (APQ) for any GraphQL server.
Stars: ✭ 43 (-95.99%)
Mutual labels:  graphql, apollo, express
Boilerplate Vue Apollo Graphql Mongodb
Start your magical stack journey!
Stars: ✭ 85 (-92.06%)
Mutual labels:  graphql, apollo, express
Next React Graphql Apollo Hooks
React, Apollo, Next.js, GraphQL, Node.js, TypeScript high performance boilerplate with React hooks GraphQL implementation and automatic static type generation
Stars: ✭ 186 (-82.63%)
Mutual labels:  graphql, apollo, express
React Hipstaplate
A ReactJS full-stack boilerplate based on typescript with ssr, custom apollo-server and huge stack of modern utilities which will help you to start your own project
Stars: ✭ 74 (-93.09%)
Mutual labels:  graphql, apollo, express
Create Graphql
Command-line utility to build production-ready servers with GraphQL.
Stars: ✭ 441 (-58.82%)
Mutual labels:  graphql, express, koa
Graphql Ws
Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
Stars: ✭ 398 (-62.84%)
Mutual labels:  graphql, apollo, express
Erxes Api
API for erxes
Stars: ✭ 57 (-94.68%)
Mutual labels:  graphql, apollo, express
React Apollo Koa Example
An example app using React, Apollo and Koa
Stars: ✭ 26 (-97.57%)
Mutual labels:  graphql, apollo, koa
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (-85.81%)
Mutual labels:  graphql, apollo, express
Apollo Upload Examples
A full stack demo of file uploads via GraphQL mutations using Apollo Server and apollo-upload-client.
Stars: ✭ 358 (-66.57%)
Mutual labels:  graphql, apollo, koa
Hackernews React Graphql
Hacker News clone rewritten with universal JavaScript, using React and GraphQL.
Stars: ✭ 4,242 (+296.08%)
Mutual labels:  graphql, apollo, express
Omdb Graphql Wrapper
🚀 GraphQL wrapper for the OMDb API
Stars: ✭ 45 (-95.8%)
Mutual labels:  graphql, apollo, npm
React Apollo
♻️ React integration for Apollo Client
Stars: ✭ 6,932 (+547.25%)
Mutual labels:  graphql, apollo
Mvfsillva
My personal website
Stars: ✭ 13 (-98.79%)
Mutual labels:  graphql, apollo
Apollo Redux Form
Redux forms powered by Apollo
Stars: ✭ 52 (-95.14%)
Mutual labels:  graphql, apollo
Graphql Config
One configuration for all your GraphQL tools (supported by most tools, editors & IDEs)
Stars: ✭ 883 (-17.55%)
Mutual labels:  graphql, apollo

graphql-upload logo

graphql-upload

npm version CI status

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

⚠️ Previously published as apollo-upload-server.

Support

The following environments are known to be compatible:

See also GraphQL multipart request spec server implementations.

Setup

Setup is necessary if your environment doesn’t feature this package inbuilt (see Support).

To install graphql-upload and the graphql peer dependency from 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) 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.

See the example API and client.

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

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';
import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';

Ways to require.

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

Setup for a schema built with makeExecutableSchema.

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

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');

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';
import Upload from 'graphql-upload/public/Upload.js';

Ways to require.

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

Upload instance method reject

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

Parameter Type Description
error object Error instance.

Upload instance method resolve

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

Parameter Type Description
file FileUpload File 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.

Parameter Type Description
options ProcessRequestOptions Middleware options. Any ProcessRequestOptions can be used.
options.processRequest ProcessRequestFunction? = processRequest Used to process GraphQL multipart requests.

Returns: Function — Express middleware.

Examples

Ways to import.

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

Ways to require.

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

Basic express-graphql setup.

const express = require('express');
const graphqlHTTP = require('express-graphql');
const { graphqlUploadExpress } = require('graphql-upload');
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.

Parameter Type Description
options ProcessRequestOptions Middleware options. Any ProcessRequestOptions can be used.
options.processRequest ProcessRequestFunction? = processRequest Used to process GraphQL multipart requests.

Returns: Function — Koa middleware.

Examples

Ways to import.

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

Ways to require.

const { graphqlUploadKoa } = require('graphql-upload');
const graphqlUploadKoa = require('graphql-upload/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');
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 it’s value. Errors are created with http-errors to assist in sending responses with appropriate HTTP status codes. Used in graphqlUploadExpress and graphqlUploadKoa and can be used to create custom middleware.

Type: ProcessRequestFunction

Examples

Ways to import.

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

Ways to require.

const { processRequest } = require('graphql-upload');
const processRequest = require('graphql-upload/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

Property Type Description
filename string File name.
mimetype string File MIME type. Provided by the client and can’t be trusted.
encoding string File stream transfer encoding.
createReadStream FileUploadCreateReadStream Creates 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

Parameter Type Description
options object? fs-capacitor ReadStreamOptions.
options.encoding string? = null Specify an encoding for the data chunks to be strings (without splitting multi-byte characters across chunks) instead of Node.js Buffer instances. Supported values depend on the Buffer implementation and include utf8, ucs2, utf16le, latin1, ascii, base64, or hex.
options.highWaterMark number? = 16384 Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.

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

Property Type Description
query string GraphQL document containing queries and fragments.
operationName string | null? GraphQL document operation name to execute.
variables object | null? GraphQL document operation variables and values map.

See


type ProcessRequestFunction

Processes a GraphQL multipart request.

Type: Function

Parameter Type Description
request IncomingMessage Node.js HTTP server request instance.
response ServerResponse Node.js HTTP server response instance.
options ProcessRequestOptions? 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; mostly relating to security, performance and limits.

Type: object

Property Type Description
maxFieldSize number? = 1000000 Maximum allowed non-file multipart form field size in bytes; enough for your queries.
maxFileSize number? = Infinity Maximum allowed file size in bytes.
maxFiles number? = Infinity Maximum allowed number of files.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].