thistle

package module
v0.0.0-...-3712696 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 28 Imported by: 0

README

Thistle

Go Documentation builds.sr.ht status

Thistle is a GraphQL federation gateway which implements a subset of the Apollo Federation specification. It provides a simple and reliable way to combine multiple GraphQL APIs.

Usage

import "git.sr.ht/~adnano/thistle"

You'll need Go 1.16 or later.

Supported features

The following features are supported:

  • @key directives
  • @requires directives
  • @external directives
  • @inaccessible directives
  • File uploads

The following features are not supported:

  • Multiple @key directives on the same object
  • Nested fields in @key or @requires directives
  • @inaccessible directives at locations other than field definitions
  • Other Federation directives (@extends, @provides, @link, @shareable, @tag, @override, @composeDirective)
  • Custom Query or Mutation types
  • Subscriptions

See federation.graphql for the exact definitions used.

Comparison with other projects

Bramble and Nautilus are two other Go implementations of a GraphQL gateway, but they do not support Apollo Federation. Thistle began as a fork of Bramble.

Apollo Gateway and Apollo Router are non-free implementations of Apollo Federation in TypeScript and Rust. They are licensed under the non-free Elastic license.

graphql-go-tools is a Go implementation of Apollo Federation, but it is poorly documented, overcomplicated, and tries to do too much.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Prelude = &ast.Source{
	Name:    "federation.graphql",
	Input:   federationGraphql,
	BuiltIn: true,
}

Functions

func AddHeader

func AddHeader(ctx context.Context, key, value string) context.Context

AddHeader adds a HTTP header to downstream GraphQL requests for the current GraphQL operation.

Types

type Client

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

Client is a GraphQL client.

func NewClient

func NewClient(hc *http.Client) *Client

NewClient creates a new GraphQL client.

If hc is nil, http.DefaultClient will be used.

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, url string, op *Operation, data interface{}) error

Execute executes a GraphQL operation.

The data returned by the server will be decoded into the data argument.

func (*Client) WithUserAgent

func (c *Client) WithUserAgent(userAgent string) *Client

WithUserAgent sets the user agent used by the client.

type Gateway

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

Gateway is a GraphQL federation gateway.

Gateway implements gqlgen/graphql.ExecutableSchema.

func NewGateway

func NewGateway() *Gateway

NewGateway creates a new GraphQL federation gateway.

func (*Gateway) Complexity

func (g *Gateway) Complexity(typeName, fieldName string, childComplexity int, args map[string]interface{}) (int, bool)

Complexity returns the query complexity (unimplemented)

func (*Gateway) Exec

Exec returns the execution handler.

func (*Gateway) Execute

func (g *Gateway) Execute(ctx context.Context) *graphql.Response

Execute executes a GraphQL operation.

func (*Gateway) Schema

func (g *Gateway) Schema() *ast.Schema

Schema returns the federated schema.

func (*Gateway) SetSchema

func (g *Gateway) SetSchema(schema *Schema)

SetSchema sets the federated schema used by the gateway.

SetSchema is safe for concurrent use by multiple goroutines.

func (*Gateway) WithClient

func (g *Gateway) WithClient(client *Client) *Gateway

WithClient configures the client used for downstream GraphQL operations.

func (*Gateway) WithDebugExtensions

func (g *Gateway) WithDebugExtensions(enabled bool) *Gateway

WithDebugExtensions configures debugging information for GraphQL requests.

If enabled, debug information will be added to the response extensions. The following information will be provided:

  • plan: the query plan, including services and subqueries
  • timings: the execution times for the query

func (*Gateway) WithMaxRequests

func (g *Gateway) WithMaxRequests(maxRequests int64) *Gateway

WithMaxRequests configures the maximum number of requests to make per query.

type Operation

type Operation struct {
	Name      string                    `json:"operationName,omitempty"`
	Query     string                    `json:"query"`
	Variables map[string]interface{}    `json:"variables,omitempty"`
	Uploads   map[string]graphql.Upload `json:"-"`
}

Operation is a GraphQL operation.

type Response

type Response struct {
	Data   interface{}   `json:"data"`
	Errors gqlerror.List `json:"errors"`
}

Response is a GraphQL response.

type Schema

type Schema struct {
	ast.Schema
	// contains filtered or unexported fields
}

Schema represents a federated GraphQL schema.

func BuildSchema

func BuildSchema(services ...*Service) (*Schema, error)

BuildSchema builds and returns a new federated GraphQL schema.

func (*Schema) HasField

func (s *Schema) HasField(location, typeName, field string) bool

HasField reports whether the given location can resolve the given field.

func (*Schema) IsEntity

func (s *Schema) IsEntity(typeName string) bool

IsEntity reports whether the given type is an entity type.

func (*Schema) IsKey

func (s *Schema) IsKey(typeName string, field string) bool

IsKey reports whether the given field is a key field.

func (*Schema) Key

func (s *Schema) Key(typeName string) []string

Key returns a list of key fields for the given entity type.

func (*Schema) Requires

func (s *Schema) Requires(typeName string, field string) []string

Requires returns a list of fields required by the given field.

func (*Schema) Route

func (s *Schema) Route(parentType string, parentLocation string, input ast.SelectionSet) (map[string]ast.SelectionSet, error)

Route routes a selection set by location.

type Service

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

Service is a downstream GraphQL service.

func NewService

func NewService(name, url string) *Service

NewService creates a new downstream service.

func (*Service) Name

func (s *Service) Name() string

Name returns the downstream service name.

func (*Service) SDL

func (s *Service) SDL() string

SDL returns the downstream service definition.

func (*Service) SetSDL

func (s *Service) SetSDL(sdl string) error

SetSDL sets the service definition for the downstream service.

func (*Service) Status

func (s *Service) Status() Status

Status returns the downstream service status.

func (*Service) URL

func (s *Service) URL() string

URL returns the downstream service URL.

func (*Service) Update

func (s *Service) Update(ctx context.Context, client *Client) (bool, error)

Update fetches and validates the service definition from downstream and reports whether the service definition was updated.

type Status

type Status int

Status represents the status of a downstream service.

const (
	StatusNone Status = iota
	StatusOK
	StatusUnreachable
	StatusInvalid
)

Directories

Path Synopsis
Package fieldset provides functions for parsing _FieldSet scalars.
Package fieldset provides functions for parsing _FieldSet scalars.

Jump to

Keyboard shortcuts

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