From 61120ac44aee6b44a5522db9b42d61d05e913d5f Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Thu, 25 Jan 2024 11:58:38 +0530 Subject: [PATCH] Add GQL mutation for updating Project entity data (#40) * Add update and query method for project * Rename variables to update result --------- Co-authored-by: neeraj --- packages/backend/src/database.ts | 33 ++++++++++++++++++++++++++++--- packages/backend/src/resolvers.ts | 15 ++++++++++++++ packages/backend/src/schema.gql | 7 +++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 6e31ff41..4716af25 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -78,6 +78,22 @@ export class Database { return projects; } + async getProjectByProjectId (projectId: string): Promise { + const projectRepository = this.dataSource.getRepository(Project); + + const project = await projectRepository.findOne({ + relations: { + organization: true, + owner: true + }, + where: { + id: projectId + } + }); + + return project; + } + async getDeploymentsByProjectId (projectId: string): Promise { const deploymentRepository = this.dataSource.getRepository(Deployment); @@ -208,10 +224,21 @@ export class Database { async updateDeploymentById (deploymentId: string, updates: DeepPartial): Promise { const deploymentRepository = this.dataSource.getRepository(Deployment); - const updatedDeployment = await deploymentRepository.update({ id: Number(deploymentId) }, updates); + const updateResult = await deploymentRepository.update({ id: Number(deploymentId) }, updates); - if (updatedDeployment.affected) { - return updatedDeployment.affected > 0; + if (updateResult.affected) { + return updateResult.affected > 0; + } else { + return false; + } + } + + async updateProjectById (projectId: string, updates: DeepPartial): Promise { + const projectRepository = this.dataSource.getRepository(Project); + const updateResult = await projectRepository.update({ id: projectId }, updates); + + if (updateResult.affected) { + return updateResult.affected > 0; } else { return false; } diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index bc7b1a72..7b4508d5 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -49,6 +49,12 @@ export const createResolvers = async (db: Database): Promise => { return orgsWithProjects; }, + project: async (_: any, { projectId }: { projectId: string }) => { + const dbProject = await db.getProjectByProjectId(projectId); + + return dbProject ? projectToGqlType(dbProject, [], []) : null; + }, + deployments: async (_: any, { projectId }: { projectId: string }) => { const dbDeployments = await db.getDeploymentsByProjectId(projectId); @@ -131,6 +137,15 @@ export const createResolvers = async (db: Database): Promise => { log(err); return false; } + }, + + updateProject: async (_: any, { projectId, updateProject }: { projectId: string, updateProject: { name: string, description: string } }) => { + try { + return db.updateProjectById(projectId, updateProject); + } catch (err) { + log(err); + return false; + } } } }; diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index 09387f3f..a0c6c13c 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -115,6 +115,7 @@ type Query { user: User! organizations: [Organization!] projects: [Project!] + project(projectId: String!): Project deployments(projectId: String!): [Deployment!] environmentVariables(projectId: String!): [EnvironmentVariable!] projectMembers(projectId: String!): [ProjectMember!] @@ -125,6 +126,7 @@ type Mutation { removeMember(memberId: String!): Boolean! addEnvironmentVariables(projectId: String!, environmentVariables: [AddEnvironmentVariableInput!]): Boolean! updateDeploymentToProd(deploymentId: String!): Boolean! + updateProject(projectId: String!, updateProject: UpdateProjectInput): Boolean! } input AddEnvironmentVariableInput { @@ -132,3 +134,8 @@ input AddEnvironmentVariableInput { key: String! value: String! } + +input UpdateProjectInput { + name: String + description: String +}