From ad2f43f0272e4df37c6b9bc3aee66f0b4037c186 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Thu, 29 Aug 2024 15:47:36 +0530 Subject: [PATCH] Configure pagination in getRecords GQL query --- gql/cerc-io/laconicd/schema.graphql | 2 ++ gql/generated.go | 26 ++++++++++++++++++++++---- gql/resolver.go | 12 +++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/gql/cerc-io/laconicd/schema.graphql b/gql/cerc-io/laconicd/schema.graphql index c3fac8f6..8d0498a4 100644 --- a/gql/cerc-io/laconicd/schema.graphql +++ b/gql/cerc-io/laconicd/schema.graphql @@ -246,6 +246,8 @@ type Query { # Whether to query all records, not just named ones (false by default). all: Boolean + limit: Int + offset: Int ): [Record] # diff --git a/gql/generated.go b/gql/generated.go index a243f6df..8b3da296 100644 --- a/gql/generated.go +++ b/gql/generated.go @@ -185,7 +185,7 @@ type ComplexityRoot struct { LookupNames func(childComplexity int, names []string) int QueryBonds func(childComplexity int, attributes []*KeyValueInput) int QueryBondsByOwner func(childComplexity int, ownerAddresses []string) int - QueryRecords func(childComplexity int, attributes []*KeyValueInput, all *bool) int + QueryRecords func(childComplexity int, attributes []*KeyValueInput, all *bool, limit *int, offset *int) int ResolveNames func(childComplexity int, names []string) int } @@ -236,7 +236,7 @@ type QueryResolver interface { QueryBonds(ctx context.Context, attributes []*KeyValueInput) ([]*Bond, error) QueryBondsByOwner(ctx context.Context, ownerAddresses []string) ([]*OwnerBonds, error) GetRecordsByIds(ctx context.Context, ids []string) ([]*Record, error) - QueryRecords(ctx context.Context, attributes []*KeyValueInput, all *bool) ([]*Record, error) + QueryRecords(ctx context.Context, attributes []*KeyValueInput, all *bool, limit *int, offset *int) ([]*Record, error) GetAuthorities(ctx context.Context, owner *string) ([]*Authority, error) LookupAuthorities(ctx context.Context, names []string) ([]*AuthorityRecord, error) LookupNames(ctx context.Context, names []string) ([]*NameRecord, error) @@ -873,7 +873,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return 0, false } - return e.complexity.Query.QueryRecords(childComplexity, args["attributes"].([]*KeyValueInput), args["all"].(*bool)), true + return e.complexity.Query.QueryRecords(childComplexity, args["attributes"].([]*KeyValueInput), args["all"].(*bool), args["limit"].(*int), args["offset"].(*int)), true case "Query.resolveNames": if e.complexity.Query.ResolveNames == nil { @@ -1330,6 +1330,24 @@ func (ec *executionContext) field_Query_queryRecords_args(ctx context.Context, r } } args["all"] = arg1 + var arg2 *int + if tmp, ok := rawArgs["limit"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("limit")) + arg2, err = ec.unmarshalOInt2ᚖint(ctx, tmp) + if err != nil { + return nil, err + } + } + args["limit"] = arg2 + var arg3 *int + if tmp, ok := rawArgs["offset"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("offset")) + arg3, err = ec.unmarshalOInt2ᚖint(ctx, tmp) + if err != nil { + return nil, err + } + } + args["offset"] = arg3 return args, nil } @@ -4766,7 +4784,7 @@ func (ec *executionContext) _Query_queryRecords(ctx context.Context, field graph }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().QueryRecords(rctx, fc.Args["attributes"].([]*KeyValueInput), fc.Args["all"].(*bool)) + return ec.resolvers.Query().QueryRecords(rctx, fc.Args["attributes"].([]*KeyValueInput), fc.Args["all"].(*bool), fc.Args["limit"].(*int), fc.Args["offset"].(*int)) }) if err != nil { ec.Error(ctx, err) diff --git a/gql/resolver.go b/gql/resolver.go index 76f3571f..97e0407d 100644 --- a/gql/resolver.go +++ b/gql/resolver.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" types "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -136,14 +137,23 @@ func (q queryResolver) LookupNames(ctx context.Context, names []string) ([]*Name return gqlResponse, nil } -func (q queryResolver) QueryRecords(ctx context.Context, attributes []*KeyValueInput, all *bool) ([]*Record, error) { +func (q queryResolver) QueryRecords(ctx context.Context, attributes []*KeyValueInput, all *bool, limit *int, offset *int) ([]*Record, error) { nsQueryClient := registrytypes.NewQueryClient(q.ctx) + pagination := &query.PageRequest{} + if limit != nil { + pagination.Limit = uint64(*limit) + } + if offset != nil { + pagination.Offset = uint64(*offset) + } + res, err := nsQueryClient.Records( context.Background(), ®istrytypes.QueryRecordsRequest{ Attributes: toRPCAttributes(attributes), All: (all != nil && *all), + Pagination: pagination, }, ) if err != nil {