https://github.com/maticzav/graphql-middleware-apollo-upload-server
Upload is hard, that's why we do it for you. :tada:
https://github.com/maticzav/graphql-middleware-apollo-upload-server
apollo-upload-server graphql-middleware-plugin
Last synced: about 1 year ago
JSON representation
Upload is hard, that's why we do it for you. :tada:
- Host: GitHub
- URL: https://github.com/maticzav/graphql-middleware-apollo-upload-server
- Owner: maticzav
- Created: 2018-06-15T13:25:21.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2025-03-14T02:39:50.000Z (over 1 year ago)
- Last Synced: 2025-03-29T13:11:15.159Z (about 1 year ago)
- Topics: apollo-upload-server, graphql-middleware-plugin
- Language: TypeScript
- Homepage:
- Size: 150 KB
- Stars: 129
- Watchers: 2
- Forks: 3
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# graphql-middleware-apollo-upload-server
[](https://circleci.com/gh/homeroom-live/graphql-middleware-apollo-upload-server)
[](https://badge.fury.io/js/graphql-middleware-apollo-upload-server)
GraphQL Middleware Apollo Upload Server manages uploads for you so you don't have to care about them.
> ❗️ Requires [Apollo Upload Server](https://github.com/jaydenseric/apollo-upload-server).
## Install
```bash
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
```ts
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
```ts
export interface IFile {
stream: string
filename: string
mimetype: string
encoding: string
}
interface IConfig {
uploadHandler: (file: IFile) => Promise
}
export const upload = (
config: IConfig,
): IMiddleware
```
## License
MIT @ Homeroom