69
pkg/server/gqlutils/errors.go
Normal file
69
pkg/server/gqlutils/errors.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package gqlutils
|
||||
|
||||
import (
|
||||
"maps"
|
||||
|
||||
"github.com/vektah/gqlparser/v2/gqlerror"
|
||||
)
|
||||
|
||||
func Unauthorized() *gqlerror.Error {
|
||||
return &gqlerror.Error{
|
||||
Message: "not authorized",
|
||||
Extensions: map[string]any{
|
||||
"code": "UNAUTHORIZED",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func AuthenticationRequired(details map[string]any) *gqlerror.Error {
|
||||
extensions := map[string]any{
|
||||
"code": "AUTHENTICATION_REQUIRED",
|
||||
}
|
||||
maps.Copy(extensions, details)
|
||||
|
||||
return &gqlerror.Error{
|
||||
Message: "Additional authentication required to access this organization",
|
||||
Extensions: extensions,
|
||||
}
|
||||
}
|
||||
|
||||
func NotFound(err error) *gqlerror.Error {
|
||||
return &gqlerror.Error{
|
||||
Message: err.Error(),
|
||||
Extensions: map[string]any{
|
||||
"code": "NOT_FOUND",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Conflict(err error) *gqlerror.Error {
|
||||
return &gqlerror.Error{
|
||||
Message: err.Error(),
|
||||
Extensions: map[string]any{
|
||||
"code": "CONFLICT",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Invalid(err error) *gqlerror.Error {
|
||||
return &gqlerror.Error{
|
||||
Message: err.Error(),
|
||||
Extensions: map[string]any{
|
||||
"code": "INVALID",
|
||||
},
|
||||
}
|
||||
}
|
||||
69
pkg/server/gqlutils/recovery.go
Normal file
69
pkg/server/gqlutils/recovery.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package gqlutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/vektah/gqlparser/v2/gqlerror"
|
||||
"go.gearno.de/kit/httpserver"
|
||||
"go.gearno.de/kit/log"
|
||||
"go.probo.inc/probo/pkg/auth"
|
||||
"go.probo.inc/probo/pkg/authz"
|
||||
)
|
||||
|
||||
func RecoverFunc(ctx context.Context, err any) error {
|
||||
if gqlErr, ok := err.(*gqlerror.Error); ok {
|
||||
return gqlErr
|
||||
}
|
||||
|
||||
var errSAMLRequired auth.ErrSAMLAuthRequired
|
||||
if errors.As(asError(err), &errSAMLRequired) {
|
||||
return AuthenticationRequired(map[string]any{
|
||||
"requiresSaml": true,
|
||||
"redirectUrl": errSAMLRequired.RedirectURL,
|
||||
"samlConfigId": errSAMLRequired.ConfigID.String(),
|
||||
"organizationId": errSAMLRequired.OrganizationID.String(),
|
||||
})
|
||||
}
|
||||
|
||||
var errPasswordRequired auth.ErrPasswordAuthRequired
|
||||
if errors.As(asError(err), &errPasswordRequired) {
|
||||
return AuthenticationRequired(map[string]any{
|
||||
"requiresSaml": false,
|
||||
"redirectUrl": errPasswordRequired.RedirectURL,
|
||||
"organizationId": errPasswordRequired.OrganizationID.String(),
|
||||
})
|
||||
}
|
||||
|
||||
var tenantAccessErr *authz.TenantAccessError
|
||||
if errTyped, ok := err.(error); ok && errors.As(errTyped, &tenantAccessErr) {
|
||||
return Unauthorized()
|
||||
}
|
||||
|
||||
logger := httpserver.LoggerFromContext(ctx)
|
||||
logger.Error("resolver panic", log.Any("error", err), log.Any("stack", string(debug.Stack())))
|
||||
|
||||
return errors.New("internal server error")
|
||||
}
|
||||
|
||||
func asError(err any) error {
|
||||
if e, ok := err.(error); ok {
|
||||
return e
|
||||
}
|
||||
return errors.New("unknown panic")
|
||||
}
|
||||
127
pkg/server/gqlutils/tracing.go
Normal file
127
pkg/server/gqlutils/tracing.go
Normal file
@@ -0,0 +1,127 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package gqlutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
"go.gearno.de/kit/log"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type TracingExtension struct {
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func NewTracingExtension(logger *log.Logger) TracingExtension {
|
||||
return TracingExtension{logger: logger}
|
||||
}
|
||||
|
||||
func (t TracingExtension) ExtensionName() string {
|
||||
return "Tracing"
|
||||
}
|
||||
|
||||
func (t TracingExtension) Validate(schema graphql.ExecutableSchema) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t TracingExtension) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) {
|
||||
rootSpan := trace.SpanFromContext(ctx)
|
||||
|
||||
if rootSpan.IsRecording() {
|
||||
tracer := otel.Tracer("graphql-field")
|
||||
fieldContext := graphql.GetFieldContext(ctx)
|
||||
|
||||
ctx, span := tracer.Start(ctx, "GraphQL Field: "+fieldContext.Field.Name)
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("graphql.field.name", fieldContext.Field.Name),
|
||||
attribute.String("graphql.field.path", fieldContext.Path().String()),
|
||||
attribute.String("graphql.field.object", fieldContext.Object),
|
||||
)
|
||||
|
||||
result, err := next(ctx)
|
||||
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
return next(ctx)
|
||||
}
|
||||
|
||||
func (t TracingExtension) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
|
||||
rootSpan := trace.SpanFromContext(ctx)
|
||||
requestContext := graphql.GetOperationContext(ctx)
|
||||
startTime := time.Now()
|
||||
|
||||
if rootSpan.IsRecording() {
|
||||
tracer := otel.Tracer("graphql-operation")
|
||||
operationName := "GraphQL Operation"
|
||||
if requestContext.OperationName != "" {
|
||||
operationName = "GraphQL " + requestContext.OperationName
|
||||
}
|
||||
|
||||
var span trace.Span
|
||||
ctx, span = tracer.Start(ctx, operationName)
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("graphql.operation_name", requestContext.OperationName),
|
||||
attribute.String("graphql.operation_type", string(requestContext.Operation.Operation)),
|
||||
attribute.String("graphql.query", requestContext.RawQuery),
|
||||
)
|
||||
}
|
||||
|
||||
handler := next(ctx)
|
||||
|
||||
return func(ctx context.Context) *graphql.Response {
|
||||
resp := handler(ctx)
|
||||
duration := time.Since(startTime)
|
||||
|
||||
operationType := string(requestContext.Operation.Operation)
|
||||
operationName := requestContext.OperationName
|
||||
if operationName == "" {
|
||||
operationName = "unnamed"
|
||||
}
|
||||
|
||||
if resp.Errors != nil {
|
||||
t.logger.ErrorCtx(ctx,
|
||||
fmt.Sprintf("%s %s failed %s", operationType, operationName, duration.String()),
|
||||
log.String("graphql_operation_name", operationName),
|
||||
log.String("graphql_operation_type", operationType),
|
||||
log.Duration("graphql_operation_duration", duration),
|
||||
log.Any("graphql_operation_errors", resp.Errors),
|
||||
)
|
||||
} else {
|
||||
t.logger.InfoCtx(ctx,
|
||||
fmt.Sprintf("%s %s succeed %s", operationType, operationName, duration.String()),
|
||||
log.String("graphql_operation_name", operationName),
|
||||
log.String("graphql_operation_type", operationType),
|
||||
log.Duration("graphql_operation_duration", duration),
|
||||
)
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
}
|
||||
60
pkg/server/gqlutils/types/bigint/bigint.go
Normal file
60
pkg/server/gqlutils/types/bigint/bigint.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package bigint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
)
|
||||
|
||||
type BigIntScalar = int64
|
||||
|
||||
func MarshalBigIntScalar(i int64) graphql.Marshaler {
|
||||
return graphql.WriterFunc(func(w io.Writer) {
|
||||
w.Write([]byte(strconv.FormatInt(i, 10)))
|
||||
})
|
||||
}
|
||||
|
||||
func UnmarshalBigIntScalar(v any) (int64, error) {
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
i, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("invalid BigInt value: %v", err)
|
||||
}
|
||||
return i, nil
|
||||
case int:
|
||||
return int64(val), nil
|
||||
case int32:
|
||||
return int64(val), nil
|
||||
case int64:
|
||||
return val, nil
|
||||
case float32:
|
||||
if val != float32(int64(val)) {
|
||||
return 0, fmt.Errorf("BigInt cannot represent non-integer value: %v", val)
|
||||
}
|
||||
return int64(val), nil
|
||||
case float64:
|
||||
if val != float64(int64(val)) {
|
||||
return 0, fmt.Errorf("BigInt cannot represent non-integer value: %v", val)
|
||||
}
|
||||
return int64(val), nil
|
||||
default:
|
||||
return 0, fmt.Errorf("cannot unmarshal %T into BigInt", v)
|
||||
}
|
||||
}
|
||||
72
pkg/server/gqlutils/types/cursor/cursorkey.go
Normal file
72
pkg/server/gqlutils/types/cursor/cursorkey.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package cursor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type CursorKeyScalar = page.CursorKey
|
||||
|
||||
func NewCursor[O page.OrderField](
|
||||
first *int,
|
||||
after *page.CursorKey,
|
||||
last *int,
|
||||
before *page.CursorKey,
|
||||
orderBy page.OrderBy[O],
|
||||
) *page.Cursor[O] {
|
||||
var (
|
||||
size int
|
||||
from *page.CursorKey
|
||||
direction = page.Head
|
||||
)
|
||||
|
||||
if first != nil {
|
||||
size = *first
|
||||
direction = page.Head
|
||||
from = after
|
||||
} else if last != nil {
|
||||
size = *last
|
||||
direction = page.Tail
|
||||
from = before
|
||||
}
|
||||
|
||||
return page.NewCursor(size, from, direction, orderBy)
|
||||
}
|
||||
|
||||
func MarshalCursorKeyScalar(ck page.CursorKey) graphql.Marshaler {
|
||||
return graphql.WriterFunc(func(w io.Writer) {
|
||||
_, _ = w.Write([]byte(strconv.Quote(ck.String())))
|
||||
})
|
||||
}
|
||||
|
||||
func UnmarshalCursorKeyScalar(v interface{}) (page.CursorKey, error) {
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
return page.CursorKeyNil, errors.New("must be a string")
|
||||
}
|
||||
|
||||
ck, err := page.ParseCursorKey(s)
|
||||
if err != nil {
|
||||
return page.CursorKeyNil, err
|
||||
}
|
||||
|
||||
return ck, nil
|
||||
}
|
||||
46
pkg/server/gqlutils/types/gid/gid.go
Normal file
46
pkg/server/gqlutils/types/gid/gid.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package gid
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
type GIDScalar = gid.GID
|
||||
|
||||
func MarshalGIDScalar(id gid.GID) graphql.Marshaler {
|
||||
return graphql.WriterFunc(func(w io.Writer) {
|
||||
w.Write([]byte(strconv.Quote(id.String())))
|
||||
})
|
||||
}
|
||||
|
||||
func UnmarshalGIDScalar(v interface{}) (gid.GID, error) {
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
return gid.Nil, errors.New("must be a string")
|
||||
}
|
||||
|
||||
id, err := gid.ParseGID(s)
|
||||
if err != nil {
|
||||
return gid.Nil, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
46
pkg/server/gqlutils/types/pageinfo/pageinfo.go
Normal file
46
pkg/server/gqlutils/types/pageinfo/pageinfo.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package pageinfo
|
||||
|
||||
import (
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"go.gearno.de/x/ref"
|
||||
)
|
||||
|
||||
type PageInfo struct {
|
||||
HasNextPage bool
|
||||
HasPreviousPage bool
|
||||
StartCursor *page.CursorKey
|
||||
EndCursor *page.CursorKey
|
||||
}
|
||||
|
||||
func NewPageInfo[T page.Paginable[O], O page.OrderField](p *page.Page[T, O]) *PageInfo {
|
||||
var (
|
||||
startCursor *page.CursorKey
|
||||
endCursor *page.CursorKey
|
||||
)
|
||||
|
||||
if len(p.Data) > 0 {
|
||||
startCursor = ref.Ref(p.First().CursorKey(p.Cursor.OrderBy.Field))
|
||||
endCursor = ref.Ref(p.Last().CursorKey(p.Cursor.OrderBy.Field))
|
||||
}
|
||||
|
||||
return &PageInfo{
|
||||
HasNextPage: p.Info.HasNext,
|
||||
HasPreviousPage: p.Info.HasPrev,
|
||||
StartCursor: startCursor,
|
||||
EndCursor: endCursor,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user