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

GraphQLUpload with processRequest on apollo-server-micro not working #280

Closed jorgev259 closed 2 years ago

jorgev259 commented 2 years ago

When uploading a file im getting the following error.

imagen

Server code:

const apolloServer = new ApolloServer({
  credentials: true,
  typeDefs: mergeTypeDefs(loadFilesSync(path.join(process.cwd(), 'graphql/schemas'))),
  resolvers: mergeResolvers([...Mutation, ...Query, ...types]),
  context: async ({ req, res }) => {
    const username = req.session.get('username')
    return { db, req, res, username, user: username && await db.models.user.findByPk(username) }
  }
})
const startServer = apolloServer.start()

export default withSession(async (req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*')
  res.setHeader('Access-Control-Allow-Credentials', 'true')
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')

  const contentType = req.headers['content-type']
  if (contentType && contentType.startsWith('multipart/form-data')) {
    req.filePayload = await processRequest(req, res)
  }

  await db.sync()
  await startServer

  return apolloServer.createHandler({ path: '/api/graphql' })(req, res)
})

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

Resolvers object: imagen

jaydenseric commented 2 years ago

What might be happening is that there are multiple installations/versions of graphql-upload, and one is being used for the middleware and another is being used for the Upload scalar. This causes a conflict; there should be only one version of graphql-upload integrated.

If you're using an old version of Apollo Server that automatically integrates an outdated version of graphql-upload, you need to opt out. From memory, you can do that via an Apollo Server contractor option uploads: false.

Once you have disabled Apollo's integration, follow the setup instructions in the graphql-upload readme carefully.

jorgev259 commented 2 years ago

uploads is already set to false, and apollo-server-micro in my current installation seems to be using 3+ apollo-server imagen

jaydenseric commented 2 years ago

Silly question, but is what you're using the variable $cover for in the GraphQL query correctly an Upload scalar? Maybe you're trying to use a file where some other kind of type is expected.

jorgev259 commented 2 years ago

It is, yarn why showed the direct graphql-upload dependency as the only one. But it seems a similar problem as other similar issues was the culprit.

Solved by using the full path to import the Scalar: import { GraphQLUpload } from 'node_modules/graphql-upload/public/index.js'

jaydenseric commented 2 years ago

Sounds like something weird about how dependencies are installed and resolved in your project.

BTW, it's best to skip the index module and directly deep import the thing you need:

import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';

Or for your funny workaround:

import GraphQLUpload from 'node_modules/graphql-upload/public/GraphQLUpload.js';