Documentation
¶
Overview ¶
Package common contains common utilities and types for the GraphQL API.
Index ¶
- Constants
- Variables
- func AddAllExtensions(h *handler.Server)
- func CheckAllowedAuthType(ctx context.Context) error
- func ConvertToObject[J any](obj any) (*J, error)
- func GetBulkUploadOwnerInput[T any](input []*T) (*string, error)
- func GetConstraintField(err error, object string) string
- func GetOrgOwnerFromInput[T any](input *T) (*string, error)
- func IsEmpty(x any) bool
- func NewCascadeDeleteError(ctx context.Context, err error) error
- func SetOrganizationInAuthContext(ctx context.Context, inputOrgID *string) error
- func SetOrganizationInAuthContextBulkRequest[T any](ctx context.Context, input []*T) error
- func UnmarshalBulkData[T any](input graphql.Upload) ([]*T, error)
- func WithContextLevelCache(h *handler.Server)
- func WithFileUploader(h *handler.Server, u *objects.Service)
- func WithMetrics(h *handler.Server)
- func WithResultLimit(h *handler.Server, limit *int)
- func WithSkipCache(h *handler.Server)
- type Action
- type AlreadyExistsError
- type Auth
- type ComplexityLimitWithMetrics
- type ComplexityStats
- type ForeignKeyError
- type NotFoundError
- type ValidationError
Constants ¶
const ( ActionGet = "get" ActionUpdate = "update" ActionDelete = "delete" ActionCreate = "create" )
const ( // AuthExtensionKey is the key used to store the auth data in the extensions map AuthExtensionKey = "auth" // ServerLatencyExtensionKey is the key used to store the server latency in the extensions map ServerLatencyExtensionKey = "server_latency" // TraceExtensionKey is the key used to store the trace id in the extensions map TraceExtensionKey = "trace_id" // ModulesExtensionKey is the key used to store the missing module in the extensions map ModulesExtensionKey = "missing_module" )
const ( // DefaultMaxMemoryMB is the default max memory for multipart forms (32MB) DefaultMaxMemoryMB = 32 // GraphPath is the api graph path for the graphql server GraphPath = "query" // Default Complexity limit for graphql queries set by default, this can be overridden in the config DefaultComplexityLimit = 100 // IntrospectionComplexity is the complexity limit for introspection queries IntrospectionComplexity = 200 )
Variables ¶
var ( // ErrInternalServerError is returned when an internal error occurs. ErrInternalServerError = errors.New("internal server error") // ErrInvalidInput is returned when the input is invalid ErrInvalidInput = errors.New("invalid input") // ErrCascadeDelete is returned when an error occurs while performing cascade deletes on associated objects ErrCascadeDelete = errors.New("error deleting associated objects") // ErrSearchFailed is returned when the search operation fails ErrSearchFailed = errors.New("search failed, please try again") // ErrSearchQueryTooShort is returned when the search query is too short ErrSearchQueryTooShort = errors.New("search query is too short, please enter a longer search query") // ErrNoOrganizationID is returned when the organization ID is not provided ErrNoOrganizationID = errors.New("unable to determine organization ID in request") // ErrUnableToDetermineObjectType is returned when the object type up the parent upload object cannot be determined ErrUnableToDetermineObjectType = errors.New("unable to determine parent object type") // ErrResourceNotAccessibleWithToken is returned when a resource is not accessible with a personal access token or api token ErrResourceNotAccessibleWithToken = errors.New("resource is not accessible with token authentication") // ErrMissingTrustCenterCnameTarget is returned when the trust center cname target is missing ErrMissingTrustCenterCnameTarget = errors.New("missing trust center cname target") // ErrTrustCenterDomainAlreadyExists is returned when the domain already exists for the trust center ErrTrustCenterDomainAlreadyExists = errors.New("domain already exists for this trust center") )
Functions ¶
func AddAllExtensions ¶
AddAllExtensions adds all the extensions to the server including auth, latency and trace
func CheckAllowedAuthType ¶
CheckAllowedAuthType checks how the user is authenticated and returns an error if the user is authenticated with an API token for a user owned setting
func ConvertToObject ¶
ConvertToObject converts an object to a specific type
func GetBulkUploadOwnerInput ¶
GetBulkUploadOwnerInput retrieves the owner id from the bulk upload input if there are multiple owner ids, it returns an error this is used to ensure that the owner id is consistent across all inputs
func GetConstraintField ¶
GetConstraintField returns the field that caused the constraint error this currently only works for postgres errors, which is the supported database of this project
func GetOrgOwnerFromInput ¶
GetOrgOwnerFromInput retrieves the owner id from the input input can be of any type, but must contain an owner id field if the owner id is not found, it returns nil
func SetOrganizationInAuthContext ¶
SetOrganizationInAuthContext sets the organization in the auth context based on the input if it is not already set in most cases this is a no-op because the organization id is set in the auth middleware only when multiple organizations are authorized (e.g. with a PAT) is this necessary
func SetOrganizationInAuthContextBulkRequest ¶
SetOrganizationInAuthContextBulkRequest sets the organization in the auth context based on the input if it is not already set in most cases this is a no-op because the organization id is set in the auth middleware in the case of personal access tokens, this is necessary to ensure the organization id is set the organization must be the same across all inputs in the bulk request
func UnmarshalBulkData ¶
UnmarshalBulkData unmarshals the input bulk data into a slice of the given type
func WithContextLevelCache ¶
WithContextLevelCache adds a context level cache to the handler
func WithFileUploader ¶
WithFileUploader adds the file uploader to the graphql handler this will handle the file upload process for the multipart form
func WithMetrics ¶
WithMetrics adds Prometheus instrumentation around GraphQL operations.
func WithResultLimit ¶
WithResultLimit adds a max result limit to the handler in order to set limits on all nested edges in the graphql request
func WithSkipCache ¶
WithSkipCache adds a skip cache middleware to the handler This is useful for testing, where you don't want to cache responses so you can see the changes immediately
Types ¶
type Action ¶
type Action struct {
// Object is the object being acted upon
Object string
// Action is the action being performed
Action string
}
Action represents an action being performed on an object
type AlreadyExistsError ¶
type AlreadyExistsError struct {
ObjectType string
}
AlreadyExistsError is returned when an object already exists
func NewAlreadyExistsError ¶
func NewAlreadyExistsError(o string) *AlreadyExistsError
NewAlreadyExistsError returns a AlreadyExistsError
func (*AlreadyExistsError) Code ¶
func (e *AlreadyExistsError) Code() string
Code returns the AlreadyExistsError code
func (*AlreadyExistsError) Error ¶
func (e *AlreadyExistsError) Error() string
Error returns the AlreadyExistsError in string format
func (*AlreadyExistsError) Message ¶
func (e *AlreadyExistsError) Message() string
Message returns the AlreadyExistsError message
func (*AlreadyExistsError) Module ¶
func (e *AlreadyExistsError) Module() models.OrgModule
Module implements the CustomErrorType interface
type Auth ¶
type Auth struct {
// AuthenticationType is the type of authentication used, e.g. JWT, API key, etc.
AuthenticationType auth.AuthenticationType `json:"authentication_type,omitempty"`
// AuthorizedOrganizations is the organization ID(s) of the authenticated user
AuthorizedOrganizations []string `json:"authorized_organization,omitempty"`
// AccessToken is the access token used for authentication, if the user did an action (e.g. created a new organization)
// that updated the access token, this will be the new access token
AccessToken string `json:"access_token,omitempty"`
// RefreshToken is the refresh token used for authentication, if the user did an action (e.g. created a new organization)
// that updated the refresh token, this will be the new refresh token
RefreshToken string `json:"refresh_token,omitempty"`
// SessionID is the session token used for authentication
SessionID string `json:"session_id,omitempty"`
}
Auth contains the authentication data to be added to the extensions map
type ComplexityLimitWithMetrics ¶
type ComplexityLimitWithMetrics struct {
// contains filtered or unexported fields
}
ComplexityLimitWithMetrics is a custom complexity limit extension that records metrics
func NewComplexityLimitWithMetrics ¶
func NewComplexityLimitWithMetrics(limitFunc func(ctx context.Context, rc *graphql.OperationContext) int) *ComplexityLimitWithMetrics
NewComplexityLimitWithMetrics creates a new complexity limit extension with metrics
func (*ComplexityLimitWithMetrics) ExtensionName ¶
func (c *ComplexityLimitWithMetrics) ExtensionName() string
ExtensionName returns the extension name
func (*ComplexityLimitWithMetrics) MutateOperationContext ¶
func (c *ComplexityLimitWithMetrics) MutateOperationContext(ctx context.Context, opCtx *graphql.OperationContext) *gqlerror.Error
MutateOperationContext calculates complexity and enforces the limit
func (*ComplexityLimitWithMetrics) Validate ¶
func (c *ComplexityLimitWithMetrics) Validate(schema graphql.ExecutableSchema) error
Validate validates the schema and stores it for complexity calculations
type ComplexityStats ¶
type ComplexityStats struct {
// The calculated complexity for this request
Complexity int
// The complexity limit for this request returned by the extension func
ComplexityLimit int
}
ComplexityStats represents the complexity calculation results
type ForeignKeyError ¶
ForeignKeyError is returned when an object does not exist in the related table
func NewForeignKeyError ¶
func NewForeignKeyError(action, objecttype string) *ForeignKeyError
NewForeignKeyError returns a ForeignKeyError
func (*ForeignKeyError) Code ¶
func (e *ForeignKeyError) Code() string
Code returns the ForeignKeyError code
func (*ForeignKeyError) Error ¶
func (e *ForeignKeyError) Error() string
Error returns the ForeignKeyError in string format
func (*ForeignKeyError) Message ¶
func (e *ForeignKeyError) Message() string
Message returns the ForeignKeyError message
func (*ForeignKeyError) Module ¶
func (e *ForeignKeyError) Module() models.OrgModule
Module implements the CustomErrorType interface
type NotFoundError ¶
type NotFoundError struct {
ObjectType string
}
NotFoundError is returned when the requested object is not found
func NewNotFoundError ¶
func NewNotFoundError(o string) NotFoundError
newPermissionDeniedError returns a NotFoundError
func (NotFoundError) Code ¶
func (e NotFoundError) Code() string
Code returns the NotFoundError code
func (NotFoundError) Error ¶
func (e NotFoundError) Error() string
Error returns the NotFoundError in string format
func (NotFoundError) Message ¶
func (e NotFoundError) Message() string
Message returns the NotFoundError in string format
func (NotFoundError) Module ¶
func (e NotFoundError) Module() models.OrgModule
Module implements the CustomErrorType interface
type ValidationError ¶
type ValidationError struct {
ErrMsg string
}
ValidationError is returned when a field fails validation
func NewValidationError ¶
func NewValidationError(errMsg string) *ValidationError
NewValidationError returns a ValidationError
func (*ValidationError) Code ¶
func (e *ValidationError) Code() string
Code returns the ValidationError code
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error returns the ValidationError in string format, by removing the "generated: " prefix
func (*ValidationError) Message ¶
func (e *ValidationError) Message() string
Message returns the ValidationError message
func (*ValidationError) Module ¶
func (e *ValidationError) Module() models.OrgModule
Module implements the CustomErrorType interface