jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

apollo-server-micro not compatible? #160

Closed shipcake closed 5 years ago

shipcake commented 5 years ago

I use Nextjs 9 and happy to use apollo graphql run same project. but i problem to use graphql-upload in apollo-server-micro

I console.log request income

[ Promise {
    { filename: 'aa5.png',
    mimetype: 'image/png',
    encoding: '7bit',
    createReadStream: [Function: createReadStream] } } ]

when I upload in client to server i found error "createReadStream is not a function" :( please help.

sambhav-gore commented 3 years ago

@shipcake How was this resolved ?

mouhsinelonly commented 3 years ago

@shipcake wondering the same what was the resolution for this, thank you

ulentini commented 3 years ago

In case someone else is stuck with this, I was able to solve it by using processRequest():

import { ApolloServer } from "apollo-server-micro"
import { processRequest } from "graphql-upload"

export const config = {
  api: {
    bodyParser: false,
  },
}

const apolloServer = new ApolloServer({
  // ...
})

const apolloHandler = apolloServer.createHandler({ path: "/api/graphql" })

export default async (req, res) => {
  const contentType = req.headers["content-type"]
  if (contentType && contentType.startsWith("multipart/form-data")) {
    req.filePayload = await processRequest(req, res)
  }
  return apolloHandler(req, res)
}
s875515 commented 2 years ago

For the guys who are used Next.js and custom express server to apply middleware, you have to move the handle up than middleware:

const express = require("express");
const next = require("next");
const { graphqlUploadExpress } = require("graphql-upload");

const port = 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.all("*", (req, res) => handle(req, res));

  server.use(
    graphqlUploadExpress({
      maxFileSize: 10000000, // 10 MB
      maxFiles: 20,
    })
  );

  server.listen(port, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${port}`);
  });
});
davevilela commented 2 years ago

Any drawbacks of using express custom server instead of micro ? I remember reading on Next.JS own docs that custom servers are not compatible with Vercel ...