Giter VIP home page Giter VIP logo

apollo-upload-server's Introduction

Apollo upload logo

apollo-upload-server

npm version Licence Github issues Github stars

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

Setup

Install

With npm:

npm install apollo-upload-server

Server middleware

Add the server middleware just before graphql-server.

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

// ✂

app.use(
  '/graphql',
  bodyParser.json(),
  apolloUploadExpress({
    // Optional, defaults to OS temp directory
    uploadDir: '/tmp/uploads'
  }),
  graphqlExpress(/* ✂ */)
)

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

// ✂

router.post(
  '/graphql',
  apolloUploadKoa({
    // Optional, defaults to OS temp directory
    uploadDir: '/tmp/uploads'
  }),
  graphqlKoa(/* ✂ */)
)

// ✂

Custom middleware

If the middleware you need is not available, import the asynchronous function processRequest to make your own:

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

GraphQL schema

Add an input type for uploads to your schema. You can name it anything but it must have this shape:

input Upload {
  name: String!
  type: String!
  size: Int!
  path: String!
}

Client

Also setup apollo-upload-client.

Usage

Once setup, you will be able to use FileList, File and ReactNativeFile instances anywhere within mutation or query input variables. See the client usage.

The files upload to a configurable temp directory. Upload input type metadata replaces file instances in the arguments received by the resolver.

Single file

In types:

type Mutation {
  updateUserAvatar(userId: String!, avatar: Upload!): User!
}

In resolvers:

updateUserAvatar(root, { userId, avatar }) {
  // ✂ Auth
  // ✂ Update avatar
  console.log(`New avatar for user ${userId} is ${avatar.size} bytes`)
  // ✂ Return fresh user data
}

See client usage for this example.

Multiple files

In types:

type Mutation {
  updateGallery(galleryId: String!, images: [Upload!]!): Gallery!
}

In resolvers:

updateGallery(root, { galleryId, images }) {
  // ✂ Auth
  // ✂ Update gallery
  console.log(`New images for gallery ${galleryId}:`)
  images.forEach((image, index) =>
    console.log(`Image ${index} is ${image.size} bytes`)
  )
  // ✂ Return fresh gallery data
}

See client usage for this example.

Support

  • Node.js: See package.json engines.
  • Koa.
  • Express.

apollo-upload-server's People

Contributors

jaydenseric avatar

Watchers

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