gqlserver

package
v0.0.0-...-5d9ef32 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: NIST-PD-fallback Imports: 29 Imported by: 0

Documentation

Overview

Package gqlserver provides a GraphQL server. It is a singleton initialized via init() functions.

Index

Constants

This section is empty.

Variables

Scalar types.

View Source
var Schema = &graphql.SchemaConfig{
	Query: graphql.NewObject(graphql.ObjectConfig{
		Name:   "Query",
		Fields: graphql.Fields{},
	}),
	Mutation: graphql.NewObject(graphql.ObjectConfig{
		Name:   "Mutation",
		Fields: graphql.Fields{},
	}),
	Subscription: graphql.NewObject(graphql.ObjectConfig{
		Name:   "Subscription",
		Fields: graphql.Fields{},
	}),
}

Schema is the singleton of graphql.SchemaConfig. It is available until Prepare() is called.

Functions

func AddCounters

func AddCounters(cfg *CountersConfig)

AddCounters defines a counters-like field as both a query field and a subscription.

func AddMutation

func AddMutation(f *graphql.Field)

AddMutation adds a top-level mutation field.

func AddQuery

func AddQuery(f *graphql.Field)

AddQuery adds a top-level query field.

func AddSubscription

func AddSubscription(f *graphql.Field)

AddSubscription adds a top-level subscription field.

func BindArguments

func BindArguments[T any](m FieldTypes) graphql.FieldConfigArgument

BindArguments creates graphql.FieldConfigArgument from a struct.

func BindFields

func BindFields[T any](m FieldTypes) graphql.Fields

BindFields creates graphql.Fields from a struct. Field resolvers can accept either T or *T as source object.

func BindInputFields

func BindInputFields[T any](m FieldTypes) graphql.InputObjectConfigFieldMap

BindInputFields creates graphql.InputObjectConfigFieldMap from a struct.

func FieldDefToField

func FieldDefToField(d *graphql.FieldDefinition) *graphql.Field

FieldDefToField converts FieldDefinition to *Field.

func ImplementsInterface

func ImplementsInterface[T any](ot *graphql.Object, it *Interface)

ImplementsInterface records an object implementing an interface. This also appends the object to Schema.Types to ensure that it appears in the schema.

func NewListNonNullBoth

func NewListNonNullBoth(ofType graphql.Type) graphql.Type

NewListNonNullBoth constructs [T!]! type.

func NewListNonNullElem

func NewListNonNullElem(ofType graphql.Type) graphql.Type

NewListNonNullElem constructs [T!] type.

func NewListNonNullList

func NewListNonNullList(ofType graphql.Type) graphql.Type

NewListNonNullList constructs [T]! type.

func NewStringEnum

func NewStringEnum[T ~string](name, desc string, values ...T) *graphql.Enum

NewStringEnum constructs an enum type.

func Optional

func Optional(value any) any

Optional turns zero value to nil.

func Prepare

func Prepare()

Prepare compiles the schema and adds handlers on http.DefaultServeMux.

func PublishChan

func PublishChan(f func(updates chan<- any)) (any, error)

PublishChan publishes a channel in reply to GraphQL subscription.

f is a callback function that sends its results into a channel. It should not close the channel - the channel will be closed by the caller when f returns.

func PublishInterval

func PublishInterval(p graphql.ResolveParams, read graphql.FieldResolveFn, enders ...any) (results any, e error)

PublishInterval publishes results at an interval in reply to GraphQL subscription.

read is a callback function that returns a single result. enders are channels that indicate the subscription should be canceled, when a value is received or the channel is closed.

func RetrieveNode

func RetrieveNode(id string) (any, error)

RetrieveNode retrieves a node.

Types

type CountersConfig

type CountersConfig struct {
	Description string

	// Parent is the parent object to define a query field.
	// If nil, no query field is defined.
	Parent *graphql.Object
	// Name is the field name to be defined in Parent.
	// If empty, no query field is defined.
	Name string

	// Subscription is the field name to be defined as subscription.
	// If empty, no subscription is defined.
	Subscription string
	// NoDiff indicates diff=true is not supported.
	// This should be set if some fields are lazily-injected with FieldResolveFn, as subtract algorithm cannot see them.
	NoDiff bool
	// FindArgs declares GraphQL arguments for finding the source object.
	FindArgs graphql.FieldConfigArgument
	// Find finds source object from FindArgs in a subscription.
	// p.Source is unspecified.
	// p.Args contains arguments declared in both Args and FindArgs.
	// enders: see PublishInterval.
	Find func(p graphql.ResolveParams) (source any, enders []any, e error)

	// Type declares GraphQL return type.
	Type graphql.Output
	// Args declares GraphQL arguments for customizing counters.
	Args graphql.FieldConfigArgument
	// Read retrieves counters from p.Source.
	// p.Source is a value from Parent or a return value from Find.
	// p.Args contains arguments declared in Args.
	Read graphql.FieldResolveFn
}

CountersConfig contains options to publish a counters-like type.

type FieldTypes

type FieldTypes map[reflect.Type]graphql.Type

FieldTypes contains known GraphQL types of fields.

type Interface

type Interface struct {
	Interface *graphql.Interface
	// contains filtered or unexported fields
}

Interface defines a GraphQL interface.

func NewInterface

func NewInterface(ic graphql.InterfaceConfig) (it *Interface)

NewInterface creates a GraphQL interface. ic.Fields should contain necessary fields. ic.ResolveType will be overwritten.

func (*Interface) AppendTo

func (it *Interface) AppendTo(oc *graphql.ObjectConfig)

AppendTo appends this interface to oc.Interfaces slice.

func (*Interface) CopyFieldsTo

func (it *Interface) CopyFieldsTo(fieldsAny any) graphql.Fields

CopyFieldsTo adds fields of this interface into InterfaceConfig.Fields or ObjectConfig.Fields. If a field of same name exists at the destination, only Type and Description are overwritten. This may be used for inheritance between interfaces or for sharing implementation.

type NodeConfig

type NodeConfig[T any] struct {
	// GetID extracts un-prefixed ID from the source object.
	GetID func(source T) string

	// Retrieve fetches an object from un-prefixed ID.
	// Returning zero value indicates the object does not exist.
	Retrieve func(id string) T

	// RetrieveInt fetches an object from un-prefixed ID parsed as integer.
	RetrieveInt func(id int) T

	// Delete deletes the source object.
	Delete func(source T) error
}

NodeConfig contains options to construct a NodeType.

type NodeType

type NodeType[T any] struct {
	NodeTypeBase
	// contains filtered or unexported fields
}

NodeType defines a Node subtype.

func NewNodeType

func NewNodeType[T any](oc graphql.ObjectConfig, nc NodeConfig[T]) (nt *NodeType[T])

NewNodeType creates a NodeType.

func (*NodeType[T]) Retrieve

func (nt *NodeType[T]) Retrieve(id string) (node T)

Retrieve retrieves node by ID.

type NodeTypeBase

type NodeTypeBase struct {
	Object *graphql.Object
	// contains filtered or unexported fields
}

NodeTypeBase contains non-generic fields of NodeType.

Directories

Path Synopsis
Package gqlsingleton provides singleton objects in GraphQL server.
Package gqlsingleton provides singleton objects in GraphQL server.

Jump to

Keyboard shortcuts

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