Git Product home page Git Product logo

graphql-middleware-apollo-upload-server's Introduction

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>(
  config: IConfig<output>,
): IMiddleware

License

MIT @ Homeroom

graphql-middleware-apollo-upload-server's People

Contributors

dirkdevriendt avatar huv1k avatar maticzav avatar renovate-bot 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.