graphql

package
v2.59.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package graphql implements the parts of GraphQL upload support that need reflection, keeping them out of the client's public surface.

A GraphQL multipart request sends its files in their own parts of the body and points each one at the variables it fills through a "map" field holding paths such as "variables.input.avatar". Those paths have to match the JSON the query marshals to exactly, which is what CollectUploads derives, and BuildMultipartBody assembles the body they describe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildMultipartBody

func BuildMultipartBody(operations []byte, files []File) ([]byte, string, error)

BuildMultipartBody assembles the multipart/form-data body carrying operations and files, as described by the GraphQL multipart request specification (https://github.com/jaydenseric/graphql-multipart-request-spec): an "operations" field holding the marshaled query, a "map" field pointing each file part at the variable paths it fills, and one part per file, named after its key in the map. It returns the body and the Content-Type header that describes it.

Types

type File

type File struct {
	Filename    string
	ContentType string
	Content     []byte
	Paths       []string
}

File is one file part of a multipart GraphQL request, bound to the variable paths it fills.

type Ref

type Ref[T any] struct {
	Value *T
	Paths []string
}

Ref is an upload found in a query's variables, together with every path in the marshaled query at which it appears.

func CollectUploads

func CollectUploads[T any](variables map[string]any) ([]Ref[T], error)

CollectUploads returns every value of type T reachable from variables, together with the paths those values occupy in the JSON the query marshals to, for example "variables.input.avatar" or "variables.input.files.0". The paths make up the "map" field of a multipart GraphQL request, so they have to match the marshaled operations exactly: struct fields are named by their json tag and embedded structs are flattened, mirroring encoding/json.

Map keys are visited in sorted order, which keeps the returned order - and therefore the part names derived from it - deterministic. A value that is referenced from several paths is returned once with all of its paths, as the specification allows a single file to fill more than one variable.

Values that encoding/json does not marshal from the Go types alone - those below a custom marshaler, or below a map key that is neither a string nor an integer - have no path that can be derived. Rather than drop them, and send a query that silently omits a file the caller provided, CollectUploads reports an error naming where the value sits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL