Hi,
I use Apollo Server with Fastify and graphql-upload and am currently trying to upgrade to Apollo Server 4, Fastify 4 (with @as-integrations/fastify) and graphql-upload 16.
When upgrading from version 2 to 3 of Apollo and Fastify, using graphql-upload 12, I followed this example for uploads which worked right away: File uploads - Apollo GraphQL Docs.
Details: in the React client we have an input of type “file”. The added files are sent to Apollo Server as a binary. As described in the linked document above, we have a preValidation hook for multipart requests, which applies to file uploads. In there we call the processRequest method of graphql-upload because it doesn’t provide middleware for Fastify. The result of that goes to the resolver. Inside the resolver we call createReadStream on the file and pass the result to the default export of the raw-body npm package, which gets the entire buffer of a stream either as a Buffer or a string. We then pass this buffer to the RESTDataSource. The upload method inside the RESTDataSource uses FormData (currently form-date from npm but also tried native FormData) and adds the buffer to the body for the request we send via the RESTDataSource’s post method.
Now, when trying to upgrade to version 4, this doesn’t work anymore (it’s not the CSRF issue). But without making any other changes apart from the upgrades, the data source server suddenly complained about an empty (or non-existent) Content-Type
header. I tried adding it manually incl. a generated boundary. But then the server started complaining about a missing parameter. Since form-data didn’t change, I wouldn’t know why the body of the request should be different unless something different is happening inside RESTDataSource’s post method.
Reverting to an older version of graphql-upload and raw-body also didn’t change anything there, so it seems like it must be caused by the Apollo upgrade (potentially a change to RESTDataSource). But so far I couldn’t find any info on what I would have to do differently.
I also briefly tried using fastify-multipart. But then graphql-upload’s processRequest
started throwing an error. Or rather, the underlying busboy package did: Error: Unexpected end of form at Multipart._final
(node_modules/busboy/lib/types/multipart.js:588:17). Wasn’t able to fix that either unfortunately.