Essentials
Uploading Files
You might be tempted to slam scalar Upload
on top of your GraphQL Schema and process file uploads using your GraphQL server.
Don’t do it. If you are going to do it:
- It adds huge network load on your server,
- It’ll cost you more money,
- You won’t be able to upload files using SwiftGraphQL.
This guide explains how you can do it better.
Server
Instead of consuming all the traffic of file uploads on your server and sending it to your cloud storage, you are going to use signed upload URLs. Signed Upload URLs let client upload a file to the cloud storage provider directly in a given time period.
We split the tasks among server and client so that:
- the server is going to be responsible for creating signed URLs, and
- the client is going to be responsible for handling uploads
Client
Instead of passing in your file as a GraphQL mutation parameter, you are going to request a file from your GraphQL server and then use native Swift methods to upload your file there. The flow is going to consist of
- Requesting the
upload_url
andfile_id
, - Uploading the file to
upload_url
, - Using the
file_id
in mutation to link your file to a database model.