2024-01-17 05:23:01 +00:00
|
|
|
import { Database } from './database';
|
2024-01-18 08:36:49 +00:00
|
|
|
import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, environmentVariableToGqlType } 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) => {
|
2024-01-18 08:36:49 +00:00
|
|
|
const organizations = await db.getOrganizationsByUserId(context.userId);
|
2024-01-17 11:29:59 +00:00
|
|
|
|
|
|
|
const orgsWithProjectsPromises = organizations.map(async (org) => {
|
2024-01-18 08:36:49 +00:00
|
|
|
const dbProjects = await db.getProjectsByOrganizationId(org.id);
|
2024-01-17 11:29:59 +00:00
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
const projectsWithPromises = dbProjects.map(async (dbProject) => {
|
|
|
|
const dbProjectMembers = await db.getProjectMembers(dbProject.id);
|
|
|
|
const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(dbProject.id);
|
|
|
|
|
|
|
|
const projectMembers = dbProjectMembers.map(dbProjectMember => {
|
|
|
|
return projectMemberToGqlType(dbProjectMember);
|
|
|
|
});
|
|
|
|
|
|
|
|
const environmentVariables = dbEnvironmentVariables.map(dbEnvironmentVariable => {
|
|
|
|
return environmentVariableToGqlType(dbEnvironmentVariable);
|
|
|
|
});
|
|
|
|
|
|
|
|
return projectToGqlType(dbProject, projectMembers, environmentVariables);
|
2024-01-17 11:29:59 +00:00
|
|
|
});
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
const projects = await Promise.all(projectsWithPromises);
|
|
|
|
|
2024-01-17 11:29:59 +00:00
|
|
|
return {
|
|
|
|
...org,
|
|
|
|
projects
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
// TODO: Add organizationMembers field when / if required
|
2024-01-17 11:29:59 +00:00
|
|
|
const orgsWithProjects = await Promise.all(orgsWithProjectsPromises);
|
|
|
|
return orgsWithProjects;
|
2024-01-18 08:36:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deployments: async (_: any, { projectId }: { projectId: string }) => {
|
|
|
|
const dbDeployments = await db.getDeploymentsByProjectId(projectId);
|
|
|
|
|
|
|
|
const deployments = dbDeployments.map(dbDeployment => {
|
|
|
|
return deploymentToGqlType(dbDeployment);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deployments;
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 09:36:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|