• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    TypeScript
  • Created over 6 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Upload is hard, that's why we do it for you. ๐ŸŽ‰

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

More Repositories

1

graphql-shield

๐Ÿ›ก A GraphQL tool to ease the creation of permission layer.
TypeScript
3,555
star
2

nookies

๐Ÿช A set of cookie helpers for Next.js
TypeScript
2,269
star
3

emma-cli

๐Ÿ“ฆ Terminal assistant to find and install node packages.
TypeScript
1,228
star
4

graphql-middleware

Split up your GraphQL resolvers in middleware functions
TypeScript
1,134
star
5

swift-graphql

A GraphQL client that lets you forget about GraphQL.
Swift
598
star
6

ink-table

๐Ÿ“A table component for Ink.
TypeScript
180
star
7

ink-autocomplete

๐Ÿง  An autocomplete component for Ink.
JavaScript
80
star
8

label-sync

๐Ÿท A delightful companion to manage Github Labels across multiple repositories.
TypeScript
60
star
9

creato

๐Ÿ‘จ๐Ÿปโ€๐Ÿญ create-app for any occasion
TypeScript
24
star
10

graphql-middleware-forward-binding

GraphQL Binding forwardTo plugin for GraphQL Middleware
TypeScript
23
star
11

graphql-server-file-upload-example

๐Ÿ’พ GraphQL Server examples: File API
TypeScript
21
star
12

graphql-server-github-auth-example

GraphQL Server examples: Authentication
TypeScript
20
star
13

kayu

TypeScript GraphQL client that lets you forget about GraphQL
TypeScript
15
star
14

sponsorsme

A lightweight utility to check whether a user sponsors you.
TypeScript
14
star
15

prisma-converter

Convert your Prisma 1.0 datamodel to Prisma 2.0
TypeScript
13
star
16

resk

โœ‚๏ธ Automatically create gists from your code.
JavaScript
9
star
17

multilinestring

The missing template literal for NodeJS.
TypeScript
7
star
18

gimb-events

๐ŸŽŸA ticketing system for all Gimnazija Beลพigrad events
TypeScript
4
star
19

sessions

Manage NodeJS server sessions easily in-memory or in Redis.
TypeScript
4
star
20

reserve-npm

๐Ÿ“ฆ A CLI tool which helps you reserve a NPM package name
TypeScript
3
star
21

github-tree

๐Ÿ™ Github API commits made easy peasy
TypeScript
3
star
22

semantha

๐Ÿ’โ€โ™€๏ธ A delightful mono-repository assistant.
TypeScript
2
star
23

multiwatch

A web application for better multiple stopwatch-times handling.
JavaScript
1
star
24

graphql-vs-rest

Elm
1
star
25

resizable-dots

Simple Elm app.
HTML
1
star
26

nepremicnine

Repozitorij s projektno nalogo pri Programiranju 1
HTML
1
star
27

stars

Draw.
Elm
1
star