2024-01-22 09:42:08 +00:00
|
|
|
import debug from 'debug';
|
2024-02-06 08:48:06 +00:00
|
|
|
import { DeepPartial, FindOptionsWhere } from 'typeorm';
|
2024-01-22 09:42:08 +00:00
|
|
|
|
2024-02-02 09:32:12 +00:00
|
|
|
import { Service } from './service';
|
2024-01-31 11:39:29 +00:00
|
|
|
import { Permission } from './entity/ProjectMember';
|
2024-01-31 12:25:19 +00:00
|
|
|
import { Domain } from './entity/Domain';
|
2024-02-01 08:34:07 +00:00
|
|
|
import { Project } from './entity/Project';
|
2024-02-05 10:51:55 +00:00
|
|
|
import { EnvironmentVariable } from './entity/EnvironmentVariable';
|
2024-01-16 09:36:35 +00:00
|
|
|
|
2024-02-12 06:04:01 +00:00
|
|
|
const log = debug('snowball:resolver');
|
2024-01-22 09:42:08 +00:00
|
|
|
|
2024-02-08 09:29:19 +00:00
|
|
|
export const createResolvers = async (service: Service): 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-02-22 11:56:26 +00:00
|
|
|
return context.user;
|
2024-01-17 11:29:59 +00:00
|
|
|
},
|
|
|
|
|
2024-06-05 16:38:19 +00:00
|
|
|
organizations: async (_: any, __: any, context: any) => {
|
2024-02-22 11:56:26 +00:00
|
|
|
return service.getOrganizationsByUserId(context.user);
|
2024-01-18 08:36:49 +00:00
|
|
|
},
|
|
|
|
|
2024-01-25 06:28:38 +00:00
|
|
|
project: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getProjectById(projectId);
|
2024-01-29 11:01:03 +00:00
|
|
|
},
|
|
|
|
|
2024-06-05 16:38:19 +00:00
|
|
|
projectsInOrganization: async (
|
|
|
|
_: any,
|
|
|
|
{ organizationSlug }: { organizationSlug: string },
|
|
|
|
context: any,
|
|
|
|
) => {
|
|
|
|
return service.getProjectsInOrganization(
|
|
|
|
context.user,
|
|
|
|
organizationSlug,
|
|
|
|
);
|
2024-01-25 06:28:38 +00:00
|
|
|
},
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
deployments: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-19 08:13:29 +00:00
|
|
|
return service.getDeploymentsByProjectId(projectId);
|
2024-01-22 09:42:08 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
environmentVariables: async (
|
|
|
|
_: any,
|
2024-06-05 16:38:19 +00:00
|
|
|
{ projectId }: { projectId: string },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getEnvironmentVariablesByProjectId(projectId);
|
2024-01-25 05:14:51 +00:00
|
|
|
},
|
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
projectMembers: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getProjectMembersByProjectId(projectId);
|
2024-01-24 14:47:43 +00:00
|
|
|
},
|
|
|
|
|
2024-06-05 16:38:19 +00:00
|
|
|
searchProjects: async (
|
|
|
|
_: any,
|
|
|
|
{ searchText }: { searchText: string },
|
|
|
|
context: any,
|
|
|
|
) => {
|
2024-02-22 11:56:26 +00:00
|
|
|
return service.searchProjects(context.user, searchText);
|
2024-01-29 12:48:43 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
domains: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
projectId,
|
2024-06-05 16:38:19 +00:00
|
|
|
filter,
|
|
|
|
}: { projectId: string; filter?: FindOptionsWhere<Domain> },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
return service.getDomainsByProjectId(projectId, filter);
|
2024-06-05 16:38:19 +00:00
|
|
|
},
|
2024-01-22 09:42:08 +00:00
|
|
|
},
|
|
|
|
|
2024-02-01 10:04:17 +00:00
|
|
|
// TODO: Return error in GQL response
|
2024-01-22 09:42:08 +00:00
|
|
|
Mutation: {
|
2024-02-22 05:45:17 +00:00
|
|
|
removeProjectMember: async (
|
|
|
|
_: any,
|
|
|
|
{ projectMemberId }: { projectMemberId: string },
|
2024-06-05 16:38:19 +00:00
|
|
|
context: any,
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-01-22 09:42:08 +00:00
|
|
|
try {
|
2024-06-05 16:38:19 +00:00
|
|
|
return await service.removeProjectMember(
|
|
|
|
context.user,
|
|
|
|
projectMemberId,
|
|
|
|
);
|
2024-01-23 09:31:33 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
updateProjectMember: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
projectMemberId,
|
2024-06-05 16:38:19 +00:00
|
|
|
data,
|
2024-02-22 05:45:17 +00:00
|
|
|
}: {
|
|
|
|
projectMemberId: string;
|
|
|
|
data: {
|
|
|
|
permissions: Permission[];
|
|
|
|
};
|
2024-06-05 16:38:19 +00:00
|
|
|
},
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.updateProjectMember(projectMemberId, data);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-01 10:04:17 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
addProjectMember: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
projectId,
|
2024-06-05 16:38:19 +00:00
|
|
|
data,
|
2024-02-22 05:45:17 +00:00
|
|
|
}: {
|
|
|
|
projectId: string;
|
|
|
|
data: {
|
|
|
|
email: string;
|
|
|
|
permissions: Permission[];
|
|
|
|
};
|
2024-06-05 16:38:19 +00:00
|
|
|
},
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return Boolean(await service.addProjectMember(projectId, data));
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 11:39:29 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
addEnvironmentVariables: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
projectId,
|
2024-06-05 16:38:19 +00:00
|
|
|
data,
|
2024-02-22 05:45:17 +00:00
|
|
|
}: {
|
|
|
|
projectId: string;
|
|
|
|
data: { environments: string[]; key: string; value: string }[];
|
2024-06-05 16:38:19 +00:00
|
|
|
},
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
2024-02-22 05:45:17 +00:00
|
|
|
return Boolean(
|
2024-06-05 16:38:19 +00:00
|
|
|
await service.addEnvironmentVariables(projectId, data),
|
2024-02-22 05:45:17 +00:00
|
|
|
);
|
2024-02-06 08:48:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 05:47:44 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
updateEnvironmentVariable: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
environmentVariableId,
|
2024-06-05 16:38:19 +00:00
|
|
|
data,
|
2024-02-22 05:45:17 +00:00
|
|
|
}: {
|
|
|
|
environmentVariableId: string;
|
|
|
|
data: DeepPartial<EnvironmentVariable>;
|
2024-06-05 16:38:19 +00:00
|
|
|
},
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
2024-02-22 05:45:17 +00:00
|
|
|
return await service.updateEnvironmentVariable(
|
|
|
|
environmentVariableId,
|
2024-06-05 16:38:19 +00:00
|
|
|
data,
|
2024-02-22 05:45:17 +00:00
|
|
|
);
|
2024-02-06 08:48:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 05:06:22 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
removeEnvironmentVariable: async (
|
|
|
|
_: any,
|
2024-06-05 16:38:19 +00:00
|
|
|
{ environmentVariableId }: { environmentVariableId: string },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.removeEnvironmentVariable(environmentVariableId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 05:06:22 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
updateDeploymentToProd: async (
|
|
|
|
_: any,
|
|
|
|
{ deploymentId }: { deploymentId: string },
|
2024-06-05 16:38:19 +00:00
|
|
|
context: any,
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
2024-06-05 16:38:19 +00:00
|
|
|
return Boolean(
|
|
|
|
await service.updateDeploymentToProd(context.user, deploymentId),
|
|
|
|
);
|
2024-02-06 08:48:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 06:28:38 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
addProject: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
organizationSlug,
|
2024-06-05 16:38:19 +00:00
|
|
|
data,
|
2024-02-22 05:45:17 +00:00
|
|
|
}: { organizationSlug: string; data: DeepPartial<Project> },
|
2024-06-05 16:38:19 +00:00
|
|
|
context: any,
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
2024-02-22 11:56:26 +00:00
|
|
|
return await service.addProject(context.user, organizationSlug, data);
|
2024-02-06 08:48:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
2024-02-12 06:04:01 +00:00
|
|
|
throw err;
|
2024-02-06 08:48:06 +00:00
|
|
|
}
|
2024-02-01 12:40:15 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
updateProject: async (
|
|
|
|
_: any,
|
2024-06-05 16:38:19 +00:00
|
|
|
{ projectId, data }: { projectId: string; data: DeepPartial<Project> },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
2024-02-14 12:05:02 +00:00
|
|
|
return await service.updateProject(projectId, data);
|
2024-02-06 08:48:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 12:04:13 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
redeployToProd: async (
|
|
|
|
_: any,
|
|
|
|
{ deploymentId }: { deploymentId: string },
|
2024-06-05 16:38:19 +00:00
|
|
|
context: any,
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
2024-06-05 16:38:19 +00:00
|
|
|
return Boolean(
|
|
|
|
await service.redeployToProd(context.user, deploymentId),
|
|
|
|
);
|
2024-02-06 08:48:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-29 11:01:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteProject: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.deleteProject(projectId);
|
|
|
|
} catch (err) {
|
2024-02-22 05:45:17 +00:00
|
|
|
log(err);
|
|
|
|
return false;
|
2024-02-06 08:48:06 +00:00
|
|
|
}
|
2024-01-29 12:09:51 +00:00
|
|
|
},
|
|
|
|
|
2024-02-01 03:34:24 +00:00
|
|
|
deleteDomain: async (_: any, { domainId }: { domainId: string }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.deleteDomain(domainId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-01 03:34:24 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
rollbackDeployment: async (
|
|
|
|
_: any,
|
|
|
|
{
|
|
|
|
projectId,
|
2024-06-05 16:38:19 +00:00
|
|
|
deploymentId,
|
|
|
|
}: { deploymentId: string; projectId: string },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-01-29 12:09:51 +00:00
|
|
|
try {
|
2024-02-06 08:48:06 +00:00
|
|
|
return await service.rollbackDeployment(projectId, deploymentId);
|
2024-01-29 12:09:51 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-29 12:48:43 +00:00
|
|
|
},
|
|
|
|
|
2024-03-27 16:54:34 +00:00
|
|
|
deleteDeployment: async (
|
|
|
|
_: any,
|
2024-06-05 16:38:19 +00:00
|
|
|
{ deploymentId }: { deploymentId: string },
|
2024-03-27 16:54:34 +00:00
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
return await service.deleteDeployment(deploymentId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
addDomain: async (
|
|
|
|
_: any,
|
2024-06-05 16:38:19 +00:00
|
|
|
{ projectId, data }: { projectId: string; data: { name: string } },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-01-29 12:48:43 +00:00
|
|
|
try {
|
2024-02-14 12:05:02 +00:00
|
|
|
return Boolean(await service.addDomain(projectId, data));
|
2024-01-29 12:48:43 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 08:31:09 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
updateDomain: async (
|
|
|
|
_: any,
|
2024-06-05 16:38:19 +00:00
|
|
|
{ domainId, data }: { domainId: string; data: DeepPartial<Domain> },
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-01-30 08:31:09 +00:00
|
|
|
try {
|
2024-02-14 12:05:02 +00:00
|
|
|
return await service.updateDomain(domainId, data);
|
2024-01-30 08:31:09 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 14:20:53 +00:00
|
|
|
},
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
authenticateGitHub: async (
|
|
|
|
_: any,
|
|
|
|
{ code }: { code: string },
|
2024-06-05 16:38:19 +00:00
|
|
|
context: any,
|
2024-02-22 05:45:17 +00:00
|
|
|
) => {
|
2024-02-08 09:29:19 +00:00
|
|
|
try {
|
2024-02-22 11:56:26 +00:00
|
|
|
return await service.authenticateGitHub(code, context.user);
|
2024-02-08 09:29:19 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 13:21:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
unauthenticateGitHub: async (_: any, __: object, context: any) => {
|
|
|
|
try {
|
2024-06-05 16:38:19 +00:00
|
|
|
return service.unauthenticateGitHub(context.user, {
|
|
|
|
gitHubToken: null,
|
|
|
|
});
|
2024-01-31 13:21:53 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-05 16:38:19 +00:00
|
|
|
},
|
|
|
|
},
|
2024-01-16 09:36:35 +00:00
|
|
|
};
|
|
|
|
};
|