Base64String — GraphQL Custom Scalar

Author – ChilliCream

Date – 2025-12-29

1Overview

The Base64String scalar type represents an array of bytes encoded as a Base64 string. It is intended for scenarios where binary data needs to be transmitted, such as file contents, cryptographic keys, image data, or any arbitrary binary data.

Base64 encoding allows binary data to be safely represented as text in JSON and GraphQL, which natively only support text-based formats.

3Result spec

A Base64String scalar must serialize to a Base64-encoded string representing the binary data.

The Base64 encoding should follow RFC 4648, using the standard Base64 alphabet (A-Z, a-z, 0-9, +, /) with padding characters (=) as needed.

3.1Examples

These are valid result values:

Value Explanation
"SGVsbG8gV29ybGQ=" Base64-encoded “Hello World”.
"AQIDBA==" Base64-encoded bytes [1, 2, 3, 4].
"" Empty byte array (zero bytes).

These are invalid result values:

Value Why is it invalid
"Hello World" Not Base64-encoded.
"SGVsbG8gV29ybGQ" Missing padding (should be SGVsbG8gV29ybGQ=).
"SGVs bG8=" Contains whitespace.
123 Not a string value.

4Input spec

A Base64String scalar accepts Base64-encoded string values, both as GraphQL literals and as JSON input values.

The input must be a valid Base64-encoded string following RFC 4648. Implementations should validate that the input string contains only valid Base64 characters and proper padding.

Implementations should validate:

4.1Examples

Valid input values:

GraphQL Literal:

mutation {
  uploadFile(content: "SGVsbG8gV29ybGQ=") {
    id
  }
}

JSON input:

{
  "content": "SGVsbG8gV29ybGQ="
}
{
  "thumbnail": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
}

Invalid input values:

Value Why is it invalid
"Hello World" Not Base64-encoded.
"SGVs bG8=" Contains whitespace.
"SGVsbG8!" Contains invalid character (!).
"SGVsbG8" Invalid length (not a multiple of 4).

5References

  1. 1Overview
  2. 2Recommended name
  3. 3Result spec
    1. 3.1Examples
  4. 4Input spec
    1. 4.1Examples
  5. 5References