From 0fdc2631ee191952a26786381ef96909e88bcd2b Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Fri, 2 Feb 2024 15:02:12 +0530 Subject: [PATCH] Implement Service class and refactor GQL queries in backend (#51) * Add Service class and use in GQL resolver * Refactor resolver query methods to service class --------- Co-authored-by: neeraj --- packages/backend/src/index.ts | 4 ++- packages/backend/src/resolvers.ts | 51 +++++++------------------- packages/backend/src/service.ts | 60 +++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 39 deletions(-) create mode 100644 packages/backend/src/service.ts diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index d2ec61d..e54f633 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -11,6 +11,7 @@ import { createResolvers } from './resolvers'; import { getConfig } from './utils'; import { Config } from './config'; import { DEFAULT_CONFIG_FILE_PATH } from './constants'; +import { Service } from './service'; const log = debug('snowball:server'); @@ -20,6 +21,7 @@ export const main = async (): Promise => { const db = new Database(database); await db.init(); + const service = new Service(db); // TODO: Move to Service class const app = new OAuthApp({ @@ -29,7 +31,7 @@ export const main = async (): Promise => { }); const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString(); - const resolvers = await createResolvers(db, app); + const resolvers = await createResolvers(db, app, service); await createAndStartServer(typeDefs, resolvers, server); }; diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index a6d0de3..bec308a 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -4,8 +4,9 @@ import { DeepPartial } from 'typeorm'; import { OAuthApp } from '@octokit/oauth-app'; +import { Service } from './service'; import { Database } from './database'; -import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, isUserOwner } from './utils'; +import { isUserOwner } from './utils'; import { Environment } from './entity/Deployment'; import { Permission } from './entity/ProjectMember'; import { Domain } from './entity/Domain'; @@ -13,71 +14,45 @@ import { Project } from './entity/Project'; const log = debug('snowball:database'); -export const createResolvers = async (db: Database, app: OAuthApp): Promise => { +// TODO: Remove Database argument and refactor code to Service +export const createResolvers = async (db: Database, app: OAuthApp, service: Service): Promise => { return { Query: { // TODO: add custom type for context user: (_: any, __: any, context: any) => { - return db.getUser(context.userId); + return service.getUser(context.userId); }, organizations: async (_:any, __: any, context: any) => { - return db.getOrganizationsByUserId(context.userId); + return service.getOrganizationsByUserId(context.userId); }, project: async (_: any, { projectId }: { projectId: string }) => { - const dbProject = await db.getProjectById(projectId); - - return dbProject || null; + return service.getProjectById(projectId); }, projectsInOrganization: async (_: any, { organizationId }: {organizationId: string }, context: any) => { - const dbProjects = await db.getProjectsInOrganization(context.userId, organizationId); - return dbProjects; + return service.getProjectsInOrganization(context.userId, organizationId); }, deployments: async (_: any, { projectId }: { projectId: string }) => { - const dbDeployments = await db.getDeploymentsByProjectId(projectId); - - const deployments = dbDeployments.map(dbDeployment => { - return deploymentToGqlType(dbDeployment); - }); - - return deployments; + return service.getDeployementsByProjectId(projectId); }, environmentVariables: async (_: any, { projectId }: { projectId: string }) => { - const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(projectId); - return dbEnvironmentVariables; + return service.getEnvironmentVariablesByProjectId(projectId); }, projectMembers: async (_: any, { projectId }: { projectId: string }) => { - const dbProjectMembers = await db.getProjectMembersByProjectId(projectId); - - const projectMembers = dbProjectMembers.map(dbProjectMember => { - return projectMemberToGqlType(dbProjectMember); - }); - - return projectMembers; + return service.getProjectMembersByProjectId(projectId); }, searchProjects: async (_: any, { searchText }: { searchText: string }, context: any) => { - const dbProjects = await db.getProjectsBySearchText(context.userId, searchText); - - const projects = dbProjects.map((project) => { - return projectToGqlType(project, [], []); - }); - - return projects; + return service.searchProjects(context.userId, searchText); }, domains: async (_:any, { projectId }: { projectId: string }) => { - try { - return db.getDomainsByProjectId(projectId); - } catch (err) { - log(err); - return false; - } + return service.getDomainsByProjectId(projectId); } }, diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts new file mode 100644 index 0000000..8c3798c --- /dev/null +++ b/packages/backend/src/service.ts @@ -0,0 +1,60 @@ +import { Database } from './database'; +import { Deployment } from './entity/Deployment'; +import { Domain } from './entity/Domain'; +import { EnvironmentVariable } from './entity/EnvironmentVariable'; +import { Organization } from './entity/Organization'; +import { Project } from './entity/Project'; +import { ProjectMember } from './entity/ProjectMember'; +import { User } from './entity/User'; + +export class Service { + private db: Database; + + constructor (db: Database) { + this.db = db; + } + + async getUser (userId: number): Promise { + return this.db.getUser(userId); + } + + async getOrganizationsByUserId (userId: number): Promise { + const dbOrganizations = await this.db.getOrganizationsByUserId(userId); + return dbOrganizations; + } + + async getProjectById (projectId: string): Promise { + const dbProject = await this.db.getProjectById(projectId); + return dbProject; + } + + async getProjectsInOrganization (userId:string, organizationId: string): Promise { + const dbProjects = await this.db.getProjectsInOrganization(userId, organizationId); + return dbProjects; + } + + async getDeployementsByProjectId (projectId: string): Promise { + const dbDeployments = await this.db.getDeploymentsByProjectId(projectId); + return dbDeployments; + } + + async getEnvironmentVariablesByProjectId (projectId: string): Promise { + const dbEnvironmentVariables = await this.db.getEnvironmentVariablesByProjectId(projectId); + return dbEnvironmentVariables; + } + + async getProjectMembersByProjectId (projectId: string): Promise { + const dbProjectMembers = await this.db.getProjectMembersByProjectId(projectId); + return dbProjectMembers; + } + + async searchProjects (userId:string, searchText: string): Promise { + const dbProjects = await this.db.getProjectsBySearchText(Number(userId), searchText); + return dbProjects; + } + + async getDomainsByProjectId (projectId: string): Promise { + const dbDomains = await this.db.getDomainsByProjectId(projectId); + return dbDomains; + } +}