Originally it was not possible to upload files through GraphQL so typically I used a seperate REST API when I needed support for uploading files.
The main disadvantage of this approach was that it introduced some extra work and that I had to make sure that things like authentication and authorization were handled on both the REST endpoint and the GraphQL endpoint.
With the introduction of the GraphQL Multipart Request specification, this separate endpoint became obsolete and uploading files through your GraphQL server became an option.
Let’s walk through the steps to get this done in HotChocolate:
- I used the HotChocolate template to get started quickly. If you don’t have the template yet, first install it using:
dotnet new -i HotChocolate.Templates.Server
- Now you can bootstrap your application using:
dotnet new graphql
- Register the
Upload
scalar to support file uploads: - Create a mutation that uses this scalar to upload the file:
- Register the mutation:
- Invoke the graphQL endpoint:
Remark: The upload scalar is not yet supported in Strawberry Shake or Banana Cake Pop.