jaydenseric / graphql-upload

Middleware and an Upload scalar 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 131 forks source link

Support Apollo-Server-micro #286

Closed NitelPhyoe closed 2 years ago

NitelPhyoe commented 2 years ago

package.json

"dependencies": {

    "@types/graphql-fields": "^1.3.4",
    "apollo-server-core": "^3.5.0",
    "apollo-server-micro": "^3.5.0",
    "graphql": "^15",
    "graphql-fields": "^2.0.3",
    "graphql-scalars": "^1.13.6",
    "graphql-upload": "^13.0.0",
    "micro": "^9.3.4",
    "next": "12.0.4",
    "reflect-metadata": "^0.1.13",
    "type-graphql": "^1.1.1",
    "type-is": "^1.6.18"
  }

I setup custom middleware like that

const middleware = (request, response, next) => {
    if (!typeis(request, ["multipart/form-data"])) return next();

    const finished = new Promise((resolve) => request.on("end", resolve));
    const { send } = response;

    response.send = (...args) => {
        finished.then(() => {
            response.send = send;
            response.send(...args);
        });
    };

    return processRequest(request, response)
        .then((body) => {
            request.body = body;
            next();
        })
        .catch((error) => {
            if (error.status && error.expose) response.status(error.status);
            next(error);
        });
};

My apollo server setup is

        const apolloServer = await getApolloServer(schema);
        await apolloServer.createHandler({ path: "/api/graphql" })(req, res);

It keep fail when mutation run api response send this message and no error log on server POST body missing, invalid Content-Type, or JSON object has no keys.

NitelPhyoe commented 2 years ago

my apologies this issue was fix on #160