Implement GQL resolver to get organizations data (#20)

* Add resolver method for getting organizations

* Fetch project data along with organizations from db

* Refactor code to map db project entity to graphql type

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
prathamesh0 2024-01-17 16:59:59 +05:30 committed by Ashwin Phatak
parent dc88b2c301
commit 1f192444c4
3 changed files with 84 additions and 7 deletions

View File

@ -2,8 +2,11 @@ import { DataSource } from 'typeorm';
import path from 'path';
import debug from 'debug';
import { User } from './entity/User';
import { DatabaseConfig } from './config';
import { User } from './entity/User';
import { Organization } from './entity/Organization';
import { UserOrganization } from './entity/UserOrganization';
import { Project } from './entity/Project';
const log = debug('snowball:database');
@ -33,4 +36,41 @@ export class Database {
return user;
}
async getOrganizationsbyUserId (userId: number) : Promise<Organization[]> {
const userOrganizationRepository = this.dataSource.getRepository(UserOrganization);
const userOrgs = await userOrganizationRepository.find({
relations: {
user: true,
organization: true
},
where: {
user: {
id: userId
}
}
});
const organizations = userOrgs.map(userOrg => userOrg.organization);
return organizations;
}
async getProjectsbyOrganizationId (organizationId: number): Promise<Project[]> {
const projectRepository = this.dataSource.getRepository(Project);
const projects = await projectRepository.find({
relations: {
organization: true,
owner: true
},
where: {
organization: {
id: organizationId
}
}
});
return projects;
}
}

View File

@ -1,15 +1,34 @@
import { Database } from './database';
import { projectToGqlType } from './utils';
export const createResolvers = async (db: Database): Promise<any> => {
return {
Query: {
user: (
_: any,
__: any,
// TODO: add custom type for context
context: any
) => {
user: (_: any, __: any, context: any) => {
return db.getUser(context.userId);
},
organizations: async (_:any, __: any, context: any) => {
const organizations = await db.getOrganizationsbyUserId(context.userId);
const orgsWithProjectsPromises = organizations.map(async (org) => {
const dbProjects = await db.getProjectsbyOrganizationId(org.id);
const projects = dbProjects.map(dbProject => {
return projectToGqlType(dbProject);
});
return {
...org,
projects
};
});
const orgsWithProjects = await Promise.all(orgsWithProjectsPromises);
// TODO: Populate members field when / if required
return orgsWithProjects;
}
}
};

View File

@ -3,6 +3,8 @@ import path from 'path';
import toml from 'toml';
import debug from 'debug';
import { Project } from './entity/Project';
const log = debug('snowball:utils');
export const getConfig = async <ConfigType>(
@ -19,3 +21,19 @@ export const getConfig = async <ConfigType>(
return config;
};
export const projectToGqlType = (dbProject: Project): any => {
return {
id: dbProject.id,
owner: dbProject.owner,
name: dbProject.name,
repository: dbProject.repository,
prodBranch: dbProject.prodBranch,
description: dbProject.description,
template: dbProject.template,
framework: dbProject.framework,
webhooks: dbProject.webhooks,
createdAt: dbProject.createdAt,
updatedAt: dbProject.updatedAt
};
};