Add create vendor action

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-02-12 11:29:00 -08:00
parent 06dcbb805d
commit 84736a21b4
6 changed files with 401 additions and 11 deletions

View File

@@ -286,3 +286,12 @@ type EvidenceStateTransition {
type Query {
node(id: ID!): Node!
}
type Mutation {
createVendor(input: CreateVendorInput!): Vendor!
}
input CreateVendorInput {
organizationId: ID!
name: String!
}

View File

@@ -44,6 +44,7 @@ type ResolverRoot interface {
Control() ControlResolver
Evidence() EvidenceResolver
Framework() FrameworkResolver
Mutation() MutationResolver
Organization() OrganizationResolver
Query() QueryResolver
Task() TaskResolver
@@ -152,6 +153,10 @@ type ComplexityRoot struct {
Node func(childComplexity int) int
}
Mutation struct {
CreateVendor func(childComplexity int, input types.CreateVendorInput) int
}
Organization struct {
CreatedAt func(childComplexity int) int
Frameworks func(childComplexity int, first *int, after *page.CursorKey, last *int, before *page.CursorKey) int
@@ -261,6 +266,9 @@ type EvidenceResolver interface {
type FrameworkResolver interface {
Controls(ctx context.Context, obj *types.Framework, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.ControlConnection, error)
}
type MutationResolver interface {
CreateVendor(ctx context.Context, input types.CreateVendorInput) (*types.Vendor, error)
}
type OrganizationResolver interface {
Frameworks(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.FrameworkConnection, error)
Vendors(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.VendorConnection, error)
@@ -691,6 +699,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return e.complexity.FrameworkEdge.Node(childComplexity), true
case "Mutation.createVendor":
if e.complexity.Mutation.CreateVendor == nil {
break
}
args, err := ec.field_Mutation_createVendor_args(context.TODO(), rawArgs)
if err != nil {
return 0, false
}
return e.complexity.Mutation.CreateVendor(childComplexity, args["input"].(types.CreateVendorInput)), true
case "Organization.createdAt":
if e.complexity.Organization.CreatedAt == nil {
break
@@ -1099,7 +1119,9 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
opCtx := graphql.GetOperationContext(ctx)
ec := executionContext{opCtx, e, 0, 0, make(chan graphql.DeferredResult)}
inputUnmarshalMap := graphql.BuildUnmarshalerMap()
inputUnmarshalMap := graphql.BuildUnmarshalerMap(
ec.unmarshalInputCreateVendorInput,
)
first := true
switch opCtx.Operation.Operation {
@@ -1133,6 +1155,21 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
return &response
}
case ast.Mutation:
return func(ctx context.Context) *graphql.Response {
if !first {
return nil
}
first = false
ctx = graphql.WithUnmarshalerMap(ctx, inputUnmarshalMap)
data := ec._Mutation(ctx, opCtx.Operation.SelectionSet)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return &graphql.Response{
Data: buf.Bytes(),
}
}
default:
return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
@@ -1469,7 +1506,15 @@ type EvidenceStateTransition {
type Query {
node(id: ID!): Node!
}
`, BuiltIn: false},
type Mutation {
createVendor(input: CreateVendorInput!): Vendor!
}
input CreateVendorInput {
organizationId: ID!
name: String!
}`, BuiltIn: false},
}
var parsedSchema = gqlparser.MustLoadSchema(sources...)
@@ -1785,6 +1830,29 @@ func (ec *executionContext) field_Framework_controls_argsBefore(
return zeroVal, nil
}
func (ec *executionContext) field_Mutation_createVendor_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
arg0, err := ec.field_Mutation_createVendor_argsInput(ctx, rawArgs)
if err != nil {
return nil, err
}
args["input"] = arg0
return args, nil
}
func (ec *executionContext) field_Mutation_createVendor_argsInput(
ctx context.Context,
rawArgs map[string]any,
) (types.CreateVendorInput, error) {
ctx = graphql.WithPathContext(ctx, graphql.NewPathWithField("input"))
if tmp, ok := rawArgs["input"]; ok {
return ec.unmarshalNCreateVendorInput2githubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐCreateVendorInput(ctx, tmp)
}
var zeroVal types.CreateVendorInput
return zeroVal, nil
}
func (ec *executionContext) field_Organization_frameworks_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
@@ -4512,6 +4580,59 @@ func (ec *executionContext) fieldContext_FrameworkEdge_node(_ context.Context, f
return fc, nil
}
func (ec *executionContext) _Mutation_createVendor(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Mutation_createVendor(ctx, field)
if err != nil {
return graphql.Null
}
ctx = graphql.WithFieldContext(ctx, fc)
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (any, error) {
ctx = rctx // use context from middleware stack in children
return ec.resolvers.Mutation().CreateVendor(rctx, fc.Args["input"].(types.CreateVendorInput))
})
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.(*types.Vendor)
fc.Result = res
return ec.marshalNVendor2ᚖgithubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐVendor(ctx, field.Selections, res)
}
func (ec *executionContext) fieldContext_Mutation_createVendor(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Mutation",
Field: field,
IsMethod: true,
IsResolver: true,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
switch field.Name {
case "id":
return ec.fieldContext_Vendor_id(ctx, field)
case "name":
return ec.fieldContext_Vendor_name(ctx, field)
case "createdAt":
return ec.fieldContext_Vendor_createdAt(ctx, field)
case "updatedAt":
return ec.fieldContext_Vendor_updatedAt(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Vendor", field.Name)
},
}
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.field_Mutation_createVendor_args(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return fc, err
}
return fc, nil
}
func (ec *executionContext) _Organization_id(ctx context.Context, field graphql.CollectedField, obj *types.Organization) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Organization_id(ctx, field)
if err != nil {
@@ -8356,6 +8477,40 @@ func (ec *executionContext) fieldContext___Type_specifiedByURL(_ context.Context
// region **************************** input.gotpl *****************************
func (ec *executionContext) unmarshalInputCreateVendorInput(ctx context.Context, obj any) (types.CreateVendorInput, error) {
var it types.CreateVendorInput
asMap := map[string]any{}
for k, v := range obj.(map[string]any) {
asMap[k] = v
}
fieldsInOrder := [...]string{"organizationId", "name"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
continue
}
switch k {
case "organizationId":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("organizationId"))
data, err := ec.unmarshalNID2githubᚗcomᚋgetproboᚋproboᚋpkgᚋgidᚐGID(ctx, v)
if err != nil {
return it, err
}
it.OrganizationID = data
case "name":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name"))
data, err := ec.unmarshalNString2string(ctx, v)
if err != nil {
return it, err
}
it.Name = data
}
}
return it, nil
}
// endregion **************************** input.gotpl *****************************
// region ************************** interface.gotpl ***************************
@@ -9294,6 +9449,55 @@ func (ec *executionContext) _FrameworkEdge(ctx context.Context, sel ast.Selectio
return out
}
var mutationImplementors = []string{"Mutation"}
func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet) graphql.Marshaler {
fields := graphql.CollectFields(ec.OperationContext, sel, mutationImplementors)
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Object: "Mutation",
})
out := graphql.NewFieldSet(fields)
deferred := make(map[string]*graphql.FieldSet)
for i, field := range fields {
innerCtx := graphql.WithRootFieldContext(ctx, &graphql.RootFieldContext{
Object: field.Name,
Field: field,
})
switch field.Name {
case "__typename":
out.Values[i] = graphql.MarshalString("Mutation")
case "createVendor":
out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
return ec._Mutation_createVendor(ctx, field)
})
if out.Values[i] == graphql.Null {
out.Invalids++
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
out.Dispatch(ctx)
if out.Invalids > 0 {
return graphql.Null
}
atomic.AddInt32(&ec.deferred, int32(len(deferred)))
for label, dfs := range deferred {
ec.processDeferredGroup(graphql.DeferredGroup{
Label: label,
Path: graphql.GetPath(ctx),
FieldSet: dfs,
Context: ctx,
})
}
return out
}
var organizationImplementors = []string{"Organization", "Node"}
func (ec *executionContext) _Organization(ctx context.Context, sel ast.SelectionSet, obj *types.Organization) graphql.Marshaler {
@@ -10710,6 +10914,11 @@ func (ec *executionContext) marshalNControlStateTransitionEdge2ᚖgithubᚗcom
return ec._ControlStateTransitionEdge(ctx, sel, v)
}
func (ec *executionContext) unmarshalNCreateVendorInput2githubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐCreateVendorInput(ctx context.Context, v any) (types.CreateVendorInput, error) {
res, err := ec.unmarshalInputCreateVendorInput(ctx, v)
return res, graphql.ErrorOnPath(ctx, err)
}
func (ec *executionContext) unmarshalNCursorKey2githubᚗcomᚋgetproboᚋproboᚋpkgᚋpageᚐCursorKey(ctx context.Context, v any) (page.CursorKey, error) {
res, err := types.UnmarshalCursorKeyScalar(v)
return res, graphql.ErrorOnPath(ctx, err)
@@ -11289,6 +11498,10 @@ func (ec *executionContext) marshalNTaskStateTransitionEdge2ᚖgithubᚗcomᚋge
return ec._TaskStateTransitionEdge(ctx, sel, v)
}
func (ec *executionContext) marshalNVendor2githubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐVendor(ctx context.Context, sel ast.SelectionSet, v types.Vendor) graphql.Marshaler {
return ec._Vendor(ctx, sel, &v)
}
func (ec *executionContext) marshalNVendor2ᚖgithubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐVendor(ctx context.Context, sel ast.SelectionSet, v *types.Vendor) graphql.Marshaler {
if v == nil {
if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {

View File

@@ -60,6 +60,11 @@ type ControlStateTransitionEdge struct {
Node *ControlStateTransition `json:"node"`
}
type CreateVendorInput struct {
OrganizationID gid.GID `json:"organizationId"`
Name string `json:"name"`
}
type Evidence struct {
ID gid.GID `json:"id"`
FileURL string `json:"fileUrl"`
@@ -125,6 +130,9 @@ type FrameworkEdge struct {
Node *Framework `json:"node"`
}
type Mutation struct {
}
type Organization struct {
ID gid.GID `json:"id"`
Name string `json:"name"`

View File

@@ -12,6 +12,7 @@ import (
"github.com/getprobo/probo/pkg/api/console/v1/types"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/getprobo/probo/pkg/probo"
"github.com/getprobo/probo/pkg/probo/coredata"
"github.com/vektah/gqlparser/v2/gqlerror"
)
@@ -64,6 +65,19 @@ func (r *frameworkResolver) Controls(ctx context.Context, obj *types.Framework,
return types.NewControlConnection(page), nil
}
// CreateVendor is the resolver for the createVendor field.
func (r *mutationResolver) CreateVendor(ctx context.Context, input types.CreateVendorInput) (*types.Vendor, error) {
vendor, err := r.svc.CreateVendor(ctx, probo.CreateVendorRequest{
OrganizationID: input.OrganizationID,
Name: input.Name,
})
if err != nil {
return nil, fmt.Errorf("cannot create vendor: %w", err)
}
return types.NewVendor(vendor), nil
}
// 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) {
cursor := types.NewCursor(first, after, last, before)
@@ -191,6 +205,9 @@ func (r *Resolver) Evidence() schema.EvidenceResolver { return &evidenceResolver
// Framework returns schema.FrameworkResolver implementation.
func (r *Resolver) Framework() schema.FrameworkResolver { return &frameworkResolver{r} }
// Mutation returns schema.MutationResolver implementation.
func (r *Resolver) Mutation() schema.MutationResolver { return &mutationResolver{r} }
// Organization returns schema.OrganizationResolver implementation.
func (r *Resolver) Organization() schema.OrganizationResolver { return &organizationResolver{r} }
@@ -203,6 +220,7 @@ func (r *Resolver) Task() schema.TaskResolver { return &taskResolver{r} }
type controlResolver struct{ *Resolver }
type evidenceResolver struct{ *Resolver }
type frameworkResolver struct{ *Resolver }
type mutationResolver struct{ *Resolver }
type organizationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type taskResolver struct{ *Resolver }