New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mercurius-upload

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mercurius-upload

Fastify plugin to support GraphQL uploads using graphql-upload

8.0.0
latest
Source
npm
Version published
Weekly downloads
5K
2.4%
Maintainers
0
Weekly downloads
 
Created
Source

mercurius-upload

graphql-upload-minimal implementation plugin for Fastify & mercurius.

Plugin made for Fastify v5:

Install

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

fastify.register(require('mercurius-upload'), {
  // options passed to processRequest from graphql-upload-minimal
  // maxFileSize: 1024 * 1024 * 5,
  // maxFiles: 1,
})

or

import MercuriusGQLUpload from 'mercurius-upload'

// ...

fastify.register(MercuriusGQLUpload, {
  // options passed to processRequest from graphql-upload-minimal
})

Example

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 Upload

  type 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()
}

Keywords

fastify

FAQs

Package last updated on 12 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts