graphql-upload: BadRequestError: Missing multipart field ‘operations’
I have read all the issues mentioned by github as similar to my, but there is no solution.
Here is my server backend base
const app = express();
app.use(cors());
app.use(
'/graphql',
graphqlUploadExpress({ maxFileSize: 50000000, maxFiles: 10 })
);
app.use('/graphql', bodyParser.json());
app.use(compression());
app.use(cookieParser());
app.use(fileUpload());
app.disable('x-powered-by');
versions of modules:
"apollo-server": "^2.10.0",
"graphql-upload": "^10.0.0",
So, I have installed graphql-upload and try to upload a file using browser and Altair (graphql playground analog supporting file uploads)
All my attempts to upload a file finish the same, I get the following error message:
BadRequestError: Missing multipart field ‘operations’ (https://github.com/jaydenseric/graphql-multipart-request-spec). at Busboy.parser.once (ProjectName/node_modules/apollo-server-core/node_modules/graphql-upload/lib/processRequest.js:290:35)
Here is the request.body (it is the same both from browser and from Altair)
{ query:
'mutation FileUpload($image: Upload!) {\n fileUpload(\n file: $image\n ) {\n tmpFilename\n }\n}',
variables:
{ file:
Upload {
resolve: [Function],
reject: [Function],
promise: [Promise],
file: [Object] } },
operationName: 'FileUpload' }
But as I see at link above (from error message), the backend app expects operations and map keys in request body.
Why does it expect these params if neither apollo-react nor Altair app doesn’t send them?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (2 by maintainers)
I solve it, by adding
uploads: false:const server = new ApolloServer({ uploads:false, schema, playground: true });GitHub issues is not really the right place to ask for general help, with the exception of people who sponsor my effort.
There is no reason to do any of that if you have already verified that the client is sending a correct GraphQL multipart request.
When file uploads are not working as expected, start verifying the client is setup perfectly and working, then move on to the server and do the same. I’m sure if everything was configured properly you won’t have any troubles. Try to understand at a basic level how the different parts work; it might help in your troubleshooting.
I’m making the base assumption that you’re deliberately not using the built in file upload support in Apollo Server Express (perhaps to use a newer
graphql-uploadversion than Apollo ships with), which in most cases works out of the box without special config.For file uploads the GraphQL API expects the request to be a multipart request, not a JSON POST requests, so those fields mentioned in the error are meant to be multipart request fields. If you are sure the client is sending a correct multipart request, you need to look at problems with how the server is configured.
I’m not sure what the
fileUploadis, and I can’t see Apollo Server in the middleware. RegardingbodyParser.json(), be aware that some body parser middleware interferes with with parsing multipart requests.If you are trying to manually setup file uploads, remember to disable the file uploads feature completely in Apollo Server - I’ll leave that to you to figure out how to do; I’ve explained how previously in other issue comments.
It seems that in previous versions of Apollo Server, they had a built-in integration with an old version of
graphql-upload. So you have to indicate withuploads: false, that you are integrating it yourself.Take a look at this implementation of the repo author: https://github.com/jaydenseric/apollo-upload-examples/blob/master/api/server.mjs
Thank you @Enmanuellperez98, that resolved the problem for me as well.
Thank you very much for the clarification and links. Very helpful.
I hope you think again about upvoting the helpful answer above instead of downvoting, @giautm.
For folks that encountered this issue, if you have middlewares like
express-fileuploadorfileParser, remove them; they won’t letgraphql-uploadconsume the data. That’s how I solved it.Unfortunately no result on frontend, this is my frontend code, I removed all other links, left only this
And I still get non-multipart request to the backend while making this:
I’ve read some more articles on the internet and now I realize that both my frontend application and Altait send bad requests to backend, thank you for the direction of my search, I will post here a solution when it will be found