From d47dd3171585594eb5b0b0d91c3c1917005cb674 Mon Sep 17 00:00:00 2001 From: Sacha Al Himdani Date: Fri, 26 Sep 2025 17:43:52 +0200 Subject: [PATCH] Use custom type for big int in graphql Signed-off-by: Sacha Al Himdani --- ...dorComplianceTabFragment_report.graphql.ts | 4 +- pkg/server/api/console/v1/gqlgen.yaml | 3 + pkg/server/api/console/v1/schema.graphql | 7 ++- pkg/server/api/console/v1/schema/schema.go | 42 +++++++++---- pkg/server/graphql/types/bigint/bigint.go | 60 +++++++++++++++++++ 5 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 pkg/server/graphql/types/bigint/bigint.go diff --git a/apps/console/src/pages/organizations/vendors/tabs/__generated__/VendorComplianceTabFragment_report.graphql.ts b/apps/console/src/pages/organizations/vendors/tabs/__generated__/VendorComplianceTabFragment_report.graphql.ts index ad4487cde..4a4fa9831 100644 --- a/apps/console/src/pages/organizations/vendors/tabs/__generated__/VendorComplianceTabFragment_report.graphql.ts +++ b/apps/console/src/pages/organizations/vendors/tabs/__generated__/VendorComplianceTabFragment_report.graphql.ts @@ -1,5 +1,5 @@ /** - * @generated SignedSource<<8f56e9dbd8ce8cf0c81d8f2e9f1ad06e>> + * @generated SignedSource<<4c861a832ad2f79bd9d6ddcc364f6898>> * @lightSyntaxTransform * @nogrep */ @@ -11,7 +11,7 @@ import { ReaderFragment } from 'relay-runtime'; import { FragmentRefs } from "relay-runtime"; export type VendorComplianceTabFragment_report$data = { - readonly fileSize: number; + readonly fileSize: any; readonly fileUrl: string; readonly id: string; readonly reportDate: any; diff --git a/pkg/server/api/console/v1/gqlgen.yaml b/pkg/server/api/console/v1/gqlgen.yaml index b1a295bbf..3b5b62ea2 100644 --- a/pkg/server/api/console/v1/gqlgen.yaml +++ b/pkg/server/api/console/v1/gqlgen.yaml @@ -30,3 +30,6 @@ models: Duration: model: - "github.com/99designs/gqlgen/graphql.Duration" + BigInt: + model: + - "github.com/getprobo/probo/pkg/server/graphql/types/bigint.BigIntScalar" diff --git a/pkg/server/api/console/v1/schema.graphql b/pkg/server/api/console/v1/schema.graphql index 4fd43f236..30b9bebba 100644 --- a/pkg/server/api/console/v1/schema.graphql +++ b/pkg/server/api/console/v1/schema.graphql @@ -18,6 +18,7 @@ scalar Void scalar Datetime scalar Upload scalar Duration +scalar BigInt # Interfaces interface Node { @@ -1719,7 +1720,7 @@ type VendorComplianceReport implements Node { validUntil: Datetime reportName: String! fileUrl: String! @goField(forceResolver: true) - fileSize: Int! + fileSize: BigInt! createdAt: Datetime! updatedAt: Datetime! } @@ -1731,7 +1732,7 @@ type VendorBusinessAssociateAgreement implements Node { validUntil: Datetime fileName: String! fileUrl: String! @goField(forceResolver: true) - fileSize: Int! + fileSize: BigInt! createdAt: Datetime! updatedAt: Datetime! } @@ -1763,7 +1764,7 @@ type VendorDataPrivacyAgreement implements Node { validUntil: Datetime fileName: String! fileUrl: String! @goField(forceResolver: true) - fileSize: Int! + fileSize: BigInt! createdAt: Datetime! updatedAt: Datetime! } diff --git a/pkg/server/api/console/v1/schema/schema.go b/pkg/server/api/console/v1/schema/schema.go index 74d9133e5..eb51aede6 100644 --- a/pkg/server/api/console/v1/schema/schema.go +++ b/pkg/server/api/console/v1/schema/schema.go @@ -18,6 +18,7 @@ import ( "github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/server/api/console/v1/types" + "github.com/getprobo/probo/pkg/server/graphql/types/bigint" "github.com/getprobo/probo/pkg/server/graphql/types/cursor" gid1 "github.com/getprobo/probo/pkg/server/graphql/types/gid" gqlparser "github.com/vektah/gqlparser/v2" @@ -8427,6 +8428,7 @@ scalar Void scalar Datetime scalar Upload scalar Duration +scalar BigInt # Interfaces interface Node { @@ -10128,7 +10130,7 @@ type VendorComplianceReport implements Node { validUntil: Datetime reportName: String! fileUrl: String! @goField(forceResolver: true) - fileSize: Int! + fileSize: BigInt! createdAt: Datetime! updatedAt: Datetime! } @@ -10140,7 +10142,7 @@ type VendorBusinessAssociateAgreement implements Node { validUntil: Datetime fileName: String! fileUrl: String! @goField(forceResolver: true) - fileSize: Int! + fileSize: BigInt! createdAt: Datetime! updatedAt: Datetime! } @@ -10172,7 +10174,7 @@ type VendorDataPrivacyAgreement implements Node { validUntil: Datetime fileName: String! fileUrl: String! @goField(forceResolver: true) - fileSize: Int! + fileSize: BigInt! createdAt: Datetime! updatedAt: Datetime! } @@ -57837,9 +57839,9 @@ func (ec *executionContext) _VendorBusinessAssociateAgreement_fileSize(ctx conte } return graphql.Null } - res := resTmp.(int) + res := resTmp.(int64) fc.Result = res - return ec.marshalNInt2int(ctx, field.Selections, res) + return ec.marshalNBigInt2int64(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_VendorBusinessAssociateAgreement_fileSize(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -57849,7 +57851,7 @@ func (ec *executionContext) fieldContext_VendorBusinessAssociateAgreement_fileSi IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + return nil, errors.New("field of type BigInt does not have child fields") }, } return fc, nil @@ -58294,9 +58296,9 @@ func (ec *executionContext) _VendorComplianceReport_fileSize(ctx context.Context } return graphql.Null } - res := resTmp.(int) + res := resTmp.(int64) fc.Result = res - return ec.marshalNInt2int(ctx, field.Selections, res) + return ec.marshalNBigInt2int64(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_VendorComplianceReport_fileSize(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -58306,7 +58308,7 @@ func (ec *executionContext) fieldContext_VendorComplianceReport_fileSize(_ conte IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + return nil, errors.New("field of type BigInt does not have child fields") }, } return fc, nil @@ -59722,9 +59724,9 @@ func (ec *executionContext) _VendorDataPrivacyAgreement_fileSize(ctx context.Con } return graphql.Null } - res := resTmp.(int) + res := resTmp.(int64) fc.Result = res - return ec.marshalNInt2int(ctx, field.Selections, res) + return ec.marshalNBigInt2int64(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_VendorDataPrivacyAgreement_fileSize(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -59734,7 +59736,7 @@ func (ec *executionContext) fieldContext_VendorDataPrivacyAgreement_fileSize(_ c IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + return nil, errors.New("field of type BigInt does not have child fields") }, } return fc, nil @@ -85858,6 +85860,22 @@ var ( } ) +func (ec *executionContext) unmarshalNBigInt2int64(ctx context.Context, v any) (int64, error) { + res, err := bigint.UnmarshalBigIntScalar(v) + return res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalNBigInt2int64(ctx context.Context, sel ast.SelectionSet, v int64) graphql.Marshaler { + _ = sel + res := bigint.MarshalBigIntScalar(v) + if res == graphql.Null { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + } + return res +} + func (ec *executionContext) unmarshalNBoolean2bool(ctx context.Context, v any) (bool, error) { res, err := graphql.UnmarshalBoolean(v) return res, graphql.ErrorOnPath(ctx, err) diff --git a/pkg/server/graphql/types/bigint/bigint.go b/pkg/server/graphql/types/bigint/bigint.go new file mode 100644 index 000000000..aa314f919 --- /dev/null +++ b/pkg/server/graphql/types/bigint/bigint.go @@ -0,0 +1,60 @@ +// Copyright (c) 2025 Probo Inc . +// +// 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) + } +}