graphapi

package
v0.78.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package graphapi provides a GraphQL schema generator using reflection.

Index

Constants

This section is empty.

Variables

View Source
var Any = graphql.NewScalar(graphql.ScalarConfig{
	Name: "Any",
	Serialize: func(value interface{}) interface{} {
		return value
	},
	ParseValue: func(value interface{}) interface{} {
		return value
	},
	ParseLiteral: func(value ast.Value) interface{} {
		return value.GetValue()
	},
})
View Source
var Data = graphql.NewScalar(graphql.ScalarConfig{
	Name: "Data",
	Serialize: func(value interface{}) interface{} {
		return base64.StdEncoding.EncodeToString(value.([]byte))
	},
	ParseValue: func(value interface{}) interface{} {
		raw, err := base64.StdEncoding.DecodeString(value.(string))
		if err != nil {
			log.Errorf("graphapi: failed to decode base64: %w", err)
			return nil
		}
		return raw
	},
	ParseLiteral: func(value ast.Value) interface{} {
		return nil
	},
})
View Source
var Int64 = graphql.NewScalar(graphql.ScalarConfig{
	Name:        "Int64",
	Description: "A whole number that may need all 64 bits.",
	Serialize: func(value interface{}) interface{} {
		number, ok := asInt64(value)
		if !ok {
			return nil
		}
		return number
	},
	ParseValue: func(value interface{}) interface{} {
		number, ok := asInt64(value)
		if !ok {
			return nil
		}
		return number
	},
	ParseLiteral: func(value ast.Value) interface{} {
		literal, ok := value.(*ast.IntValue)
		if !ok {
			return nil
		}
		number, err := strconv.ParseInt(literal.Value, 10, 64)
		if err != nil {
			return nil
		}
		return number
	},
})

Int64 is a whole number too big for the 32 bits a GraphQL Int promises.

The library serializes Int through int32 and returns nil for anything past 2147483647. A summed token count is an int64 and is not nullable, so once the total passed two billion the whole query failed with "Cannot return null for non-nullable field" instead of returning the number. Fields and arguments declared int64, uint64 or uint use this scalar, which goes on the wire as a plain JSON number like Int does.

View Source
var JSON = graphql.NewScalar(graphql.ScalarConfig{
	Name: "JSON",
	Serialize: func(value interface{}) interface{} {
		raw, ok := value.(json.RawMessage)
		if !ok {
			if bytes, isBytes := value.([]byte); isBytes {
				raw = bytes
			} else {
				return value
			}
		}
		if len(raw) == 0 {
			return nil
		}
		var decoded interface{}
		if err := json.Unmarshal(raw, &decoded); err != nil {
			log.Errorf("graphapi: a JSON field does not hold JSON: %s", err)
			return nil
		}
		return decoded
	},
	ParseValue: func(value interface{}) interface{} {
		raw, err := json.Marshal(value)
		if err != nil {
			log.Errorf("graphapi: cannot read a JSON field: %s", err)
			return nil
		}
		return json.RawMessage(raw)
	},
	ParseLiteral: func(value ast.Value) interface{} {
		return nil
	},
})

JSON is a value that is already JSON: a json.RawMessage, sent as what it says rather than as the bytes that spell it.

It matters because reflect does not see a named type as its underlying one, so json.RawMessage — which is a []byte — did not match the []byte case above and fell through to the generic slice branch. An audit event's "before" and "after" came out as arrays of byte values: [123, 34, 105 ...], which is "{"i..." spelled one number at a time.

View Source
var Void = graphql.NewScalar(graphql.ScalarConfig{
	Name: "Void",
	Serialize: func(interface{}) interface{} {
		return nil
	},
})

Functions

func Selected

func Selected(ctx context.Context, selectionPath ...string) bool

Selected checks if a field is selected to be returned.

Types

type GraphAPI

type GraphAPI interface {
	// Return the generated graphql schema
	Build() (graphql.Schema, error)

	Register(query, mutation, subscription interface{}) error
}

func New

func New() GraphAPI

Jump to

Keyboard shortcuts

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