Skip to content

Instantly share code, notes, and snippets.

resolveInput: async ({ resolvedData, context }) => {
if (resolvedData?.products.connect.length) {
const products = await context.query.CartProduct.findMany({
where: {
id: {
in: resolvedData?.products.connect.map(
(el: { id: string }) => el.id
),
},
},
validateInput: async ({ resolvedData, context, addValidationError }) => {
const id = resolvedData.product.connect.id;
const productStocks = await context.query.Product.findOne({
where: { id },
query: 'stock { stock amountInNextDelivery }',
});
if (
resolvedData.amount >
productStocks.stock.stock + productStocks.stock.amountInNextDelivery
) {
beforeOperation: async ({ resolvedData, item, context }) => {
if (resolvedData?.products) {
const dbItems = await context.query.Cart.findOne({
where: { id: item ? (item.id as string) : '' },
query: 'products { id }',
});
if (dbItems.products.length) {
await context.query.CartProduct.deleteMany({
where: dbItems.products.map((el: { id: string }) => ({
id: el.id,
mutation {
updateCart(
where: { id: "cl2dfwo3q00210vk0s3jq6qxm" }
data: {
products: {
create: [
{
product: { connect: { id: "cl2dfz56505040vk0ggih6mll" } }
amount: 5
}
import { list } from '@keystone-6/core';
import { integer, relationship } from '@keystone-6/core/fields';
export const CartProduct = list({
fields: {
product: relationship({ ref: 'Product' }),
amount: integer({ defaultValue: 0, validation: { isRequired: true } }),
}
});
import data from './data.json';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
const { users } = data;
for await (const user of users) {
const currentUser = await prisma.user.findFirst({
where: { email: user.email },
});
{
"users": [
{
"name": "Admin",
"email": "admin@gmail.com",
"password": "$2a$10$mJBzYnc/23jilS8.V1AKgOPlwOtjOcF.wAEoz29qk8SxYno/o6aYC",
"role": "admin"
},
{
"name": "Employee",
import { list } from '@keystone-6/core';
import { text, password, select, relationship } from '@keystone-6/core/fields';
import { RolesValues } from '../consts/roles.const';
import { Roles } from '../enums/roles.enum';
import { filterCustomerAccess, filterCustomerAccessCreate } from '../shared';
export const User = list({
fields: {
name: text({ validation: { isRequired: true } }),
email: text({
access: {
operation: {
create: ({ session }) =>
!!session && session.data.role !== Roles.Customer,
update: ({ session }) =>
!!session && session.data.role !== Roles.Customer,
delete: ({ session }) =>
!!session && session.data.role !== Roles.Customer,
},
},
import { list } from '@keystone-6/core';
import { decimal, relationship, timestamp } from '@keystone-6/core/fields';
import { filterCustomerAccess } from '../shared';
export const Cart = list({
fields: {
user: relationship({
ref: 'User',
ui: {
hideCreate: true,