Skip to content

Commit

Permalink
Add missing GraphQL mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
mkleszcz committed May 4, 2023
1 parent 3370170 commit 47950e8
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
},
"dependencies": {
"@apollo/client": "^3.7.13",
"@headlessui/react": "^1.7.14",
"@heroicons/react": "^2.0.17",
"@types/node": "18.16.3",
"@types/react": "18.2.0",
"@types/react-dom": "18.2.1",
Expand Down
56 changes: 56 additions & 0 deletions packages/graphql/schema/environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ZodError } from 'zod';

import prismaClient from '@vm/prisma/client';

import { builder } from '../builder';
Expand Down Expand Up @@ -26,3 +28,57 @@ builder.prismaNode('Environment', {
}),
}),
});

const EnvironmentInput = builder.inputType('EnvironmentInput', {
fields: (t) => ({
name: t.string({
required: true,
}),
projectId: t.string({
required: true,
validate: {
uuid: true,
refine: [
async (id) => {
const count = await prismaClient.project.count({
where: {
id,
},
});
return count === 1;
},
{ message: 'ProjectId is invalid' },
],
},
}),
}),
validate: [
async (args) => {
const name = args['name'];
const projectId = args['projectId'];
if (!name || !projectId) return false;
const count = await prismaClient.environment.count({
where: {
name,
projectId,
},
});
return count === 0;
},
{ message: 'Environment name must be unique' },
],
});

builder.mutationField('createEnvironment', (t) =>
t.prismaField({
type: 'Environment',
errors: {
types: [ZodError],
},
args: { input: t.arg({ type: EnvironmentInput, required: true }) },
resolve: (root, _parent, args) =>
prismaClient.environment.create({
data: args.input,
}),
})
);
11 changes: 11 additions & 0 deletions packages/graphql/schema/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ const ProjectInput = builder.inputType('ProjectInput', {
required: true,
validate: {
maxLength: 255,
refine: [
async (val) => {
const count = await prismaClient.project.count({
where: {
name: val,
},
});
return count === 0;
},
{ message: 'Project name must be unique' },
],
},
}),
}),
Expand Down
56 changes: 56 additions & 0 deletions packages/graphql/schema/service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ZodError } from 'zod';

import prismaClient from '@vm/prisma/client';

import { builder } from '../builder';
Expand Down Expand Up @@ -26,3 +28,57 @@ builder.prismaNode('Service', {
}),
}),
});

const ServiceInput = builder.inputType('ServiceInput', {
fields: (t) => ({
name: t.string({
required: true,
}),
projectId: t.string({
required: true,
validate: {
uuid: true,
refine: [
async (id) => {
const count = await prismaClient.project.count({
where: {
id,
},
});
return count === 1;
},
{ message: 'ProjectId is invalid' },
],
},
}),
}),
validate: [
async (args) => {
const name = args['name'];
const projectId = args['projectId'];
if (!name || !projectId) return false;
const count = await prismaClient.service.count({
where: {
name,
projectId,
},
});
return count === 0;
},
{ message: 'Service name must be unique' },
],
});

builder.mutationField('createService', (t) =>
t.prismaField({
type: 'Service',
errors: {
types: [ZodError],
},
args: { input: t.arg({ type: ServiceInput, required: true }) },
resolve: (root, _parent, args) =>
prismaClient.service.create({
data: args.input,
}),
})
);
80 changes: 80 additions & 0 deletions packages/graphql/schema/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ZodError } from 'zod';

import prismaClient from '@vm/prisma/client';

import { builder } from '../builder';
Expand All @@ -18,3 +20,81 @@ builder.prismaNode('Version', {
}),
}),
});

const VersionInput = builder.inputType('VersionInput', {
fields: (t) => ({
name: t.string({
required: true,
}),
environmentId: t.string({
required: true,
validate: {
uuid: true,
refine: [
async (id) => {
const count = await prismaClient.environment.count({
where: {
id,
},
});
return count === 1;
},
{ message: 'EnvironmentId is invalid' },
],
},
}),
isCurrent: t.boolean({
required: true,
validate: {
type: 'boolean',
},
}),
}),
validate: [
async (args) => {
const name = args['name'];
const environmentId = args['environmentId'];
if (!name || !environmentId) return false;
const count = await prismaClient.version.count({
where: {
name,
environmentId,
},
});
return count === 0;
},
{ message: 'Version name must be unique' },
],
});

builder.mutationField('createVersion', (t) =>
t.prismaField({
type: 'Version',
errors: {
types: [ZodError],
},
args: { input: t.arg({ type: VersionInput, required: true }) },
resolve: async (root, _parent, args) => {
const { input } = args;
const getCreateVersionQuery = () =>
prismaClient.version.create({
data: input,
});
if (!input.isCurrent) {
return getCreateVersionQuery();
}
const [_, version] = await prismaClient.$transaction([
prismaClient.version.updateMany({
where: {
environmentId: input.environmentId,
},
data: {
isCurrent: false,
},
}),
getCreateVersionQuery(),
]);
return version;
},
})
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- A unique constraint covering the columns `[name]` on the table `Project` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "Project_name_key" ON "Project"("name");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- A unique constraint covering the columns `[name,projectId]` on the table `Environment` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "Environment_name_projectId_key" ON "Environment"("name", "projectId");
2 changes: 2 additions & 0 deletions packages/prisma/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ model Project {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
services Service[]
@@unique(fields: [name], name: "uniqueProjectName")
}

model Service {
Expand All @@ -40,6 +41,7 @@ model Environment {
versions Version[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique(fields: [name, projectId], name: "uniqueEnvironmentName")
}

model Version {
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47950e8

Please sign in to comment.