-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
215 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createYoga } from 'graphql-yoga'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import schema from '@vm/graphql/schema/cli'; | ||
|
||
export default createYoga<{ | ||
req: NextApiRequest; | ||
res: NextApiResponse; | ||
}>({ | ||
schema, | ||
graphqlEndpoint: '/api/cli/graphql', | ||
}); | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { GraphQLSchema } from 'graphql/type'; | ||
|
||
import { builder } from '../../builder'; | ||
import '../../types'; | ||
import '../nodes'; | ||
import './version'; | ||
|
||
const schema: GraphQLSchema = builder.toSchema(); | ||
export default schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ZodError } from 'zod'; | ||
|
||
import prismaClient from '@vm/prisma/client'; | ||
|
||
import { builder } from '../../builder'; | ||
|
||
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; | ||
}, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import prismaClient from '@vm/prisma/client'; | ||
|
||
import { builder } from '../../builder'; | ||
|
||
builder.prismaNode('Environment', { | ||
id: { field: 'id' }, | ||
fields: (t) => ({ | ||
name: t.exposeString('name'), | ||
project: t.relation('project'), | ||
versions: t.prismaConnection({ | ||
type: 'Version', | ||
cursor: 'id', | ||
resolve: (query, parent) => prismaClient.version.findMany({ ...query, where: { environmentId: parent.id } }), | ||
}), | ||
currentVersion: t.prismaField({ | ||
type: 'Version', | ||
nullable: true, | ||
resolve: async (query, parent) => | ||
prismaClient.version.findFirst({ | ||
...query, | ||
where: { | ||
isCurrent: true, | ||
environmentId: parent.id, | ||
}, | ||
}), | ||
}), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import './environment'; | ||
import './project'; | ||
import './service'; | ||
import './serviceVersion'; | ||
import './version'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import prismaClient from '@vm/prisma/client'; | ||
|
||
import { builder } from '../../builder'; | ||
|
||
builder.prismaNode('Project', { | ||
id: { field: 'id' }, | ||
fields: (t) => ({ | ||
name: t.exposeString('name'), | ||
environments: t.prismaConnection({ | ||
type: 'Environment', | ||
cursor: 'id', | ||
resolve: (query, parent, args, context, info) => | ||
prismaClient.environment.findMany({ ...query, where: { projectId: parent.id } }), | ||
}), | ||
services: t.prismaConnection({ | ||
type: 'Service', | ||
cursor: 'id', | ||
resolve: (query, parent, args, context, info) => | ||
prismaClient.service.findMany({ ...query, where: { projectId: parent.id } }), | ||
}), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import prismaClient from '@vm/prisma/client'; | ||
|
||
import { builder } from '../../builder'; | ||
|
||
builder.prismaNode('Service', { | ||
id: { field: 'id' }, | ||
fields: (t) => ({ | ||
name: t.exposeString('name'), | ||
project: t.relation('project'), | ||
versions: t.prismaConnection({ | ||
type: 'ServiceVersion', | ||
cursor: 'id', | ||
resolve: (query, parent) => prismaClient.serviceVersion.findMany({ ...query, where: { serviceId: parent.id } }), | ||
}), | ||
currentVersion: t.prismaField({ | ||
type: 'ServiceVersion', | ||
nullable: true, | ||
resolve: async (query, parent) => | ||
prismaClient.serviceVersion.findFirst({ | ||
...query, | ||
where: { | ||
isCurrent: true, | ||
serviceId: parent.id, | ||
}, | ||
}), | ||
}), | ||
}), | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/graphql/schema/serviceVersion.ts → ...es/graphql/schema/nodes/serviceVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import prismaClient from '@vm/prisma/client'; | ||
|
||
import { builder } from '../../builder'; | ||
|
||
builder.prismaNode('Version', { | ||
id: { field: 'id' }, | ||
fields: (t) => ({ | ||
name: t.exposeString('name'), | ||
createdAt: t.expose('createdAt', { | ||
type: 'DateTime', | ||
}), | ||
isCurrent: t.exposeBoolean('isCurrent'), | ||
environment: t.relation('environment'), | ||
serviceVersions: t.prismaConnection({ | ||
type: 'ServiceVersion', | ||
cursor: 'id', | ||
resolve: (query, parent) => prismaClient.serviceVersion.findMany({ ...query, where: { versionId: parent.id } }), | ||
}), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters