Skip to content

Enhances Apollo for intuitive file uploads via GraphQL mutations.

Notifications You must be signed in to change notification settings

AdamYee/apollo-upload-server

 
 

Repository files navigation

Apollo upload logo

apollo-upload-server

npm version Licence Github issues Github stars

Enhances Apollo for intuitive file uploads via GraphQL queries or mutations. Use with apollo-upload-client.

Setup

Install with peer dependencies using npm:

npm install apollo-upload-server graphql

Middleware

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).
import { apolloUploadKoa } from 'apollo-upload-server'

// …

router.post(
  '/graphql',
  koaBody(),
  apolloUploadKoa(/* Options */),
  graphqlKoa(/* … */)
)
import { apolloUploadExpress } from 'apollo-upload-server'

// …

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

Custom middleware

To make your own middleware import the processRequest async function:

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
  }
})

Client

Also setup apollo-upload-client.

Usage

Once setup, on the client use FileList, File and ReactNativeFile instances anywhere within query or mutation input variables. See the client usage.

Files upload via a GraphQL multipart request and appear as Upload scalars in resolver arguments.

See the example API and client.

Support

See package.json engines.

About

Enhances Apollo for intuitive file uploads via GraphQL mutations.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%