From c833cb672bc958a1ac271ca5f077f08367c8dafa Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 29 Aug 2024 17:55:37 +0530 Subject: [PATCH] Add a method to return paginated list of records --- gql/cerc-io/laconicd/schema.graphql | 6 +- gql/generated.go | 87 +++++++++++++++-------------- x/registry/keeper/keeper.go | 17 ++++++ x/registry/keeper/query_server.go | 6 +- 4 files changed, 71 insertions(+), 45 deletions(-) diff --git a/gql/cerc-io/laconicd/schema.graphql b/gql/cerc-io/laconicd/schema.graphql index 8d0498a4..86431843 100644 --- a/gql/cerc-io/laconicd/schema.graphql +++ b/gql/cerc-io/laconicd/schema.graphql @@ -246,7 +246,11 @@ type Query { # Whether to query all records, not just named ones (false by default). all: Boolean + + # Pagination limit limit: Int + + # Pagination offset offset: Int ): [Record] @@ -255,7 +259,7 @@ type Query { # # Get authorities list. - getAuthorities(owner: String): [Authority] + getAuthorities(owner: String): [Authority]! # Lookup authority information. lookupAuthorities(names: [String!]): [AuthorityRecord]! diff --git a/gql/generated.go b/gql/generated.go index 8b3da296..564e94a0 100644 --- a/gql/generated.go +++ b/gql/generated.go @@ -4861,11 +4861,14 @@ func (ec *executionContext) _Query_getAuthorities(ctx context.Context, field gra return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } res := resTmp.([]*Authority) fc.Result = res - return ec.marshalOAuthority2ᚕᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx, field.Selections, res) + return ec.marshalNAuthority2ᚕᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_Query_getAuthorities(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -9599,6 +9602,9 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr } }() res = ec._Query_getAuthorities(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } return res } @@ -10414,6 +10420,44 @@ func (ec *executionContext) marshalNAttribute2ᚖgitᚗvdbᚗtoᚋcercᚑioᚋla return ec._Attribute(ctx, sel, v) } +func (ec *executionContext) marshalNAuthority2ᚕᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx context.Context, sel ast.SelectionSet, v []*Authority) graphql.Marshaler { + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalOAuthority2ᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + return ret +} + func (ec *executionContext) marshalNAuthorityRecord2ᚕᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthorityRecord(ctx context.Context, sel ast.SelectionSet, v []*AuthorityRecord) graphql.Marshaler { ret := make(graphql.Array, len(v)) var wg sync.WaitGroup @@ -11249,47 +11293,6 @@ func (ec *executionContext) marshalOAuctionBid2ᚖgitᚗvdbᚗtoᚋcercᚑioᚋl return ec._AuctionBid(ctx, sel, v) } -func (ec *executionContext) marshalOAuthority2ᚕᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx context.Context, sel ast.SelectionSet, v []*Authority) graphql.Marshaler { - if v == nil { - return graphql.Null - } - ret := make(graphql.Array, len(v)) - var wg sync.WaitGroup - isLen1 := len(v) == 1 - if !isLen1 { - wg.Add(len(v)) - } - for i := range v { - i := i - fc := &graphql.FieldContext{ - Index: &i, - Result: &v[i], - } - ctx := graphql.WithFieldContext(ctx, fc) - f := func(i int) { - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = nil - } - }() - if !isLen1 { - defer wg.Done() - } - ret[i] = ec.marshalOAuthority2ᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx, sel, v[i]) - } - if isLen1 { - f(i) - } else { - go f(i) - } - - } - wg.Wait() - - return ret -} - func (ec *executionContext) marshalOAuthority2ᚖgitᚗvdbᚗtoᚋcercᚑioᚋlaconicdᚋgqlᚐAuthority(ctx context.Context, sel ast.SelectionSet, v *Authority) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index ea36de14..a59d70ca 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/legacy" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/types/query" auth "github.com/cosmos/cosmos-sdk/x/auth/keeper" bank "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/gibson042/canonicaljson-go" @@ -204,6 +205,22 @@ func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) { return records, nil } +// PaginatedListRecords - get all records with pagination. +func (k Keeper) PaginatedListRecords(ctx sdk.Context, pagination *query.PageRequest) ([]registrytypes.Record, *query.PageResponse, error) { + records, pageResp, err := query.CollectionPaginate(ctx, k.Records, pagination, func(key string, value registrytypes.Record) (registrytypes.Record, error) { + if err := k.populateRecordNames(ctx, &value); err != nil { + return registrytypes.Record{}, err + } + + return value, nil + }) + if err != nil { + return nil, nil, err + } + + return records, pageResp, nil +} + // GetRecordById - gets a record from the store. func (k Keeper) GetRecordById(ctx sdk.Context, id string) (registrytypes.Record, error) { record, err := k.Records.Get(ctx, id) diff --git a/x/registry/keeper/query_server.go b/x/registry/keeper/query_server.go index 17d2883d..5bee250f 100644 --- a/x/registry/keeper/query_server.go +++ b/x/registry/keeper/query_server.go @@ -6,6 +6,7 @@ import ( errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/types/query" registrytypes "git.vdb.to/cerc-io/laconicd/x/registry" ) @@ -39,6 +40,7 @@ func (qs queryServer) Records(c context.Context, req *registrytypes.QueryRecords all := req.GetAll() var records []registrytypes.Record + var pageResp *query.PageResponse var err error if len(attributes) > 0 { records, err = qs.k.RecordsFromAttributes(ctx, attributes, all) @@ -46,13 +48,13 @@ func (qs queryServer) Records(c context.Context, req *registrytypes.QueryRecords return nil, err } } else { - records, err = qs.k.ListRecords(ctx) + records, pageResp, err = qs.k.PaginatedListRecords(ctx, req.Pagination) if err != nil { return nil, err } } - return ®istrytypes.QueryRecordsResponse{Records: records}, nil + return ®istrytypes.QueryRecordsResponse{Records: records, Pagination: pageResp}, nil } func (qs queryServer) GetRecord(c context.Context, req *registrytypes.QueryGetRecordRequest) (*registrytypes.QueryGetRecordResponse, error) {