Skip to content

Commit

Permalink
Convert REST API for Analysis CRUD to GraphQL mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravsapkota committed Aug 15, 2024
1 parent 0d2fa7d commit 38c259e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
55 changes: 54 additions & 1 deletion apps/analysis/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
generate_input_type_for_serializer,
PsGrapheneMutation,
PsDeleteMutation,
PsBulkGrapheneMutation,
)
from deep.permissions import ProjectPermissions as PP

Expand All @@ -17,6 +18,7 @@
AnalysisReport,
AnalysisReportUpload,
AnalysisReportSnapshot,
Analysis,
)
from .schema import (
get_analysis_pillar_qs,
Expand All @@ -31,6 +33,7 @@
AnalysisReportType,
AnalysisReportUploadType,
AnalysisReportSnapshotType,
AnalysisType,
)
from .serializers import (
AnalysisPillarGqlSerializer,
Expand All @@ -42,6 +45,7 @@
AnalysisReportSerializer,
AnalysisReportSnapshotSerializer,
AnalysisReportUploadSerializer,
AnalysisGqlSerializer,
)


Expand Down Expand Up @@ -84,7 +88,7 @@
)


# Analysi Report
# Analysis Report
AnalysisReportInputType = generate_input_type_for_serializer(
'AnalysisReportInputType',
serializer_class=AnalysisReportSerializer,
Expand All @@ -105,6 +109,11 @@
serializer_class=AnalysisReportUploadSerializer,
)

AnalysisInputType = generate_input_type_for_serializer(
'AnalysisInputType',
serializer_class=AnalysisGqlSerializer,
)


class RequiredPermissionMixin():
permissions = [
Expand Down Expand Up @@ -269,6 +278,45 @@ class Arguments:
result = graphene.Field(AnalysisReportUploadType)


class CreateAnalysis(RequiredPermissionMixin, PsGrapheneMutation):
class Arguments:
data = AnalysisInputType(required=True)
model = Analysis
serializer_class = AnalysisGqlSerializer
result = graphene.Field(AnalysisType)


class UpdateAnalysis(RequiredPermissionMixin, PsGrapheneMutation):
class Arguments:
data = AnalysisInputType(required=True)
id = graphene.ID(required=True)
model = Analysis
serializer_class = AnalysisGqlSerializer
result = graphene.Field(AnalysisType)


class DeleteAnalysis(RequiredPermissionMixin, PsDeleteMutation):
class Arguments:
id = graphene.ID(required=True)
model = Analysis
result = graphene.Field(AnalysisType)


class BulkAnalysisInputType(AnalysisInputType):
id = graphene.ID()


class BulkAnalysis(RequiredPermissionMixin, PsBulkGrapheneMutation):
class Arguments:
items = graphene.List(graphene.NonNull(BulkAnalysisInputType))
delete_ids = graphene.List(graphene.NonNull(graphene.ID))

result = graphene.List(AnalysisType)
deleted_result = graphene.List(graphene.NonNull(AnalysisType))
model = Analysis
serializer_class = AnalysisGqlSerializer


class Mutation():
# Analysis Pillar
analysis_pillar_update = UpdateAnalysisPillar.Field()
Expand All @@ -289,3 +337,8 @@ class Mutation():
# -- Uploads
analysis_report_upload_create = CreateAnalysisReportUpload.Field()
analysis_report_upload_delete = DeleteAnalysisReportUpload.Field()
# Analysis
analysis_create = CreateAnalysis.Field()
analysis_update = UpdateAnalysis.Field()
analysis_delete = DeleteAnalysis.Field()
analysis_bulk = BulkAnalysis.Field()
5 changes: 3 additions & 2 deletions apps/analysis/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def validate(self, data):
return data


class AnalysisGqlSerializer(UserResourceSerializer):
class AnalysisGqlSerializer(UserResourceSerializer, ProjectPropertySerializerMixin):
id = IntegerIDField(required=False)
analysis_pillar = AnalysisPillarGqlSerializer(many=True, source='analysispillar_set', required=False)
start_date = serializers.DateField(required=False, allow_null=True)
Expand All @@ -420,10 +420,10 @@ class Meta:
'id',
'title',
'team_lead',
'project',
'start_date',
'end_date',
'cloned_from',
'analysis_pillar',
)

def validate_project(self, project):
Expand All @@ -432,6 +432,7 @@ def validate_project(self, project):
return project

def validate(self, data):
data['project'] = self.project
start_date = data.get('start_date')
end_date = data.get('end_date')
if start_date and start_date > end_date:
Expand Down
60 changes: 60 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ type AnalysisFrameworkVisibleProjectType {
isPrivate: Boolean!
}

input AnalysisInputType {
id: ID
title: String!
teamLead: ID!
startDate: Date
endDate: Date!
clonedFrom: ID
analysisPillar: [AnalysisPillarGqlInputType!]
}

type AnalysisListType {
results: [AnalysisType!]
totalCount: Int
Expand Down Expand Up @@ -311,6 +321,18 @@ type AnalysisPillarEntryListType {
pageSize: Int
}

input AnalysisPillarGqlInputType {
title: String!
mainStatement: String
informationGap: String
filters: GenericScalar
assignee: ID!
analysis: ID!
clonedFrom: ID
statements: [AnalyticalStatementGqlInputType!]
clientId: String
}

type AnalysisPillarListType {
results: [AnalysisPillarType!]
totalCount: Int
Expand Down Expand Up @@ -3265,13 +3287,29 @@ enum AutomaticSummaryStatusEnum {
SEND_FAILED
}

type BulkAnalysis {
errors: [[GenericScalar!]]
result: [AnalysisType]
deletedResult: [AnalysisType!]
}

input BulkAnalysisFrameworkMembershipInputType {
id: ID
member: ID!
role: ID
clientId: String
}

input BulkAnalysisInputType {
id: ID
title: String!
teamLead: ID!
startDate: Date
endDate: Date!
clonedFrom: ID
analysisPillar: [AnalysisPillarGqlInputType!]
}

type BulkEntry {
errors: [[GenericScalar!]]
result: [EntryType]
Expand Down Expand Up @@ -3548,6 +3586,12 @@ type ConnectorSourceType {
statusDisplay: EnumDescription!
}

type CreateAnalysis {
errors: [GenericScalar!]
ok: Boolean
result: AnalysisType
}

type CreateAnalysisFramework {
errors: [GenericScalar!]
ok: Boolean
Expand Down Expand Up @@ -3673,6 +3717,12 @@ scalar DateTime

scalar Decimal

type DeleteAnalysis {
errors: [GenericScalar!]
ok: Boolean
result: AnalysisType
}

type DeleteAnalysisPillarDiscardedEntry {
errors: [GenericScalar!]
ok: Boolean
Expand Down Expand Up @@ -5435,6 +5485,10 @@ type ProjectMutationType {
analysisReportSnapshotCreate(data: AnalysisReportSnapshotInputType!): CreateAnalysisReportSnapshot
analysisReportUploadCreate(data: AnalysisReportUploadInputType!): CreateAnalysisReportUpload
analysisReportUploadDelete(id: ID!): DeleteAnalysisReportUpload
analysisCreate(data: AnalysisInputType!): CreateAnalysis
analysisUpdate(data: AnalysisInputType!, id: ID!): UpdateAnalysis
analysisDelete(id: ID!): DeleteAnalysis
analysisBulk(deleteIds: [ID!], items: [BulkAnalysisInputType!]): BulkAnalysis
exportCreate(data: ExportCreateInputType!): CreateUserExport
exportUpdate(data: ExportUpdateInputType!, id: ID!): UpdateUserExport
exportCancel(id: ID!): CancelUserExport
Expand Down Expand Up @@ -6315,6 +6369,12 @@ input UnifiedConnectorWithSourceInputType {
sources: [ConnectorSourceGqInputType!]
}

type UpdateAnalysis {
errors: [GenericScalar!]
ok: Boolean
result: AnalysisType
}

type UpdateAnalysisFramework {
errors: [GenericScalar!]
ok: Boolean
Expand Down

0 comments on commit 38c259e

Please sign in to comment.