graphql-middleware-apollo-upload-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.10 • Public • Published

graphql-middleware-apollo-upload-server

CircleCI npm version

GraphQL Middleware Apollo Upload Server manages uploads for you so you don't have to care about them.

❗️ Requires Apollo Upload Server.

Install

yarn add graphql-middleware-apollo-upload-server

Overview

graphql-middleware-apollo-upload-server handles file upload for you by searching for all Upload types first, and handling the files if they are included in arguments. Everything else is in your hands!

Features

  • 👌 Easy to use.
  • 🛴 Half automatic.
  • 🏆 Works with every GraphQL server.

Demo

import { GraphQLServer } from 'graphql-yoga'
import { S3 } from 'aws-sdk'
import { upload } from 'graphql-middleware-apollo-upload-server'
 
const client = new S3({
  accessKeyId: __S3_KEY__,
  secretAccessKey: __S3_SECRET__,
  params: { Bucket: __S3_BUCKET__ },
})
 
const uploadToS3 = async file => {
  const { stream, filename, mimetype, encoding } = file
 
  const response = await client
    .upload({
      Key: filename,
      ACL: 'public-read',
      Body: file.stream,
    })
    .promise()
 
  return {
    name: filename,
    url: response.Location
  }
}
 
const typeDefs = `
  scalar Upload
 
  type Query {
    me: User
  }
 
  type Mutation {
    signup(name: String!, password: String!, picture: Upload!): User
  }
 
  type User {
    id: ID!
    name: String!
    password: String!
    picture: File!
  }
 
  type File {
    id: ID!
    name: String!
    url: String!
  }
`
 
const resolvers = {
  Query: {
    me: getMyself
  },
  Mutation: {
    signup: async (parent, { name, password, picture }, ctx, info) => {
      // "picture" has already been uploaded!
      return ctx.db.createUser({
        data: {
          name,
          password,
          picture: picture.url
        }
      })
    }
  }
}
 
const server = new GraphQLServer({
  typeDefs,
  resolvers,
  middlewares: [upload({ uploadHandler: uploadToS3 })],
  context: req => ({
    ...req,
    db: new Prisma({
      endpoint: __PRISMA_ENDPOINT__,
    })
  })
})
 
server.listen(() => {
  console.log(`Server running on https://localhost:4000`)
})

API

export interface IFile {
  stream: string
  filename: string
  mimetype: string
  encoding: string
}
 
interface IConfig<output> {
  uploadHandler: (file: IFile) => Promise<output>
}
 
export const upload = <output>(
  configIConfig<output>,
)IMiddleware

License

MIT @ Homeroom

Readme

Keywords

none

Package Sidebar

Install

npm i graphql-middleware-apollo-upload-server

Weekly Downloads

1

Version

1.0.10

License

MIT

Unpacked Size

53.5 kB

Total Files

8

Last publish

Collaborators

  • maticzav