Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mercurius-js/mercurius-upload
graphql-upload implementation plugin for Fastify & mercurius
https://github.com/mercurius-js/mercurius-upload
Last synced: 13 days ago
JSON representation
graphql-upload implementation plugin for Fastify & mercurius
- Host: GitHub
- URL: https://github.com/mercurius-js/mercurius-upload
- Owner: mercurius-js
- License: mit
- Fork: true (bmullan91/fastify-gql-upload)
- Created: 2020-07-17T00:55:48.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-29T12:28:14.000Z (5 months ago)
- Last Synced: 2024-07-05T13:49:24.138Z (4 months ago)
- Language: TypeScript
- Homepage: https://mercurius.dev/
- Size: 249 KB
- Stars: 27
- Watchers: 3
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mercurius-upload
[graphql-upload-minimal](https://github.com/flash-oss/graphql-upload-minimal) implementation plugin for [Fastify](https://www.fastify.io/) & [mercurius](https://github.com/mercurius-js/mercurius).
Plugin made for **Fastify v5**:
## Install
```sh
yarn add mercurius-upload
# or
npm i mercurius-upload
# or
pnpm add mercurius-upload
```## Usage
Plugin options should conform to https://github.com/flash-oss/graphql-upload-minimal#type-processrequestoptions
```js
fastify.register(require('mercurius-upload'), {
// options passed to processRequest from graphql-upload-minimal
// maxFileSize: 1024 * 1024 * 5,
// maxFiles: 1,
})
```> or
```ts
import MercuriusGQLUpload from 'mercurius-upload'// ...
fastify.register(MercuriusGQLUpload, {
// options passed to processRequest from graphql-upload-minimal
})
```## Example
```js
const GQL = require('mercurius')
const { GraphQLUpload } = require('graphql-upload-minimal')
const fs = require('fs')
const util = require('util')
const stream = require('stream')
const path = require('path')const pipeline = util.promisify(stream.pipeline)
const uploadsDir = path.resolve(__dirname, '../uploads')const schema = /* GraphQL */ `
scalar Uploadtype Query {
add(x: Int, y: Int): Int
}type Mutation {
uploadImage(image: Upload): Boolean
}
`const resolvers = {
Upload: GraphQLUpload,
Query: {
add: async (_, { x, y }) => {
return x + y
},
},
Mutation: {
uploadImage: async (_, { image }) => {
const { filename, createReadStream } = await image
const rs = createReadStream()
const ws = fs.createWriteStream(path.join(uploadsDir, filename))
await pipeline(rs, ws)
return true
},
},
}module.exports = function (fastify, options, done) {
fastify.register(require('mercurius-upload'))fastify.register(GQL, {
schema,
resolvers,
graphiql: true,
})done()
}
```