federation

package
v0.0.0-...-2f911e3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 12 Imported by: 0

README

Federation plugin

Add support for graphql federation in your graphql Go server!

TODO(miguel): add details.

Tests

There are several different tests. Some will process the configuration file directly. You can see those in the federation_test.go. There are also tests for entity resolvers, which will simulate requests from a federation server like Apollo Federation.

Running entity resolver tests.

  1. Go to plugin/federation
  2. Run the command go generate
  3. Run the tests with go test ./....

Architecture

TODO(miguel): add details.

Entity resolvers - GetMany entities

The federation plugin implements GetMany semantics in which entity resolvers get the entire list of representations that need to be resolved. This functionality is currently option tho, and to enable it you need to specify the directive @entityResolver in the federated entity you want this feature for. E.g.

directive @entityResolver(multi: Boolean) on OBJECT

type MultiHello @key(fields: "name") @entityResolver(multi: true) {
    name: String!
}

That allows the federation plugin to generate GetMany resolver function that can take a list of representations to be resolved.

From that entity type, the resolver function would be

func (r *entityResolver) FindManyMultiHellosByName(ctx context.Context, reps []*generated.ManyMultiHellosByNameInput) ([]*generated.MultiHello, error) {
  /// <Your code to resolve the list of items>
}

Note: If you are using omit_slice_element_pointers: true option in your config yaml, your GetMany resolver will still generate in the example above the same signature FindManyMultiHellosByName(ctx context.Context, reps []*generated.ManyMultiHellosByNameInput) ([]*generated.MultiHello, error). But all other instances will continue to honor omit_slice_element_pointers: true

Documentation

Index

Constants

View Source
const DirArgFields = "fields"

The fields arguments must be provided to both key and requires directives.

View Source
const DirArgType = "type"

Tells the code generator what type the directive is referencing

View Source
const DirNameEntityReference = "entityReference"

Variables

This section is empty.

Functions

This section is empty.

Types

type Entity

type Entity struct {
	Name      string // The same name as the type declaration
	Def       *ast.Definition
	Resolvers []*EntityResolver
	Requires  []*Requires
	Multi     bool
	Type      types.Type
}

Entity represents a federated type that was declared in the GQL schema.

func (Entity) GetTypeInfo

func (e Entity) GetTypeInfo() string

GetTypeInfo - get the imported package & type name combo. package.TypeName

type EntityResolver

type EntityResolver struct {
	ResolverName   string      // The resolver name, such as FindUserByID
	KeyFields      []*KeyField // The fields declared in @key.
	InputType      types.Type  // The Go generated input type for multi entity resolvers
	InputTypeName  string
	ReturnType     types.Type // The Go generated return type for the entity
	ReturnTypeName string
}

func (*EntityResolver) LookupInputType

func (e *EntityResolver) LookupInputType() string

type Federation

type Federation struct {
	Entities       []*Entity
	PackageOptions PackageOptions
	// contains filtered or unexported fields
}

func New

func New(version int, cfg *config.Config) (*Federation, error)

New returns a federation plugin that injects federated directives and types into the schema

func (*Federation) GenerateCode

func (f *Federation) GenerateCode(data *codegen.Data) error

func (*Federation) InjectSourcesEarly

func (f *Federation) InjectSourcesEarly() ([]*ast.Source, error)

func (*Federation) InjectSourcesLate

func (f *Federation) InjectSourcesLate(schema *ast.Schema) ([]*ast.Source, error)

InjectSourceLate creates a GraphQL Entity type with all the fields that had the @key directive

func (*Federation) MutateConfig

func (f *Federation) MutateConfig(cfg *config.Config) error

MutateConfig mutates the configuration

func (*Federation) Name

func (f *Federation) Name() string

Name returns the plugin name

type KeyField

type KeyField struct {
	Definition *ast.FieldDefinition
	Field      fieldset.Field        // len > 1 for nested fields
	Type       *config.TypeReference // The Go representation of that field type
}

type PackageOptions

type PackageOptions struct {
	// ExplicitRequires will generate a function in the execution context
	// to populate fields using the @required directive into the entity.
	//
	// You can only set one of ExplicitRequires or ComputedRequires to true.
	ExplicitRequires bool
	// ComputedRequires generates resolver functions to compute values for
	// fields using the @required directive.
	ComputedRequires bool
}

type Requires

type Requires struct {
	Name  string                // the name of the field
	Field fieldset.Field        // source Field, len > 1 for nested fields
	Type  *config.TypeReference // The Go representation of that field type
}

Requires represents an @requires clause

Directories

Path Synopsis
test_data

Jump to

Keyboard shortcuts

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