2024-01-17 05:23:01 +00:00
|
|
|
import { Database } from './database';
|
2024-01-17 11:29:59 +00:00
|
|
|
import { projectToGqlType } from './utils';
|
2024-01-16 09:36:35 +00:00
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
export const createResolvers = async (db: Database): Promise<any> => {
|
2024-01-16 09:36:35 +00:00
|
|
|
return {
|
|
|
|
Query: {
|
2024-01-17 11:29:59 +00:00
|
|
|
// TODO: add custom type for context
|
|
|
|
user: (_: any, __: any, context: any) => {
|
2024-01-17 05:23:01 +00:00
|
|
|
return db.getUser(context.userId);
|
2024-01-17 11:29:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 09:36:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|