Refactor model cursor to use orderBy
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -20,7 +20,7 @@ import (
|
||||
)
|
||||
|
||||
// Tasks is the resolver for the tasks field.
|
||||
func (r *controlResolver) Tasks(ctx context.Context, obj *types.Control, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.TaskConnection, error) {
|
||||
func (r *controlResolver) Tasks(ctx context.Context, obj *types.Control, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.TaskOrder) (*types.TaskConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
@@ -45,7 +45,7 @@ func (r *evidenceResolver) FileURL(ctx context.Context, obj *types.Evidence) (st
|
||||
}
|
||||
|
||||
// Controls is the resolver for the controls field.
|
||||
func (r *frameworkResolver) Controls(ctx context.Context, obj *types.Framework, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.ControlConnection, error) {
|
||||
func (r *frameworkResolver) Controls(ctx context.Context, obj *types.Framework, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.ControlOrder) (*types.ControlConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
@@ -77,7 +77,7 @@ func (r *mutationResolver) CreateVendor(ctx context.Context, input types.CreateV
|
||||
return nil, fmt.Errorf("cannot create vendor: %w", err)
|
||||
}
|
||||
return &types.CreateVendorPayload{
|
||||
VendorEdge: types.NewVendorEdge(vendor),
|
||||
VendorEdge: types.NewVendorEdge(vendor, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ func (r *mutationResolver) CreatePeople(ctx context.Context, input types.CreateP
|
||||
}
|
||||
|
||||
return &types.CreatePeoplePayload{
|
||||
PeopleEdge: types.NewPeopleEdge(people),
|
||||
PeopleEdge: types.NewPeopleEdge(people, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (r *mutationResolver) CreateOrganization(ctx context.Context, input types.C
|
||||
*tenantIDs = append(*tenantIDs, organization.ID.TenantID())
|
||||
|
||||
return &types.CreateOrganizationPayload{
|
||||
OrganizationEdge: types.NewOrganizationEdge(organization),
|
||||
OrganizationEdge: types.NewOrganizationEdge(organization, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ func (r *mutationResolver) CreateTask(ctx context.Context, input types.CreateTas
|
||||
}
|
||||
|
||||
return &types.CreateTaskPayload{
|
||||
TaskEdge: types.NewTaskEdge(task),
|
||||
TaskEdge: types.NewTaskEdge(task, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ func (r *mutationResolver) CreateFramework(ctx context.Context, input types.Crea
|
||||
}
|
||||
|
||||
return &types.CreateFrameworkPayload{
|
||||
FrameworkEdge: types.NewFrameworkEdge(framework),
|
||||
FrameworkEdge: types.NewFrameworkEdge(framework, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -362,7 +362,7 @@ func (r *mutationResolver) ImportFramework(ctx context.Context, input types.Impo
|
||||
}
|
||||
|
||||
return &types.ImportFrameworkPayload{
|
||||
FrameworkEdge: types.NewFrameworkEdge(framework),
|
||||
FrameworkEdge: types.NewFrameworkEdge(framework, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -382,7 +382,7 @@ func (r *mutationResolver) CreateControl(ctx context.Context, input types.Create
|
||||
}
|
||||
|
||||
return &types.CreateControlPayload{
|
||||
ControlEdge: types.NewControlEdge(control),
|
||||
ControlEdge: types.NewControlEdge(control, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -424,7 +424,7 @@ func (r *mutationResolver) UploadEvidence(ctx context.Context, input types.Uploa
|
||||
}
|
||||
|
||||
return &types.UploadEvidencePayload{
|
||||
EvidenceEdge: types.NewEvidenceEdge(evidence),
|
||||
EvidenceEdge: types.NewEvidenceEdge(evidence, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -459,7 +459,7 @@ func (r *mutationResolver) CreatePolicy(ctx context.Context, input types.CreateP
|
||||
}
|
||||
|
||||
return &types.CreatePolicyPayload{
|
||||
PolicyEdge: types.NewPolicyEdge(policy),
|
||||
PolicyEdge: types.NewPolicyEdge(policy, nil),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -564,7 +564,7 @@ func (r *organizationResolver) LogoURL(ctx context.Context, obj *types.Organizat
|
||||
}
|
||||
|
||||
// Users is the resolver for the users field.
|
||||
func (r *organizationResolver) Users(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.UserConnection, error) {
|
||||
func (r *organizationResolver) Users(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.UserOrder) (*types.UserConnection, error) {
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
page, err := r.usrmgrSvc.ListUsersForTenant(ctx, obj.ID, cursor)
|
||||
@@ -576,7 +576,7 @@ func (r *organizationResolver) Users(ctx context.Context, obj *types.Organizatio
|
||||
}
|
||||
|
||||
// Frameworks is the resolver for the frameworks field.
|
||||
func (r *organizationResolver) Frameworks(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.FrameworkConnection, error) {
|
||||
func (r *organizationResolver) Frameworks(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.FrameworkOrder) (*types.FrameworkConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
@@ -590,10 +590,10 @@ func (r *organizationResolver) Frameworks(ctx context.Context, obj *types.Organi
|
||||
}
|
||||
|
||||
// Vendors is the resolver for the vendors field.
|
||||
func (r *organizationResolver) Vendors(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.VendorConnection, error) {
|
||||
func (r *organizationResolver) Vendors(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *page.OrderBy) (*types.VendorConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
cursor := types.NewCursorWithOrder(first, after, last, before, orderBy)
|
||||
|
||||
page, err := svc.Vendors.ListForOrganizationID(ctx, obj.ID, cursor)
|
||||
if err != nil {
|
||||
@@ -604,11 +604,10 @@ func (r *organizationResolver) Vendors(ctx context.Context, obj *types.Organizat
|
||||
}
|
||||
|
||||
// Peoples is the resolver for the peoples field.
|
||||
func (r *organizationResolver) Peoples(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.PeopleConnection, error) {
|
||||
func (r *organizationResolver) Peoples(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *page.OrderBy) (*types.PeopleConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
cursor := types.NewCursorWithOrder(first, after, last, before, orderBy)
|
||||
page, err := svc.Peoples.ListForOrganizationID(ctx, obj.ID, cursor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list organization peoples: %w", err)
|
||||
@@ -618,8 +617,9 @@ func (r *organizationResolver) Peoples(ctx context.Context, obj *types.Organizat
|
||||
}
|
||||
|
||||
// Policies is the resolver for the policies field.
|
||||
func (r *organizationResolver) Policies(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.PolicyConnection, error) {
|
||||
func (r *organizationResolver) Policies(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.PolicyOrder) (*types.PolicyConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
page, err := svc.Policies.ListByOrganizationID(ctx, obj.ID, cursor)
|
||||
@@ -747,7 +747,7 @@ func (r *taskResolver) AssignedTo(ctx context.Context, obj *types.Task) (*types.
|
||||
}
|
||||
|
||||
// Evidences is the resolver for the evidences field.
|
||||
func (r *taskResolver) Evidences(ctx context.Context, obj *types.Task, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.EvidenceConnection, error) {
|
||||
func (r *taskResolver) Evidences(ctx context.Context, obj *types.Task, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.EvidenceOrder) (*types.EvidenceConnection, error) {
|
||||
svc := r.GetTenantServiceIfAuthorized(ctx, obj.ID.TenantID())
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
@@ -760,9 +760,10 @@ func (r *taskResolver) Evidences(ctx context.Context, obj *types.Task, first *in
|
||||
}
|
||||
|
||||
// Organizations is the resolver for the organizations field.
|
||||
func (r *viewerResolver) Organizations(ctx context.Context, obj *types.Viewer, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.OrganizationConnection, error) {
|
||||
func (r *viewerResolver) Organizations(ctx context.Context, obj *types.Viewer, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.OrganizationOrder) (*types.OrganizationConnection, error) {
|
||||
user := UserFromContext(ctx)
|
||||
|
||||
// For now, we're not using cursor pagination since we're loading all organizations
|
||||
organizations, err := r.usrmgrSvc.ListOrganizationsForUserID(ctx, user.ID)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to list organizations for user: %w", err))
|
||||
@@ -770,12 +771,16 @@ func (r *viewerResolver) Organizations(ctx context.Context, obj *types.Viewer, f
|
||||
|
||||
var edges []*types.OrganizationEdge
|
||||
for _, organization := range organizations {
|
||||
edges = append(edges, types.NewOrganizationEdge(organization))
|
||||
edges = append(edges, types.NewOrganizationEdge(organization, nil))
|
||||
}
|
||||
|
||||
// The simple implementation doesn't handle pagination yet
|
||||
return &types.OrganizationConnection{
|
||||
Edges: edges,
|
||||
PageInfo: &types.PageInfo{},
|
||||
Edges: edges,
|
||||
PageInfo: &types.PageInfo{
|
||||
HasNextPage: false,
|
||||
HasPreviousPage: false,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -815,3 +820,23 @@ type policyResolver struct{ *Resolver }
|
||||
type queryResolver struct{ *Resolver }
|
||||
type taskResolver struct{ *Resolver }
|
||||
type viewerResolver struct{ *Resolver }
|
||||
|
||||
// !!! WARNING !!!
|
||||
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
|
||||
// one last chance to move it out of harms way if you want. There are two reasons this happens:
|
||||
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
|
||||
// it when you're done.
|
||||
// - You have helper methods in this file. Move them out to keep these resolver files clean.
|
||||
/*
|
||||
func (r *peopleOrderResolver) Field(ctx context.Context, obj *page.OrderBy, data page.GenericOrderField) error {
|
||||
panic(fmt.Errorf("not implemented: Field - field"))
|
||||
}
|
||||
func (r *vendorOrderResolver) Field(ctx context.Context, obj *page.OrderBy, data page.GenericOrderField) error {
|
||||
obj.Field = data
|
||||
return nil
|
||||
}
|
||||
func (r *Resolver) PeopleOrder() schema.PeopleOrderResolver { return &peopleOrderResolver{r} }
|
||||
func (r *Resolver) VendorOrder() schema.VendorOrderResolver { return &vendorOrderResolver{r} }
|
||||
type peopleOrderResolver struct{ *Resolver }
|
||||
type vendorOrderResolver struct{ *Resolver }
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user