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 <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2024-02-02 15:02:12 +05:30 committed by GitHub
parent 8111d34d86
commit 0fdc2631ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 76 additions and 39 deletions

View File

@ -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<void> => {
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<void> => {
});
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);
};

View File

@ -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<any> => {
// TODO: Remove Database argument and refactor code to Service
export const createResolvers = async (db: Database, app: OAuthApp, service: Service): Promise<any> => {
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);
}
},

View File

@ -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<User | null> {
return this.db.getUser(userId);
}
async getOrganizationsByUserId (userId: number): Promise<Organization[]> {
const dbOrganizations = await this.db.getOrganizationsByUserId(userId);
return dbOrganizations;
}
async getProjectById (projectId: string): Promise<Project | null> {
const dbProject = await this.db.getProjectById(projectId);
return dbProject;
}
async getProjectsInOrganization (userId:string, organizationId: string): Promise<Project[]> {
const dbProjects = await this.db.getProjectsInOrganization(userId, organizationId);
return dbProjects;
}
async getDeployementsByProjectId (projectId: string): Promise<Deployment[]> {
const dbDeployments = await this.db.getDeploymentsByProjectId(projectId);
return dbDeployments;
}
async getEnvironmentVariablesByProjectId (projectId: string): Promise<EnvironmentVariable[]> {
const dbEnvironmentVariables = await this.db.getEnvironmentVariablesByProjectId(projectId);
return dbEnvironmentVariables;
}
async getProjectMembersByProjectId (projectId: string): Promise<ProjectMember[]> {
const dbProjectMembers = await this.db.getProjectMembersByProjectId(projectId);
return dbProjectMembers;
}
async searchProjects (userId:string, searchText: string): Promise<Project[]> {
const dbProjects = await this.db.getProjectsBySearchText(Number(userId), searchText);
return dbProjects;
}
async getDomainsByProjectId (projectId: string): Promise<Domain[]> {
const dbDomains = await this.db.getDomainsByProjectId(projectId);
return dbDomains;
}
}