Code Monkey home page Code Monkey logo

apollo-upload-server's Introduction

Apollo upload logo

apollo-upload-server

npm version Licence Github issues Github stars Travis status

Enhances Apollo for intuitive file uploads via GraphQL queries or mutations. Use with a GraphQL multipart request spec client implementation such as apollo-upload-client.

Setup

Install with peer dependencies using npm:

npm install apollo-upload-server graphql

Add the middleware just before graphql-server.

Options

  • maxFieldSize (integer): Max allowed non-file multipart form field size in bytes; enough for your queries (default: 1 MB).
  • maxFileSize (integer): Max allowed file size in bytes (default: Infinity).
  • maxFiles (integer): Max allowed number of files (default: Infinity).

Koa

import { apolloUploadKoa } from 'apollo-upload-server'

// …

router.post(
  '/graphql',
  koaBody(),
  apolloUploadKoa(/* Options */),
  graphqlKoa(/* … */)
)

Express

import { apolloUploadExpress } from 'apollo-upload-server'

// …

app.use(
  '/graphql',
  bodyParser.json(),
  apolloUploadExpress(/* Options */),
  graphqlExpress(/* … */)
)

Custom middleware

Middleware wraps the async function processRequest which accepts a Node.js request and an optional options object as arguments. It returns a promise that resolves an operations object for a GraphQL server to consume (usually as the request body). Import it to create custom middleware:

import { processRequest } from 'apollo-upload-server'

Upload scalar

A file upload promise that resolves an object containing:

  • stream
  • filename
  • mimetype
  • encoding

It must be added to your types and resolvers:

import { makeExecutableSchema } from 'graphql-tools'
import { GraphQLUpload } from 'apollo-upload-server'

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

Usage

Files uploaded via a GraphQL multipart request appear as Upload scalars in resolver arguments. The upload streams can be used to store the files in the local filesystem or in the cloud. See also apollo-upload-client usage and the example API and client.

Support

  • Node.js v6.10+, see package.json engines.

apollo-upload-server's People

Contributors

fberthelot avatar jaydenseric avatar samcoenen avatar wtgtybhertgeghgtwtg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.