How do you handle file uploads in GraphQL?
Answer
GraphQL doesn't natively support file uploads since it is typically JSON-based. The most common approach is the GraphQL multipart request spec, implemented by libraries like graphql-upload. This uses a multipart form-data request to send files alongside the query. The Upload scalar is added to the schema: scalar Upload. In a mutation: mutation ($file: Upload!) { uploadFile(file: $file) { filename } }. The resolver receives a Promise resolving to an object with createReadStream, filename, mimetype. Alternative approaches: (1) Use a pre-signed URL from S3/GCS — return a URL from a mutation, then upload directly to storage from the client (preferred for large files). (2) Upload to a separate REST endpoint and pass the URL to GraphQL. Apollo Client supports the multipart spec via @apollo/client + apollo-upload-client.
Previous
What is the difference between query, mutation, and subscription operations?
Next
What is the difference between REST and GraphQL in terms of over-fetching and under-fetching?