Documentation ¶
Index ¶
- Constants
- func AugmentGraphError(err error, message string, pos lexer.Position, paths ...string) error
- type EnumUnmarshaler
- type EnumValue
- type ErrorLocation
- type FunctionDefinition
- type GraphError
- type GraphFunctionMode
- type GraphFunctionParamType
- type GraphHttpHandler
- type GraphRequestCache
- type GraphTypeExtension
- type GraphTypeInfo
- type Graphy
- func (g *Graphy) EnableIntrospection(ctx context.Context)
- func (g *Graphy) HttpHandler() http.Handler
- func (g *Graphy) ProcessRequest(ctx context.Context, request string, variableJson string) (string, error)
- func (g *Graphy) RegisterAnyType(ctx context.Context, types ...any)
- func (g *Graphy) RegisterFunction(ctx context.Context, def FunctionDefinition)
- func (g *Graphy) RegisterMutation(ctx context.Context, name string, f any, names ...string)
- func (g *Graphy) RegisterQuery(ctx context.Context, name string, f any, names ...string)
- func (g *Graphy) RegisterTypes(ctx context.Context, types ...any)
- func (g *Graphy) SchemaDefinition(ctx context.Context) string
- type RequestStub
- type RequestType
- type StringEnumValues
- type TypeKind
- type UnknownCommandError
Constants ¶
const ( IntrospectionKindScalar __TypeKind = "SCALAR" IntrospectionKindObject __TypeKind = "OBJECT" IntrospectionKindInterface __TypeKind = "INTERFACE" IntrospectionKindUnion __TypeKind = "UNION" IntrospectionKindEnum __TypeKind = "ENUM" IntrospectionKindInputObject __TypeKind = "INPUT_OBJECT" IntrospectionKindList __TypeKind = "LIST" IntrospectionKindNonNull __TypeKind = "NON_NULL" )
const ( FieldTypeField fieldType = iota FieldTypeGraphFunction )
Variables ¶
This section is empty.
Functions ¶
func AugmentGraphError ¶
AugmentGraphError wraps the provided error with additional context, creating or augmenting a GraphError structure. This function serves to enrich errors with Graph-specific details such as file position, path, and a custom message.
The function primarily handles two cases:
- If the passed error is already a GraphError, it augments the existing GraphError with the provided context without losing any existing data.
- If the passed error is not a GraphError, it wraps it within a new GraphError, setting the provided context.
Parameters:
- err: The error to be wrapped or augmented. It can be a regular error or a GraphError.
- message: A custom message to be set on the GraphError. If a GraphError is passed and it already contains a message, this parameter is ignored.
- pos: A lexer.Position structure indicating where in the source the error occurred. If a GraphError is passed and it already contains location data, this parameter is ignored.
- paths: A variadic slice of strings indicating the path in the graph where the error occurred. These are prepended to any existing paths in a GraphError.
Returns: - A GraphError containing the augmented or wrapped error details.
Types ¶
type EnumUnmarshaler ¶
EnumUnmarshaler provides an interface for types that can unmarshal a string representation into their enumerated type. This is useful for types that need to convert a string, typically from external sources like JSON or XML, into a specific enumerated type in Go.
UnmarshalString should return the appropriate enumerated value for the given input string, or an error if the input is not valid for the enumeration.
type ErrorLocation ¶
ErrorLocation provides details about where in the source a particular error occurred.
Fields: - Line: The line number of the error. - Column: The column number of the error.
func (ErrorLocation) String ¶
func (e ErrorLocation) String() string
type FunctionDefinition ¶
type FunctionDefinition struct { // Name is the name of the function. // This is used to map the function to the GraphQL query. Name string // Function is the function to call. Function any // ParameterNames is a list of parameter names to use for the function. Since reflection // doesn't provide parameter names, this is needed to map the parameters to the function. // This is used when the function can have anonymous parameters otherwise. ParameterNames []string // ReturnAnyOverride is a list of types that may be returned as `any` when returned from // the function. This is a function-specific override to the global `any` types that are // on the base Graphy instance. ReturnAnyOverride []any // Mode controls how the function is to be run. Mutators are functions that change the // state of the system. They will be called sequentially and in the order they are referred // to in the query. Regular queries are functions that do not change the state of the // system. They will be called in parallel. Mode GraphFunctionMode // ReturnEnumName is used to provide a custom name for implicit return unions. If this is // not set the default name is the name of the function followed by "ResultUnion". ReturnUnionName string // Description is used to provide a description for the function. This will be used in the // schema. Description *string // DeprecatedReason is used to mark a function as deprecated. This will cause the function to // be marked as deprecated in the schema. DeprecatedReason *string }
type GraphError ¶
type GraphError struct { Message string `json:"message"` Locations []ErrorLocation `json:"locations,omitempty"` Path []string `json:"path,omitempty"` Extensions map[string]string `json:"extensions,omitempty"` InnerError error `json:"-"` }
GraphError represents an error that occurs within a graph structure. It provides a structured way to express errors with added context, such as their location in the source and any associated path.
Fields: - Message: The main error message. - Locations: A slice of ErrorLocation structs that detail where in the source the error occurred. - Path: Represents the path in the graph where the error occurred. - Extensions: A map containing additional error information not part of the standard fields. - InnerError: An underlying error that might have caused this GraphError. It is not serialized to JSON.
func NewGraphError ¶
func NewGraphError(message string, pos lexer.Position, paths ...string) GraphError
NewGraphError creates a new GraphError with the provided message, position, and path. It uses the position to create an ErrorLocation structure and adds it to the Locations field.
func (*GraphError) AddExtension ¶
func (e *GraphError) AddExtension(key string, value string)
AddExtension adds a key-value pair to the Extensions field of a GraphError. Extensions in a GraphError provide a way to include additional error information that is not part of the standard error fields.
If the Extensions map is nil, it will be initialized before adding the key-value pair.
Parameters: - key: The key for the extension. It represents the name or identifier for the additional data. - value: The value associated with the key. It provides extra context or data for the error.
func (GraphError) MarshalJSON ¶
func (e GraphError) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for GraphError. This allows us to format the error in the way that GraphQL expects.
func (GraphError) Unwrap ¶
func (e GraphError) Unwrap() error
type GraphFunctionMode ¶
type GraphFunctionMode int
const ( ModeQuery GraphFunctionMode = iota ModeMutation )
type GraphFunctionParamType ¶
type GraphFunctionParamType int
const ( NamedParamsStruct GraphFunctionParamType = iota NamedParamsInline AnonymousParamsInline )
type GraphHttpHandler ¶ added in v0.5.0
type GraphHttpHandler struct {
// contains filtered or unexported fields
}
func (GraphHttpHandler) ServeHTTP ¶ added in v0.5.0
func (g GraphHttpHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request)
type GraphRequestCache ¶
type GraphRequestCache interface { // GetRequestStub returns the request stub for a request. It should return nil if the request // is not cached. The error can either be the cached error or an error indicating a cache error. // In case the request is not cached, the returned *RequestStub should be nil. GetRequestStub(ctx context.Context, request string) (*RequestStub, error) // SetRequestStub sets the request stub for a request. SetRequestStub(ctx context.Context, request string, stub *RequestStub, err error) }
GraphRequestCache represents an interface for caching request stubs associated with graph requests. Implementations of this interface provide mechanisms to store and retrieve `RequestStub`s, allowing for optimizations and reduced processing times in graph operations. Note that the `RequestStub` is an internal representation of a graph request, and is not intended to be used directly by consumers. It is not serializable to JSON and needs to be kept in memory.
type GraphTypeExtension ¶ added in v0.7.1
type GraphTypeExtension interface {
GraphTypeExtension() GraphTypeInfo
}
type GraphTypeInfo ¶ added in v0.7.1
type GraphTypeInfo struct { // Name is the name of the type. Name string // Description is the description of the type. Description string // Deprecated is the deprecation status of the type. Deprecated string // Function overrides for the type. FunctionDefinitions []FunctionDefinition }
type Graphy ¶
type Graphy struct { RequestCache GraphRequestCache EnableTiming bool // contains filtered or unexported fields }
Graphy is the main entry point for the go-quickgraph library. This holds all the registered functions and types and provides methods for executing requests. It is safe to use concurrently once it has been initialized -- there is no guarantee that the initialization is thread-safe.
The zero value for Graphy is safe to use.
The RequestCache is optional, but it can be used to cache the results of parsing requests. This can be useful if you are using the library in a server environment and want to cache the results of parsing requests to speed things up. Refer to GraphRequestCache for more information.
func (*Graphy) EnableIntrospection ¶ added in v0.7.0
func (*Graphy) HttpHandler ¶ added in v0.5.0
func (*Graphy) ProcessRequest ¶
func (*Graphy) RegisterAnyType ¶
RegisterAnyType registers a type that is potentially used as a return type for a function that returns `any`. This isn't critical to use all cases, but it can be needed for results that contain functions that can be called. Without this, those functions would not be found -- this it needed to infer the types of parameters in cases those are fulfilled with variables.
func (*Graphy) RegisterFunction ¶
func (g *Graphy) RegisterFunction(ctx context.Context, def FunctionDefinition)
RegisterFunction is similar to both RegisterQuery and RegisterMutation, but it allows the caller to specify additional parameters that are less commonly used. See the FunctionDefinition documentation for more information.
func (*Graphy) RegisterMutation ¶ added in v0.4.0
RegisterMutation registers a function as a mutator.
The function must return a valid result value and may return an error. If the function returns multiple values, they must be pointers and the result will be an implicit union type.
If the names are specified, they must match the non-context parameter count of the function. If the names are not specified, then the parameters are dealt with as either anonymous parameters or as a single parameter that is a struct. If the function has a single parameter that is a struct, then the names of the struct fields are used as the parameter names.
func (*Graphy) RegisterQuery ¶ added in v0.4.0
RegisterQuery registers a function as a query.
The function must return a valid result value and may return an error. If the function returns multiple values, they must be pointers and the result will be an implicit union type.
If the names are specified, they must match the non-context parameter count of the function. If the names are not specified, then the parameters are dealt with as either anonymous parameters or as a single parameter that is a struct. If the function has a single parameter that is a struct, then the names of the struct fields are used as the parameter names.
func (*Graphy) RegisterTypes ¶ added in v0.7.1
RegisterTypes is a method on the Graphy struct that registers types that implement interfaces. This is useful for discovering types that implement certain interfaces. The method takes in a context and a variadic parameter of types (of any kind). It iterates over the provided types and performs a type lookup for each type.
Parameters: - ctx: The context within which the method operates. - types: A variadic parameter that represents instances ot types to be registered.
Usage: g := &Graphy{} g.RegisterTypes(context.Background(), Type1{}, Type2{}, Type3{})
type RequestStub ¶
type RequestStub struct {
// contains filtered or unexported fields
}
RequestStub represents a stub of a GraphQL-like request. It contains the Graphy instance, the mode of the request (Query or Mutation), the commands to execute, and the variables used in the request.
func (*RequestStub) Name ¶ added in v0.7.2
func (r *RequestStub) Name() string
type RequestType ¶
type RequestType int
RequestType is an enumeration of the types of requests. It can be a Query or a Mutation.
const ( RequestQuery RequestType = iota RequestMutation )
type StringEnumValues ¶
type StringEnumValues interface {
EnumValues() []EnumValue
}
StringEnumValues provides an interface for types that can return a list of valid string representations for their enumeration. This can be useful in scenarios like validation or auto-generation of documentation where a list of valid enum values is required.
EnumValues should return a slice of strings representing the valid values for the enumeration.
type UnknownCommandError ¶
type UnknownCommandError struct { GraphError Commands []string }
UnknownCommandError is a specific type of GraphError that occurs when an unknown command is encountered. It embeds the GraphError and adds a Commands field which contains a list of commands that were unrecognized.
func (UnknownCommandError) Unwrap ¶
func (e UnknownCommandError) Unwrap() error