Documentation
¶
Overview ¶
Package graphql provides utilities for working with GraphQL.
Package graphql provides utilities for working with GraphQL.
Package graphql provides utilities for working with GraphQL.
Index ¶
- Variables
- func CheckAuthorization(ctx context.Context, allowedRoles []string, requiredScopes []string, ...) error
- func ConvertRolesToStrings[T fmt.Stringer](roles []T) []string
- func HandleError(ctx context.Context, err error, operation string, ...) error
- func IsAuthorizedDirective(ctx context.Context, obj interface{}, next graphql.Resolver, ...) (interface{}, error)
- func NewServer(schema graphql.ExecutableSchema, logger *logging.ContextLogger, ...) *handler.Server
- type ServerConfig
Constants ¶
This section is empty.
Variables ¶
var ( // AuthorizationCheckDuration measures the duration of authorization checks AuthorizationCheckDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ Name: "authorization_check_duration_seconds", Help: "Duration of authorization checks in seconds", Buckets: prometheus.DefBuckets, }) // AuthorizationFailures counts the number of failed authorization attempts AuthorizationFailures = prometheus.NewCounter(prometheus.CounterOpts{ Name: "authorization_failures_total", Help: "Total number of authorization failures", }) )
Functions ¶
func CheckAuthorization ¶
func CheckAuthorization(ctx context.Context, allowedRoles []string, requiredScopes []string, resource string, operation string, logger *logging.ContextLogger) error
CheckAuthorization is a helper function to check if the user is authorized to perform an operation It can be used in resolvers to perform custom authorization checks
Parameters:
- ctx: The context of the request
- allowedRoles: The roles that are allowed to perform the operation
- requiredScopes: The scopes that are required to perform the operation (optional)
- resource: The resource being accessed (optional)
- operation: The name of the operation being performed
- logger: A context logger for logging (optional, can be nil)
Returns:
- An error if not authorized, nil if authorized
func ConvertRolesToStrings ¶
ConvertRolesToStrings converts a slice of enum roles to a slice of strings This is useful when working with generated enum types for roles
Parameters:
- roles: A slice of role enums that have a String() method
Returns:
- A slice of strings representing the roles
func HandleError ¶
func HandleError(ctx context.Context, err error, operation string, logger *logging.ContextLogger) error
HandleError processes an error and returns an appropriate GraphQL error. It logs the error and converts it to a GraphQL error with appropriate extensions. Parameters:
- ctx: The context containing trace information
- err: The error to handle
- operation: The name of the operation that caused the error
- logger: The logger to use for logging the error
Returns:
- error: A GraphQL error with appropriate extensions
func IsAuthorizedDirective ¶
func IsAuthorizedDirective(ctx context.Context, obj interface{}, next graphql.Resolver, allowedRoles []string, requiredScopes []string, resource string, logger *logging.ContextLogger) (interface{}, error)
IsAuthorizedDirective implements the @isAuthorized directive for GraphQL It checks if the user has any of the allowed roles and all of the required scopes for the specified resource
Parameters:
- ctx: The context of the request
- obj: The object being resolved
- next: The next resolver in the chain
- allowedRoles: The roles that are allowed to access this field
- requiredScopes: The scopes that are required to access this field (optional)
- resource: The resource being accessed (optional)
- logger: A context logger for logging (optional, can be nil)
Returns:
- The result of the next resolver if authorized
- An error if not authorized
func NewServer ¶
func NewServer(schema graphql.ExecutableSchema, logger *logging.ContextLogger, cfg ServerConfig) *handler.Server
NewServer creates a new GraphQL server with the given schema and configuration
Types ¶
type ServerConfig ¶
ServerConfig contains configuration for the GraphQL server
func DefaultServerConfig ¶ added in v1.5.0
func DefaultServerConfig() ServerConfig
DefaultServerConfig returns a default configuration for the GraphQL server. This is an alias for NewDefaultServerConfig for compatibility with the configscan tool.
func NewDefaultServerConfig ¶
func NewDefaultServerConfig() ServerConfig
NewDefaultServerConfig creates a new server configuration with default values