argo

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 7 Imported by: 0

README

⛵ argo-go: Argo for Go

argo-go is a Go implementation of Argo, a compact and compressible binary serialization format for GraphQL.

It is written in Go and distributed as a Go module. The MIT-licensed source code is available on GitHub at github.com/beeper/argo-go.

Features
Feature Implemented? Notes
InlineEverything
SelfDescribing
OutOfBandFieldErrors ⚠️ Inline field errors not supported
SelfDescribingErrors ⚠️ Non-self-describing not supported
NullTerminatedStrings
NoDeduplication
HasUserFlags No user flags implemented
Dependencies

argo-go relies on the following external libraries:

Tests

Tests may be run with go test ./...

argo-go includes a suite of test data adapted from the reference Argo implementation, facilitating equivalence testing.

Versioning

argo-go uses Semantic Versioning 2.0.0.

Example

Here's a basic example of how to use argo-go to encode a GraphQL response to Argo and decode it back. This example uses a very simple schema and query.

main/main.go

package main

import (
	"fmt"
	"github.com/beeper/argo-go"
	"github.com/beeper/argo-go/internal/util"
	"github.com/elliotchance/orderedmap/v3"
	"github.com/vektah/gqlparser/v2"
	"github.com/vektah/gqlparser/v2/ast"
)

// Production code should handle errors, we skip over them for brevity
func main() {
	// 1. Define a simple GraphQL schema
	schemaString := `
    type Query {
      hello: String
      version: Float
    }
  `
	schema := gqlparser.MustLoadSchema(&ast.Source{Name: "example.graphql", Input: schemaString})

	// 2. Define a simple GraphQL query
	queryString := `
    query MyQuery {
      hello
      version
    }
  `
	query := gqlparser.MustLoadQuery(schema, queryString)

	// 3. Create an ExecutionResultCodec
	// The operation name "MyQuery" is optional if there's only one operation in the query document.
	erc, _ := argo.NewExecutionResultCodec(schema, query, "MyQuery")

	// 4. Prepare the data to encode (simulating a GraphQL execution result)
	// The top-level map should conform to the GraphQL ExecutionResult structure, typically with a "data" key.
	dataField := orderedmap.NewOrderedMap[string, any]()
	dataField.Set("hello", "Welcome to Argo!")
	dataField.Set("version", 1.2)

	executionResult := orderedmap.NewOrderedMap[string, any]()
	executionResult.Set("data", dataField)

	fmt.Printf("Original data: %s\n", util.NewOrderedMapJSON[string, any](executionResult).MustMarshalJSON())

	// 5. Encode the data to Argo
	argoBytes, _ := erc.OrderedMapToArgo(executionResult)
	fmt.Printf("Encoded Argo bytes: %x\n", argoBytes.Bytes())

	// 6. Decode the Argo bytes back to an OrderedMap
	decodedResult, _ := erc.ArgoToOrderedMap(argoBytes)
	fmt.Printf("Decoded data:  %s\n", util.NewOrderedMapJSON[string, any](decodedResult).MustMarshalJSON())
}
$ go run main/main.go
Original data: {"data":{"hello":"Welcome to Argo!","version":1.2}}
Encoded Argo bytes: 182057656c636f6d6520746f204172676f2110333333333333f33f0800200003
Decoded data:  {"data":{"hello":"Welcome to Argo!","version":1.2}}

This example demonstrates the core round-trip functionality. In a real application, the GraphQL schema and queries would be loaded from files or other sources.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecutionResultCodec

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

ExecutionResultCodec is the primary entry point for encoding and decoding GraphQL ExecutionResult objects to and from the Argo binary format. It utilizes a GraphQL schema and query document to determine the wire types for encoding and decoding data, managed by an internal Typer instance.

func NewExecutionResultCodec

func NewExecutionResultCodec(schema *ast.Schema, query *ast.QueryDocument, operationName ...string) (*ExecutionResultCodec, error)

NewExecutionResultCodec creates a new ExecutionResultCodec. It requires a GraphQL schema and a query document. An optional operationName can be provided to specify which operation in the query document to use; if not provided, the Typer will attempt to select a default operation. Returns an error if the Typer cannot be initialized (e.g., due to an invalid schema, query, or operation name).

func (*ExecutionResultCodec) ArgoToOrderedMap

func (erc *ExecutionResultCodec) ArgoToOrderedMap(argoBytes *buf.Buf) (*orderedmap.OrderedMap[string, interface{}], error)

ArgoToOrderedMap converts Argo-encoded bytes from a buf.Buf into an orderedmap.OrderedMap. This OrderedMap represents a GraphQL ExecutionResult (typically containing "data", "errors", "extensions" keys). The method relies on the underlying ArgoDecoder to correctly parse the Argo bytes according to the schema and query associated with this ExecutionResultCodec.

func (*ExecutionResultCodec) OrderedMapToArgo

func (erc *ExecutionResultCodec) OrderedMapToArgo(om *orderedmap.OrderedMap[string, interface{}]) (*buf.Buf, error)

OrderedMapToArgo converts an orderedmap.OrderedMap (representing a GraphQL ExecutionResult) into Argo-encoded bytes contained in a buf.Buf. The `om` parameter holds the GraphQL execution result (data, errors, extensions). This method uses a default ArgoEncoder.

func (*ExecutionResultCodec) OrderedMapToArgoWithEncoder

func (erc *ExecutionResultCodec) OrderedMapToArgoWithEncoder(om *orderedmap.OrderedMap[string, interface{}], encoder *codec.ArgoEncoder) (*buf.Buf, error)

OrderedMapToArgoWithEncoder converts an orderedmap.OrderedMap (representing a GraphQL ExecutionResult) into Argo-encoded bytes using a provided ArgoEncoder. This allows for customization of the encoding process, such as setting specific header flags. The `om` parameter holds the GraphQL execution result. The encoder is responsible for deterministic serialization of the map contents.

func (*ExecutionResultCodec) Typer

func (erc *ExecutionResultCodec) Typer() *typer.Typer

Typer returns the Typer instance used by this ExecutionResultCodec. The Typer is responsible for determining the Argo wire types based on the GraphQL schema and query.

Directories

Path Synopsis
Package block provides reader and writer types for processing Argo data blocks.
Package block provides reader and writer types for processing Argo data blocks.
Package codec provides the tools for encoding and decoding Argo data.
Package codec provides the tools for encoding and decoding Argo data.
internal
testutils
Package testutils provides utility functions for testing Argo functionality.
Package testutils provides utility functions for testing Argo functionality.
util
Package util provides miscellaneous utility functions used across the argo-go library.
Package util provides miscellaneous utility functions used across the argo-go library.
Package label defines the Label type and its associated operations for Argo.
Package label defines the Label type and its associated operations for Argo.
pkg
bitset
Package bitset provides a BitSet data structure backed by *big.Int, along with helpers for reading and writing bitsets in variable-length (self-delimiting) and fixed-length formats.
Package bitset provides a BitSet data structure backed by *big.Int, along with helpers for reading and writing bitsets in variable-length (self-delimiting) and fixed-length formats.
buf
directives
Package directives defines and provides helpers for Argo-specific GraphQL directives used to control serialization, deserialization, and other behaviors.
Package directives defines and provides helpers for Argo-specific GraphQL directives used to control serialization, deserialization, and other behaviors.
varint
Package varint implements ULEB128 (Unsigned Little Endian Base 128) and ZigZag encoding/decoding for arbitrary-precision integers (*big.Int) and standard integer types (uint64, int64).
Package varint implements ULEB128 (Unsigned Little Endian Base 128) and ZigZag encoding/decoding for arbitrary-precision integers (*big.Int) and standard integer types (uint64, int64).
Package typer is responsible for converting GraphQL schema types and query selections into Argo wire types.
Package typer is responsible for converting GraphQL schema types and query selections into Argo wire types.
Package wire defines the fundamental Argo wire types, such as String, Varint, Record, Array, etc., and provides utilities for working with these types.
Package wire defines the fundamental Argo wire types, such as String, Varint, Record, Array, etc., and provides utilities for working with these types.

Jump to

Keyboard shortcuts

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