federation

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package federation implements Apollo Federation v2 query planning and execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyProjection

func ApplyProjection(data map[string]any, proj []*FieldProjection) map[string]any

ApplyProjection trims data to only the fields in proj, discarding planner-added fields.

func Handler

func Handler(sg *Supergraph, client *http.Client) http.Handler

Handler returns an http.Handler that executes GraphQL federation queries against the given supergraph routing table using client for subgraph HTTP calls.

Types

type EntityFetchSpec

type EntityFetchSpec struct {
	Subgraph       *Subgraph
	TypeName       string   // entity type, e.g. "User"
	KeyFields      []string // key field names, e.g. ["id"]
	RequiresFields []string // @requires fields to embed in representations beyond key fields
	Selection      string   // the fields to fetch: "reviews {\n  title\n}\n"
	ParentPath     []string // JSON path to the parent in the merged data, e.g. ["user"]
	IsParentList   bool     // true when ParentPath resolves to a list
}

EntityFetchSpec describes a cross-subgraph entity resolution step.

type FetchSpec

type FetchSpec struct {
	Subgraph  *Subgraph
	Query     string
	Variables []string // variable names from the original operation used here
}

FetchSpec is a query to send to one subgraph.

type FieldProjection

type FieldProjection struct {
	Key      string             // response key (alias or field name)
	Children []*FieldProjection // nil for scalar fields
}

FieldProjection is a node in the user-requested selection tree. It is used to strip planner-added fields from the final merged response.

type GraphQLError

type GraphQLError struct {
	Message    string           `json:"message"`
	Locations  []map[string]int `json:"locations,omitempty"`
	Path       []any            `json:"path,omitempty"`
	Extensions map[string]any   `json:"extensions,omitempty"`
}

GraphQLError is a single error object in a GraphQL response.

func Execute

func Execute(
	ctx context.Context,
	plan *Plan,
	variables map[string]any,
	client *http.Client,
) (map[string]any, []GraphQLError, error)

Execute runs a Plan against real HTTP subgraph endpoints. It executes initial fetches in parallel, then resolves entity fetches sequentially, and returns the merged data and any accumulated errors.

type GraphQLRequest

type GraphQLRequest struct {
	Query         string         `json:"query"`
	OperationName string         `json:"operationName,omitempty"`
	Variables     map[string]any `json:"variables,omitempty"`
}

GraphQLRequest is the JSON body sent to a subgraph.

type GraphQLResponse

type GraphQLResponse struct {
	Data   json.RawMessage `json:"data"`
	Errors []GraphQLError  `json:"errors,omitempty"`
}

GraphQLResponse is the JSON body received from a subgraph.

type Plan

type Plan struct {
	// Fetches are the initial per-subgraph queries, safe to run in parallel.
	Fetches []*FetchSpec
	// EntityFetches run after Fetches in order; later steps may depend on earlier ones.
	EntityFetches []*EntityFetchSpec
	// Projection holds the user-requested field tree, used to strip planner-added
	// fields (key fields, __typename, @requires pre-fetch fields) from the final response.
	Projection []*FieldProjection
}

Plan is the execution plan for a GraphQL operation.

func BuildPlan

func BuildPlan(sg *Supergraph, queryStr, operationName string) (*Plan, error)

BuildPlan analyzes a GraphQL query against the supergraph routing table and returns a Plan describing how to execute it.

type Subgraph

type Subgraph struct {
	EnumName string // join__Graph enum value, e.g. "ACCOUNTS"
	Name     string // human name from @join__graph, e.g. "accounts"
	URL      string // service URL
}

Subgraph is a downstream federated GraphQL service.

type Supergraph

type Supergraph struct {
	// contains filtered or unexported fields
}

Supergraph holds parsed routing information extracted from a supergraph SDL.

func ParseSchema

func ParseSchema(sdl string) (*Supergraph, error)

ParseSchema parses a Federation v2 supergraph SDL and returns a routing table. It extracts subgraph URLs from the join__Graph enum and field ownership from @join__type / @join__field directives.

func (*Supergraph) FieldIsList

func (sg *Supergraph) FieldIsList(typeName, fieldName string) bool

FieldIsList reports whether a field on a type returns a list.

func (*Supergraph) FieldProvides

func (sg *Supergraph) FieldProvides(typeName, fieldName string) string

FieldProvides returns the raw @provides field set for a field (e.g. "totalProductsCreated"), or "" if the field has no @provides.

func (*Supergraph) FieldRequires

func (sg *Supergraph) FieldRequires(typeName, fieldName string) string

FieldRequires returns the raw @requires field set for a field (e.g. "id email"), or "" if the field has no @requires.

func (*Supergraph) FieldTypeName

func (sg *Supergraph) FieldTypeName(typeName, fieldName string) string

FieldTypeName returns the base return type name of a field (no !, no []).

func (*Supergraph) KeysFor

func (sg *Supergraph) KeysFor(typeName, subgraphEnum string) []string

KeysFor returns the key field names for a type in a given subgraph, parsed from the @join__type key argument (e.g. "id email" → ["id","email"]). Falls back to ["id"] when not specified.

func (*Supergraph) OwnerOf

func (sg *Supergraph) OwnerOf(typeName, fieldName string) string

OwnerOf returns the subgraph enum name owning a field on a type, or "" if unknown.

func (*Supergraph) SubgraphByEnum

func (sg *Supergraph) SubgraphByEnum(name string) *Subgraph

SubgraphByEnum returns the subgraph for an enum value name (e.g. "ACCOUNTS").

Jump to

Keyboard shortcuts

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