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 <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2024-01-25 11:58:38 +05:30 committed by Ashwin Phatak
parent 2fb048e8ab
commit 61120ac44a
3 changed files with 52 additions and 3 deletions

View File

@ -78,6 +78,22 @@ export class Database {
return projects;
}
async getProjectByProjectId (projectId: string): Promise<Project | null> {
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<Deployment[]> {
const deploymentRepository = this.dataSource.getRepository(Deployment);
@ -208,10 +224,21 @@ export class Database {
async updateDeploymentById (deploymentId: string, updates: DeepPartial<Deployment>): Promise<boolean> {
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<Project>): Promise<boolean> {
const projectRepository = this.dataSource.getRepository(Project);
const updateResult = await projectRepository.update({ id: projectId }, updates);
if (updateResult.affected) {
return updateResult.affected > 0;
} else {
return false;
}

View File

@ -49,6 +49,12 @@ export const createResolvers = async (db: Database): Promise<any> => {
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<any> => {
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;
}
}
}
};

View File

@ -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
}