Add peoples totalCount support
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -293,6 +293,38 @@ DELETE FROM peoples WHERE %s AND id = @people_id
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Peoples) CountByOrganizationID(
|
||||||
|
ctx context.Context,
|
||||||
|
conn pg.Conn,
|
||||||
|
scope Scoper,
|
||||||
|
organizationID gid.GID,
|
||||||
|
) (int, error) {
|
||||||
|
q := `
|
||||||
|
SELECT
|
||||||
|
COUNT(id)
|
||||||
|
FROM
|
||||||
|
peoples
|
||||||
|
WHERE
|
||||||
|
%s
|
||||||
|
AND organization_id = @organization_id
|
||||||
|
`
|
||||||
|
|
||||||
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||||
|
|
||||||
|
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||||
|
maps.Copy(args, scope.SQLArguments())
|
||||||
|
|
||||||
|
row := conn.QueryRow(ctx, q, args)
|
||||||
|
|
||||||
|
var count int
|
||||||
|
err := row.Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("cannot count people: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Peoples) LoadByOrganizationID(
|
func (p *Peoples) LoadByOrganizationID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
|||||||
@@ -95,6 +95,32 @@ func (s PeopleService) GetByUserID(
|
|||||||
return people, nil
|
return people, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s PeopleService) CountForOrganizationID(
|
||||||
|
ctx context.Context,
|
||||||
|
organizationID gid.GID,
|
||||||
|
) (int, error) {
|
||||||
|
var count int
|
||||||
|
|
||||||
|
err := s.svc.pg.WithConn(
|
||||||
|
ctx,
|
||||||
|
func(conn pg.Conn) (err error) {
|
||||||
|
peoples := coredata.Peoples{}
|
||||||
|
count, err = peoples.CountByOrganizationID(ctx, conn, s.svc.scope, organizationID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot count peoples: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s PeopleService) ListForOrganizationID(
|
func (s PeopleService) ListForOrganizationID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
organizationID gid.GID,
|
organizationID gid.GID,
|
||||||
|
|||||||
@@ -1070,7 +1070,11 @@ type UserEdge {
|
|||||||
node: User!
|
node: User!
|
||||||
}
|
}
|
||||||
|
|
||||||
type PeopleConnection {
|
type PeopleConnection
|
||||||
|
@goModel(
|
||||||
|
model: "github.com/getprobo/probo/pkg/server/api/console/v1/types.PeopleConnection"
|
||||||
|
) {
|
||||||
|
totalCount: Int! @goField(forceResolver: true)
|
||||||
edges: [PeopleEdge!]!
|
edges: [PeopleEdge!]!
|
||||||
pageInfo: PageInfo!
|
pageInfo: PageInfo!
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ type ResolverRoot interface {
|
|||||||
MeasureConnection() MeasureConnectionResolver
|
MeasureConnection() MeasureConnectionResolver
|
||||||
Mutation() MutationResolver
|
Mutation() MutationResolver
|
||||||
Organization() OrganizationResolver
|
Organization() OrganizationResolver
|
||||||
|
PeopleConnection() PeopleConnectionResolver
|
||||||
Query() QueryResolver
|
Query() QueryResolver
|
||||||
Risk() RiskResolver
|
Risk() RiskResolver
|
||||||
RiskConnection() RiskConnectionResolver
|
RiskConnection() RiskConnectionResolver
|
||||||
@@ -605,8 +606,9 @@ type ComplexityRoot struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
PeopleConnection struct {
|
PeopleConnection struct {
|
||||||
Edges func(childComplexity int) int
|
Edges func(childComplexity int) int
|
||||||
PageInfo func(childComplexity int) int
|
PageInfo func(childComplexity int) int
|
||||||
|
TotalCount func(childComplexity int) int
|
||||||
}
|
}
|
||||||
|
|
||||||
PeopleEdge struct {
|
PeopleEdge struct {
|
||||||
@@ -1026,6 +1028,9 @@ type OrganizationResolver interface {
|
|||||||
Assets(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.AssetOrder) (*types.AssetConnection, error)
|
Assets(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.AssetOrder) (*types.AssetConnection, error)
|
||||||
Data(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.DatumOrder) (*types.DatumConnection, error)
|
Data(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.DatumOrder) (*types.DatumConnection, error)
|
||||||
}
|
}
|
||||||
|
type PeopleConnectionResolver interface {
|
||||||
|
TotalCount(ctx context.Context, obj *types.PeopleConnection) (int, error)
|
||||||
|
}
|
||||||
type QueryResolver interface {
|
type QueryResolver interface {
|
||||||
Node(ctx context.Context, id gid.GID) (types.Node, error)
|
Node(ctx context.Context, id gid.GID) (types.Node, error)
|
||||||
Viewer(ctx context.Context) (*types.Viewer, error)
|
Viewer(ctx context.Context) (*types.Viewer, error)
|
||||||
@@ -3588,6 +3593,13 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
|
|||||||
|
|
||||||
return e.complexity.PeopleConnection.PageInfo(childComplexity), true
|
return e.complexity.PeopleConnection.PageInfo(childComplexity), true
|
||||||
|
|
||||||
|
case "PeopleConnection.totalCount":
|
||||||
|
if e.complexity.PeopleConnection.TotalCount == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.complexity.PeopleConnection.TotalCount(childComplexity), true
|
||||||
|
|
||||||
case "PeopleEdge.cursor":
|
case "PeopleEdge.cursor":
|
||||||
if e.complexity.PeopleEdge.Cursor == nil {
|
if e.complexity.PeopleEdge.Cursor == nil {
|
||||||
break
|
break
|
||||||
@@ -5863,7 +5875,11 @@ type UserEdge {
|
|||||||
node: User!
|
node: User!
|
||||||
}
|
}
|
||||||
|
|
||||||
type PeopleConnection {
|
type PeopleConnection
|
||||||
|
@goModel(
|
||||||
|
model: "github.com/getprobo/probo/pkg/server/api/console/v1/types.PeopleConnection"
|
||||||
|
) {
|
||||||
|
totalCount: Int! @goField(forceResolver: true)
|
||||||
edges: [PeopleEdge!]!
|
edges: [PeopleEdge!]!
|
||||||
pageInfo: PageInfo!
|
pageInfo: PageInfo!
|
||||||
}
|
}
|
||||||
@@ -25844,6 +25860,8 @@ func (ec *executionContext) fieldContext_Organization_peoples(ctx context.Contex
|
|||||||
IsResolver: true,
|
IsResolver: true,
|
||||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||||
switch field.Name {
|
switch field.Name {
|
||||||
|
case "totalCount":
|
||||||
|
return ec.fieldContext_PeopleConnection_totalCount(ctx, field)
|
||||||
case "edges":
|
case "edges":
|
||||||
return ec.fieldContext_PeopleConnection_edges(ctx, field)
|
return ec.fieldContext_PeopleConnection_edges(ctx, field)
|
||||||
case "pageInfo":
|
case "pageInfo":
|
||||||
@@ -27157,6 +27175,50 @@ func (ec *executionContext) fieldContext_People_updatedAt(_ context.Context, fie
|
|||||||
return fc, nil
|
return fc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ec *executionContext) _PeopleConnection_totalCount(ctx context.Context, field graphql.CollectedField, obj *types.PeopleConnection) (ret graphql.Marshaler) {
|
||||||
|
fc, err := ec.fieldContext_PeopleConnection_totalCount(ctx, field)
|
||||||
|
if err != nil {
|
||||||
|
return graphql.Null
|
||||||
|
}
|
||||||
|
ctx = graphql.WithFieldContext(ctx, fc)
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
ec.Error(ctx, ec.Recover(ctx, r))
|
||||||
|
ret = graphql.Null
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (any, error) {
|
||||||
|
ctx = rctx // use context from middleware stack in children
|
||||||
|
return ec.resolvers.PeopleConnection().TotalCount(rctx, obj)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
ec.Error(ctx, err)
|
||||||
|
return graphql.Null
|
||||||
|
}
|
||||||
|
if resTmp == nil {
|
||||||
|
if !graphql.HasFieldError(ctx, fc) {
|
||||||
|
ec.Errorf(ctx, "must not be null")
|
||||||
|
}
|
||||||
|
return graphql.Null
|
||||||
|
}
|
||||||
|
res := resTmp.(int)
|
||||||
|
fc.Result = res
|
||||||
|
return ec.marshalNInt2int(ctx, field.Selections, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ec *executionContext) fieldContext_PeopleConnection_totalCount(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||||
|
fc = &graphql.FieldContext{
|
||||||
|
Object: "PeopleConnection",
|
||||||
|
Field: field,
|
||||||
|
IsMethod: true,
|
||||||
|
IsResolver: true,
|
||||||
|
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 fc, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ec *executionContext) _PeopleConnection_edges(ctx context.Context, field graphql.CollectedField, obj *types.PeopleConnection) (ret graphql.Marshaler) {
|
func (ec *executionContext) _PeopleConnection_edges(ctx context.Context, field graphql.CollectedField, obj *types.PeopleConnection) (ret graphql.Marshaler) {
|
||||||
fc, err := ec.fieldContext_PeopleConnection_edges(ctx, field)
|
fc, err := ec.fieldContext_PeopleConnection_edges(ctx, field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -27233,9 +27295,9 @@ func (ec *executionContext) _PeopleConnection_pageInfo(ctx context.Context, fiel
|
|||||||
}
|
}
|
||||||
return graphql.Null
|
return graphql.Null
|
||||||
}
|
}
|
||||||
res := resTmp.(*types.PageInfo)
|
res := resTmp.(types.PageInfo)
|
||||||
fc.Result = res
|
fc.Result = res
|
||||||
return ec.marshalNPageInfo2ᚖgithubᚗcomᚋgetproboᚋproboᚋpkgᚋserverᚋapiᚋconsoleᚋv1ᚋtypesᚐPageInfo(ctx, field.Selections, res)
|
return ec.marshalNPageInfo2githubᚗcomᚋgetproboᚋproboᚋpkgᚋserverᚋapiᚋconsoleᚋv1ᚋtypesᚐPageInfo(ctx, field.Selections, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ec *executionContext) fieldContext_PeopleConnection_pageInfo(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
func (ec *executionContext) fieldContext_PeopleConnection_pageInfo(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||||
@@ -46166,15 +46228,51 @@ func (ec *executionContext) _PeopleConnection(ctx context.Context, sel ast.Selec
|
|||||||
switch field.Name {
|
switch field.Name {
|
||||||
case "__typename":
|
case "__typename":
|
||||||
out.Values[i] = graphql.MarshalString("PeopleConnection")
|
out.Values[i] = graphql.MarshalString("PeopleConnection")
|
||||||
|
case "totalCount":
|
||||||
|
field := field
|
||||||
|
|
||||||
|
innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
ec.Error(ctx, ec.Recover(ctx, r))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
res = ec._PeopleConnection_totalCount(ctx, field, obj)
|
||||||
|
if res == graphql.Null {
|
||||||
|
atomic.AddUint32(&fs.Invalids, 1)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
if field.Deferrable != nil {
|
||||||
|
dfs, ok := deferred[field.Deferrable.Label]
|
||||||
|
di := 0
|
||||||
|
if ok {
|
||||||
|
dfs.AddField(field)
|
||||||
|
di = len(dfs.Values) - 1
|
||||||
|
} else {
|
||||||
|
dfs = graphql.NewFieldSet([]graphql.CollectedField{field})
|
||||||
|
deferred[field.Deferrable.Label] = dfs
|
||||||
|
}
|
||||||
|
dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler {
|
||||||
|
return innerFunc(ctx, dfs)
|
||||||
|
})
|
||||||
|
|
||||||
|
// don't run the out.Concurrently() call below
|
||||||
|
out.Values[i] = graphql.Null
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
|
||||||
case "edges":
|
case "edges":
|
||||||
out.Values[i] = ec._PeopleConnection_edges(ctx, field, obj)
|
out.Values[i] = ec._PeopleConnection_edges(ctx, field, obj)
|
||||||
if out.Values[i] == graphql.Null {
|
if out.Values[i] == graphql.Null {
|
||||||
out.Invalids++
|
atomic.AddUint32(&out.Invalids, 1)
|
||||||
}
|
}
|
||||||
case "pageInfo":
|
case "pageInfo":
|
||||||
out.Values[i] = ec._PeopleConnection_pageInfo(ctx, field, obj)
|
out.Values[i] = ec._PeopleConnection_pageInfo(ctx, field, obj)
|
||||||
if out.Values[i] == graphql.Null {
|
if out.Values[i] == graphql.Null {
|
||||||
out.Invalids++
|
atomic.AddUint32(&out.Invalids, 1)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic("unknown field " + strconv.Quote(field.Name))
|
panic("unknown field " + strconv.Quote(field.Name))
|
||||||
|
|||||||
@@ -16,14 +16,28 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/getprobo/probo/pkg/coredata"
|
"github.com/getprobo/probo/pkg/coredata"
|
||||||
|
"github.com/getprobo/probo/pkg/gid"
|
||||||
"github.com/getprobo/probo/pkg/page"
|
"github.com/getprobo/probo/pkg/page"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
PeopleOrderBy OrderBy[coredata.PeopleOrderField]
|
PeopleOrderBy OrderBy[coredata.PeopleOrderField]
|
||||||
|
|
||||||
|
PeopleConnection struct {
|
||||||
|
TotalCount int
|
||||||
|
Edges []*PeopleEdge
|
||||||
|
PageInfo PageInfo
|
||||||
|
|
||||||
|
Resolver any
|
||||||
|
ParentID gid.GID
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPeopleConnection(p *page.Page[*coredata.People, coredata.PeopleOrderField]) *PeopleConnection {
|
func NewPeopleConnection(
|
||||||
|
p *page.Page[*coredata.People, coredata.PeopleOrderField],
|
||||||
|
parentType any,
|
||||||
|
parentID gid.GID,
|
||||||
|
) *PeopleConnection {
|
||||||
var edges = make([]*PeopleEdge, len(p.Data))
|
var edges = make([]*PeopleEdge, len(p.Data))
|
||||||
|
|
||||||
for i := range edges {
|
for i := range edges {
|
||||||
@@ -32,7 +46,10 @@ func NewPeopleConnection(p *page.Page[*coredata.People, coredata.PeopleOrderFiel
|
|||||||
|
|
||||||
return &PeopleConnection{
|
return &PeopleConnection{
|
||||||
Edges: edges,
|
Edges: edges,
|
||||||
PageInfo: NewPageInfo(p),
|
PageInfo: *NewPageInfo(p),
|
||||||
|
|
||||||
|
Resolver: parentType,
|
||||||
|
ParentID: parentID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -792,11 +792,6 @@ type People struct {
|
|||||||
func (People) IsNode() {}
|
func (People) IsNode() {}
|
||||||
func (this People) GetID() gid.GID { return this.ID }
|
func (this People) GetID() gid.GID { return this.ID }
|
||||||
|
|
||||||
type PeopleConnection struct {
|
|
||||||
Edges []*PeopleEdge `json:"edges"`
|
|
||||||
PageInfo *PageInfo `json:"pageInfo"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PeopleEdge struct {
|
type PeopleEdge struct {
|
||||||
Cursor page.CursorKey `json:"cursor"`
|
Cursor page.CursorKey `json:"cursor"`
|
||||||
Node *People `json:"node"`
|
Node *People `json:"node"`
|
||||||
|
|||||||
@@ -2200,7 +2200,7 @@ func (r *organizationResolver) Peoples(ctx context.Context, obj *types.Organizat
|
|||||||
panic(fmt.Errorf("cannot list organization peoples: %w", err))
|
panic(fmt.Errorf("cannot list organization peoples: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return types.NewPeopleConnection(page), nil
|
return types.NewPeopleConnection(page, r, obj.ID), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Documents is the resolver for the documents field.
|
// Documents is the resolver for the documents field.
|
||||||
@@ -2368,6 +2368,22 @@ func (r *organizationResolver) Data(ctx context.Context, obj *types.Organization
|
|||||||
return types.NewDataConnection(page), nil
|
return types.NewDataConnection(page), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TotalCount is the resolver for the totalCount field.
|
||||||
|
func (r *peopleConnectionResolver) TotalCount(ctx context.Context, obj *types.PeopleConnection) (int, error) {
|
||||||
|
svc := GetTenantService(ctx, r.proboSvc, obj.ParentID.TenantID())
|
||||||
|
|
||||||
|
switch obj.Resolver.(type) {
|
||||||
|
case *organizationResolver:
|
||||||
|
count, err := svc.Peoples.CountForOrganizationID(ctx, obj.ParentID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("cannot count peoples: %w", err)
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
panic(fmt.Errorf("unsupported resolver: %T", obj.Resolver))
|
||||||
|
}
|
||||||
|
|
||||||
// Node is the resolver for the node field.
|
// Node is the resolver for the node field.
|
||||||
func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error) {
|
func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error) {
|
||||||
svc := GetTenantService(ctx, r.proboSvc, id.TenantID())
|
svc := GetTenantService(ctx, r.proboSvc, id.TenantID())
|
||||||
@@ -3027,6 +3043,11 @@ func (r *Resolver) Mutation() schema.MutationResolver { return &mutationResolver
|
|||||||
// Organization returns schema.OrganizationResolver implementation.
|
// Organization returns schema.OrganizationResolver implementation.
|
||||||
func (r *Resolver) Organization() schema.OrganizationResolver { return &organizationResolver{r} }
|
func (r *Resolver) Organization() schema.OrganizationResolver { return &organizationResolver{r} }
|
||||||
|
|
||||||
|
// PeopleConnection returns schema.PeopleConnectionResolver implementation.
|
||||||
|
func (r *Resolver) PeopleConnection() schema.PeopleConnectionResolver {
|
||||||
|
return &peopleConnectionResolver{r}
|
||||||
|
}
|
||||||
|
|
||||||
// Query returns schema.QueryResolver implementation.
|
// Query returns schema.QueryResolver implementation.
|
||||||
func (r *Resolver) Query() schema.QueryResolver { return &queryResolver{r} }
|
func (r *Resolver) Query() schema.QueryResolver { return &queryResolver{r} }
|
||||||
|
|
||||||
@@ -3082,6 +3103,7 @@ type measureResolver struct{ *Resolver }
|
|||||||
type measureConnectionResolver struct{ *Resolver }
|
type measureConnectionResolver struct{ *Resolver }
|
||||||
type mutationResolver struct{ *Resolver }
|
type mutationResolver struct{ *Resolver }
|
||||||
type organizationResolver struct{ *Resolver }
|
type organizationResolver struct{ *Resolver }
|
||||||
|
type peopleConnectionResolver struct{ *Resolver }
|
||||||
type queryResolver struct{ *Resolver }
|
type queryResolver struct{ *Resolver }
|
||||||
type riskResolver struct{ *Resolver }
|
type riskResolver struct{ *Resolver }
|
||||||
type riskConnectionResolver struct{ *Resolver }
|
type riskConnectionResolver struct{ *Resolver }
|
||||||
|
|||||||
Reference in New Issue
Block a user