Gateway Fields

There are some situations where it makes sense to have fields that resolve at the gateway. One common example is the viewer field that resolves to the User type representing the current user. This can be achieved by adding a custom field to the gateway whose resolver returns the id of the entity.

import (
	// ...
	"github.com/vektah/gqlparser/ast"
)

viewerField := &gateway.QueryField{
	Name: "viewer",
	Type: ast.NamedType("User", &ast.Position{}),
	Resolver: func(ctx context.Context, args map[string]interface{}) (string, error) {
		// for now just return the value in context
		return ctx.Value("user-id").(string), nil
	},
}

// ... somewhere else ...

gateway.New(..., gateway.withFields(viewerField))