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:
parent
dc88b2c301
commit
1f192444c4
@ -2,8 +2,11 @@ import { DataSource } from 'typeorm';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
|
|
||||||
import { User } from './entity/User';
|
|
||||||
import { DatabaseConfig } from './config';
|
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');
|
const log = debug('snowball:database');
|
||||||
|
|
||||||
@ -33,4 +36,41 @@ export class Database {
|
|||||||
|
|
||||||
return user;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,34 @@
|
|||||||
import { Database } from './database';
|
import { Database } from './database';
|
||||||
|
import { projectToGqlType } from './utils';
|
||||||
|
|
||||||
export const createResolvers = async (db: Database): Promise<any> => {
|
export const createResolvers = async (db: Database): Promise<any> => {
|
||||||
return {
|
return {
|
||||||
Query: {
|
Query: {
|
||||||
user: (
|
// TODO: add custom type for context
|
||||||
_: any,
|
user: (_: any, __: any, context: any) => {
|
||||||
__: any,
|
|
||||||
// TODO: add custom type for context
|
|
||||||
context: any
|
|
||||||
) => {
|
|
||||||
return db.getUser(context.userId);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -3,6 +3,8 @@ import path from 'path';
|
|||||||
import toml from 'toml';
|
import toml from 'toml';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
|
|
||||||
|
import { Project } from './entity/Project';
|
||||||
|
|
||||||
const log = debug('snowball:utils');
|
const log = debug('snowball:utils');
|
||||||
|
|
||||||
export const getConfig = async <ConfigType>(
|
export const getConfig = async <ConfigType>(
|
||||||
@ -19,3 +21,19 @@ export const getConfig = async <ConfigType>(
|
|||||||
|
|
||||||
return config;
|
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
|
||||||
|
};
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user