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 Jul 25, 2024
1 parent a9d342d commit 752e009
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 5 deletions.
62 changes: 59 additions & 3 deletions apps/analysis/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
generate_input_type_for_serializer,
PsGrapheneMutation,
PsDeleteMutation,
PsBulkGrapheneMutation,
)
from deep.permissions import (
ProjectPermissions as PP,
IsProjectMember,
)
from deep.permissions import ProjectPermissions as PP

from .models import (
AnalysisPillar,
DiscardedEntry,
Expand All @@ -17,6 +20,7 @@
AnalysisReport,
AnalysisReportUpload,
AnalysisReportSnapshot,
Analysis,
)
from .schema import (
get_analysis_pillar_qs,
Expand All @@ -31,6 +35,7 @@
AnalysisReportType,
AnalysisReportUploadType,
AnalysisReportSnapshotType,
AnalysisType,
)
from .serializers import (
AnalysisPillarGqlSerializer,
Expand All @@ -42,6 +47,7 @@
AnalysisReportSerializer,
AnalysisReportSnapshotSerializer,
AnalysisReportUploadSerializer,
AnalysisGqlSerializer,
)


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


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

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


class RequiredPermissionMixin():
permissions = [
Expand Down Expand Up @@ -269,6 +280,46 @@ 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(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
permissions = [IsProjectMember]


class Mutation():
# Analysis Pillar
analysis_pillar_update = UpdateAnalysisPillar.Field()
Expand All @@ -289,3 +340,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 @@ -260,6 +260,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 @@ -304,6 +314,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 @@ -3258,13 +3280,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 @@ -3535,6 +3573,12 @@ type ConnectorSourceType {
statusDisplay: EnumDescription!
}

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

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

scalar Decimal

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

type DeleteAnalysisPillarDiscardedEntry {
errors: [GenericScalar!]
ok: Boolean
Expand Down Expand Up @@ -5421,6 +5471,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 @@ -6301,6 +6355,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 752e009

Please sign in to comment.