Skip to content

File Upload🔗

In this section, we'll cover everything necessary for adding support for file uploads to your GraphQL schema using the GraphQL multipart request specification.

Setup🔗

Undine supports file uploads, but they are disabled by default due to security reasons. Specifically, since file uploads are sent using a multipart/form-data request, they may be sent without a CORS preflight request if the browser determines the request meets the criteria for a "simple request".

Therefore, you should make sure CSRF protection is enabled on the GraphQL endpoint if you want to use file uploads. This can be done by making sure that

  1. The GraphQL view is decorated with @csrf_protect, OR
  2. CsrfViewMiddleware exists in your MIDDLEWARE setting (and the GraphQL view is not decorated with @csrf_exempt)
1
2
3
4
5
MIDDLEWARE = [
    # ...
    "django.middleware.csrf.CsrfViewMiddleware",
    # ...
]

Then you can enable file uploads by adding the following to your settings:

1
2
3
UNDINE = {
    "FILE_UPLOAD_ENABLED": True,
}

Uploading files🔗

Undine has two Scalars for uploading files: File and Image. They correspond to Django's FileField and ImageField respectively. The File scalar is for general files while the Image scalar validates that the file is an image file.

Like Django's ImageField, using the Image scalar requires the Pillow library to be installed. You can install it together with Undine using pip install undine[image].

Now, let's suppose you have the following Model.

1
2
3
4
5
6
7
8
from django.db import models


class Task(models.Model):
    name = models.CharField(max_length=255)

    image = models.ImageField()
    attachment = models.FileField(null=True)

If you add a create mutation to the GraphQL schema like this

from undine import Entrypoint, MutationType, QueryType, RootType

from .models import Task


class TaskType(QueryType[Task], auto=True): ...


class TaskCreateMutation(MutationType[Task], auto=True): ...


class Mutation(RootType):
    create_task = Entrypoint(TaskCreateMutation)

...the TaskCreateMutation mutation InputObjectType will look like this:

1
2
3
4
input TaskCreateMutation {
  name: String!
  image: Image!
}

Now the client can send a request that conforms to the GraphQL multipart request specification, and Undine's GraphQL view will parse the request and slot the files into the correct locations in the input data.

Example request
POST /graphql/ HTTP/1.1
Content-Type: multipart/form-data; boundary=--BoUnDaRyStRiNg
Content-Length: 490

--BoUnDaRyStRiNg
Content-Disposition: form-data; name="operations"

{"query": "mutation($input: TaskCreateMutation!) { createTask(input: $input) { pk } }", "variables": {"input": {"name": "Task", "image": null}}}
--BoUnDaRyStRiNg
Content-Disposition: form-data; name="map"

{"0": ["variables.input.image"]}
--BoUnDaRyStRiNg
Content-Disposition: form-data; name="0"; filename="image.png"

\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x01c```\x00\x00\x00\x04\x00\x01\xe4\x94\x84\x06\x00\x00\x00\x00IEND\xaeB`\x82
--BoUnDaRyStRiNg--

See the Implementations section in the GraphQL multipart request specification for client side libraries that support file uploads.

Reading file data🔗

By default, the File and Image scalars return the URL of the file. To return more data about the file, use a TypedDict as the Field reference and add a custom resolver.

from pathlib import Path
from typing import TypedDict

from undine import Field, GQLInfo, QueryType

from .models import Task


class FileInfo(TypedDict):
    url: str
    name: str


class TaskType(QueryType[Task]):
    attachment = Field(FileInfo, nullable=True)

    @attachment.resolve
    def resolve_attachment(root: Task, info: GQLInfo) -> FileInfo | None:
        if not root.attachment:
            return None
        return FileInfo(url=root.attachment.url, name=Path(root.attachment.name).name)
1
2
3
4
5
6
7
8
type FileInfoType {
  url: String!
  name: String!
}

type TaskType {
  attachment: FileInfoType
}

Only add data that is stored in the database or computed without the storage backend. For example, FieldFile.size asks the storage backend for the file size. With a remote storage like S3, this is one request for every row in the result.

Image dimensions🔗

To return the width and height of an image, store them on the model with the width_field and height_field arguments of ImageField. Django updates these fields when the image is saved, and you can add them to the QueryType like other model fields.

1
2
3
4
5
6
7
8
9
from django.db import models


class Task(models.Model):
    name = models.CharField(max_length=255)

    image = models.ImageField(width_field="image_width", height_field="image_height")
    image_width = models.PositiveIntegerField(editable=False)
    image_height = models.PositiveIntegerField(editable=False)
1
2
3
4
5
6
7
8
9
from undine import Field, QueryType

from .models_image_dimensions import Task


class TaskType(QueryType[Task]):
    image = Field()
    image_width = Field()
    image_height = Field()
1
2
3
4
5
type TaskType {
  image: Image!
  imageWidth: Int!
  imageHeight: Int!
}

Without these fields, ImageFieldFile.width and ImageFieldFile.height open and read the image file for every row.