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

Making graphql-upload work with apollo-server-express #271

Closed mdcarter closed 3 years ago

mdcarter commented 3 years ago

Hello everyone,

I have a next.js application running with an apollo server running inside of it. I'm trying to get the upload functionnality up and running but I don't understand where is my issue. I either have a timeout error when uploading, or this issue : POST body missing, invalid Content-Type, or JSON object has no keys.

Here is a simplified version of my server for you to see :

async function runMiddleware (req: NextApiRequest, res: NextApiResponse, fn: any) {
  return await new Promise((resolve, reject) => {
    fn(req, res, (result: any) => {
      if (result instanceof Error) {
        return reject(result)
      }

      return resolve(result)
    })
  })
}

const server = new ApolloServer({
  dataSources,
  schema: authDirectiveTransformer(
    makeExecutableSchema({ typeDefs, resolvers })
  ),
  context: ({ req }) => {
    return { prisma, req }
  }
})

await server.start()

const apolloMiddleware = server.getMiddleware({ path: '/api/graphql' })
export const config: PageConfig = { api: { bodyParser: false } }

export default async function handler (
  req: NextApiRequest,
  res: NextApiResponse
): Promise<any> {
  await runMiddleware(req, res, apolloMiddleware)
}

I tried to apply the middleware everywhere I could think of but no luck, if you guys could give me a hand that would be great, thank 🙏

MatthewHallCom commented 3 years ago

Seems this is caused by a recent version of apollo-server-express, we just upgraded to a newer version and ran into this problem

MatthewHallCom commented 3 years ago

https://github.com/profusion/apollo-federation-file-upload/issues/31#issuecomment-888658003

Following this resolved it for us -- we needed to add the app.use(graphqlUploadExpress());

jaydenseric commented 3 years ago

graphql-upload exports a very simple graphqlUploadExpress function to create express middleware; I don't think anyone here is confused how to use the graphql-upload API which is what I'm here to support. The confusion seems to be based around how to use Next.js and flavors of Apollo Server. The Next.js repo discussions has a help category. The best place to ask for help about Apollo products is through Apollo repos or support channels. Apollo has had $1.5 billion of funding to document their products and support their users; I get paid nothing each time I help them out.

These are not questions with answers I know off the top of my head that I can help out with as a quick favor. If anyone knows the answers, feel free to comment solutions.

One piece of advice; don't build your GraphQL API in the same project as an app. Keep the GraphQL API separate with it's own package.json dependencies and deployments. A GraphQL API is a long term investment and can be used to power multiple generations of apps. Ideally you can work on multiple apps at the same time even. You don't want to end up in a situation where your GraphQL API gets hacked via a compromised npm package that's one of the many thousands of dependencies of your Next.js app. You also don't want to have the nightmare of untangling your app dependencies, tooling and code from the API stuff when the app becomes legacy and you want to replace it.