handler

package
v0.0.0-...-32f7d7b Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2019 License: ISC, MIT Imports: 20 Imported by: 0

Documentation

Overview

Package handler helps build GraphQL services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(schema graphql.Schema, opts ...Option) (http.Handler, error)

New creates a net/http.Handler and builds a GraphQL web service to serve queries against the schema.

Types

type DefaultErrorPresenter

type DefaultErrorPresenter struct {
	// ResultPresenter is used to present ErrPrepare.Errs in an ExecutionResult.
	ResultPresenter ResultPresenter
}

DefaultErrorPresenter implements an ErrorPresenter which is default used by HTTP handler when no error presenter is provided.

func (DefaultErrorPresenter) Write

func (presenter DefaultErrorPresenter) Write(w http.ResponseWriter, err error)

Write implements ErrorPresenter.

type DefaultRequestBuilder

type DefaultRequestBuilder struct {
	Config *DefaultRequestBuilderConfig
}

DefaultRequestBuilder implements the default request builder used by HTTP handler to obtain a Request object from a http.Request.

func (DefaultRequestBuilder) Build

func (builder DefaultRequestBuilder) Build(r *http.Request, h HTTPHandler) (*Request, error)

Build implements RequestBuilder.

type DefaultRequestBuilderConfig

type DefaultRequestBuilderConfig struct {
	HTTPRequestParserOptions ParseHTTPRequestOptions
	QueryParserOptions       []parser.ParseOption
	DefaultFieldResolver     graphql.FieldResolver
}

DefaultRequestBuilderConfig specifies settings to configure DefaultRequestBuilder.

type DefaultResultPresenter

type DefaultResultPresenter struct{}

DefaultResultPresenter implements a ResultPresenter used by HTTP handler to present an ExecutionResult.

func (DefaultResultPresenter) Write

func (DefaultResultPresenter) Write(
	w http.ResponseWriter,
	httpRequest *http.Request,
	graphqlRequest *Request,
	result *executor.ExecutionResult)

type ErrEmptyQuery

type ErrEmptyQuery struct {
	Request *http.Request
}

ErrEmptyQuery describes an error when an empty query is not allowed.

func (ErrEmptyQuery) Error

func (err ErrEmptyQuery) Error() string

Error implements Go's error interface.

type ErrParseQuery

type ErrParseQuery struct {
	Request       *http.Request
	ParsedRequest *HTTPRequest
	Err           error
}

ErrParseQuery describes an invalid GraphQL query document that failed parsing.

func (*ErrParseQuery) Error

func (err *ErrParseQuery) Error() string

Error implements Go's error interface.

type ErrPrepare

type ErrPrepare struct {
	Request       *http.Request
	ParsedRequest *HTTPRequest
	Document      ast.Document
	Errs          graphql.Errors
}

ErrPrepare indicates a failure in prepare a PreparedOperation for execution for a query.

func (*ErrPrepare) Error

func (err *ErrPrepare) Error() string

Error implements Go's error interface.

type ErrorPresenter

type ErrorPresenter interface {
	// Write sends the given error to w.
	Write(w http.ResponseWriter, err error)
}

ErrorPresenter presents an error to a http.ResponseWriter.

type HTTPHandler

type HTTPHandler interface {
	// Schema served by this handler
	Schema() graphql.Schema

	// OperationCache for the parsed queries
	OperationCache() OperationCache
}

HTTPHandler provides interfaces to access settings in httpHandler from RequestBuilder.

type HTTPRequest

type HTTPRequest struct {
	Query         string                 `json:"query"`
	OperationName string                 `json:"operationName"`
	Variables     map[string]interface{} `json:"variables"`
}

HTTPRequest contains result values of ParseHTTPRequest.

func ParseHTTPRequest

func ParseHTTPRequest(r *http.Request, options *ParseHTTPRequestOptions) (*HTTPRequest, error)

ParseHTTPRequest parses a GraphQL request from a http.Request object.

type HTTPRequestParseError

type HTTPRequestParseError struct {
	Request *http.Request
	Options *ParseHTTPRequestOptions
	Err     error
}

HTTPRequestParseError is returned by ParseHTTPRequest when parsing failed.

func (*HTTPRequestParseError) Error

func (err *HTTPRequestParseError) Error() string

Error implements Go's error interface.

type LLConfig

type LLConfig struct {
	// Schema to be working on
	Schema graphql.Schema

	// OperationCache caches graphql.PreparedOperation created from a query to save parsing efforts.
	OperationCache OperationCache

	// Middlewares to be applied before executing a Request
	Middlewares []RequestMiddleware
}

LLConfig contains configuration to set up a LLHandler.

type LLHandler

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

LLHandler creates a handler that is suit for serving GraphQL queries against a schema in a long-running process. It is useful as a low-level building block for building GraphQL services such as GraphQL web services.

func NewLLHandler

func NewLLHandler(config *LLConfig) (*LLHandler, error)

NewLLHandler creates a LLHandler from given configuration.

func (*LLHandler) OperationCache

func (handler *LLHandler) OperationCache() OperationCache

OperationCache returns handler.cache.

func (*LLHandler) Schema

func (handler *LLHandler) Schema() graphql.Schema

Schema returns handler.schema.

func (*LLHandler) Serve

func (handler *LLHandler) Serve(request *Request) *executor.ExecutionResult

Serve executes the operation with given context and parameters. The given request object must not be nil.

type LRUOperationCache

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

LRUOperationCache is a thread-safe LRU cache that implements OperationCache. It serves as default operation cache for LLHandler. Most part of implementation directly derived from groupcache/lru [0] with sync.RWLock added to make it safe for concurrent access.

func NewLRUOperationCache

func NewLRUOperationCache(maxEntries uint) (*LRUOperationCache, error)

NewLRUOperationCache creates a new LRUOperationCache with given size.

func (*LRUOperationCache) Add

func (c *LRUOperationCache) Add(query string, operation *executor.PreparedOperation)

Add implements OperationCache.

func (*LRUOperationCache) Get

func (c *LRUOperationCache) Get(query string) (operation *executor.PreparedOperation, ok bool)

Get implements OperationCache.

type NopOperationCache

type NopOperationCache struct{}

NopOperationCache does nothing.

func (NopOperationCache) Add

func (NopOperationCache) Add(query string, operation *executor.PreparedOperation)

Add implements OperationCache.

func (NopOperationCache) Get

func (NopOperationCache) Get(query string) (operation *executor.PreparedOperation, ok bool)

Get implements OperationCache.

type OperationCache

type OperationCache interface {
	// Get looks up operation for the given query.
	Get(query string) (operation *executor.PreparedOperation, ok bool)

	// Add adds an operation that associated with the query to the cache.
	Add(query string, operation *executor.PreparedOperation)
}

OperationCache caches executor.PreparedOperation created from a query to save parsing efforts.

type Option

type Option func(h *httpHandlerConfig)

Option configures httpHandler

func DefaultFieldResolver

func DefaultFieldResolver(resolver graphql.FieldResolver) Option

DefaultFieldResolver sets the resolver to be used when a field doesn't provide one.

func MaxBodySize

func MaxBodySize(size uint) Option

MaxBodySize sets the maximum number of bytes to be read from request body for ParseHTTPRequest called by DefaultRequestBuilder.

func OverrideErrorPresenter

func OverrideErrorPresenter(errorPresenter ErrorPresenter) Option

OverrideErrorPresenter overrides default RequestBuilder.

func OverrideOperationCache

func OverrideOperationCache(cache OperationCache) Option

OverrideOperationCache that overrides default OperationCache.

func OverrideRequestBuilder

func OverrideRequestBuilder(requestBuilder RequestBuilder) Option

OverrideRequestBuilder overrides default RequestBuilder.

func OverrideResultPresenter

func OverrideResultPresenter(resultPresenter ResultPresenter) Option

OverrideResultPresenter overrides default ResultPresenter.

func QueryParserOptions

func QueryParserOptions(options ...parser.ParseOption) Option

QueryParserOptions provides settings to the parser for GraphQL query.

type ParseHTTPRequestOptions

type ParseHTTPRequestOptions struct {
	// Maximum size in bytes to be read when parsing a GraphQL query from HTTP request body. If it is
	// not set, the size is capped at 10MB.
	MaxBodySize uint
}

ParseHTTPRequestOptions provides settings to ParseHTTPRequest.

type Request

type Request struct {
	Ctx         context.Context
	Operation   *executor.PreparedOperation
	ExecuteOpts []executor.ExecuteOption
}

Request contains parameter required by Serve.

type RequestBuilder

type RequestBuilder interface {
	// Build turns a http.Request r into a Request for h. w is used for sending errors.
	Build(r *http.Request, h HTTPHandler) (*Request, error)
}

RequestBuilder generates a Request to be served by LLHandler from an HTTP request.

type RequestMiddleware

type RequestMiddleware interface {
	// Apply modifies request. next specifies the next action to do after applying the middleware.
	Apply(request *Request, next *RequestMiddlewareNext)
}

RequestMiddleware applies changes on Request before its operation gets executed. It can be used to modify ExecuteParams in Request such as setting root values and/or supplied app-specific context.

type RequestMiddlewareNext

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

RequestMiddlewareNext is provided to a RequestMiddleware to specify the next action to do.

func (*RequestMiddlewareNext) Next

func (next *RequestMiddlewareNext) Next(request *Request)

Next continues applying the next middleware in the chain.

func (*RequestMiddlewareNext) NextError

func (next *RequestMiddlewareNext) NextError(err *graphql.Error)

NextError stops applying rest middlewares in the chain and sends an ExecutionResult that includes given error.

func (*RequestMiddlewareNext) NextResult

func (next *RequestMiddlewareNext) NextResult(result *executor.ExecutionResult)

NextResult stops applying rest middlewares in the chain and sends the result.

type ResultPresenter

type ResultPresenter interface {
	// Write writes an ExecutionResult to w.
	Write(
		w http.ResponseWriter,
		httpRequest *http.Request,
		graphqlRequest *Request,
		result *executor.ExecutionResult)
}

ResultPresenter presents an execution result to a http.ResponseWriter.

Jump to

Keyboard shortcuts

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