Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-quiet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": minor
---

Add a `--quiet' flag to `keystone` CLI commands, suppressing most output except for errors
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { createExpressServer } from '../lib/createExpressServer'
export { createSystem } from '../lib/createSystem'
export { buildArtifacts, generateArtifacts } from '../artifacts'
export { createExpressServer } from '../lib/express'
export { withMigrate } from '../lib/migrations'
export { generateArtifacts, getArtifacts } from '../artifacts'
export { createSystem } from '../lib/system'
export { ExitError } from '../scripts/utils'
9 changes: 5 additions & 4 deletions packages/core/src/admin-ui/system/generateAdminUI.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type Entry, walk as _walk } from '@nodelib/fs.walk'
import fse from 'fs-extra'
import fs from 'node:fs/promises'
import Path from 'node:path'
import { promisify } from 'node:util'
import fs from 'node:fs/promises'
import fse from 'fs-extra'
import { type Entry, walk as _walk } from '@nodelib/fs.walk'

import type { AdminMetaSource } from '../../lib/admin-meta'
import type { AdminFileToWrite, KeystoneConfig } from '../../types'
import { writeAdminFiles } from '../templates'
import type { AdminMetaSource } from '../../lib/create-admin-meta'

const walk = promisify(_walk)

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/admin-ui/templates/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Path from 'path'
import Path from 'node:path'
import resolve from 'resolve'

import type { AdminMetaSource } from '../../lib/create-admin-meta'
import type { AdminMetaSource } from '../../lib/admin-meta'
import type { KeystoneConfig } from '../../types'

function doesConfigExist(path: string[]) {
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/admin-ui/templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import path from 'path'
import path from 'node:path'

import type { AdminMetaSource } from '../../lib/admin-meta'
import type { KeystoneConfig } from '../../types'
import type { AdminMetaSource } from '../../lib/create-admin-meta'
import { appTemplate } from './app'
import { createItemTemplate } from './create-item'
import { homeTemplate } from './home'
import { listTemplate } from './list'
import { itemTemplate } from './item'
import { noAccessTemplate } from './no-access'
import { createItemTemplate } from './create-item'
import { listTemplate } from './list'
import { nextConfigTemplate } from './next-config'
import { noAccessTemplate } from './no-access'

const pkgDir = path.dirname(require.resolve('@keystone-6/core/package.json'))

Expand Down
33 changes: 14 additions & 19 deletions packages/core/src/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { ChildProcess } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path'
import type { ChildProcess } from 'node:child_process'

import { formatSchema, getGenerators } from '@prisma/internals'
import { printSchema } from 'graphql'
import { getGenerators, formatSchema } from '@prisma/internals'
import { ExitError } from './scripts/utils'
import { initialiseLists } from './lib/core/initialise-lists'
import { type System, getSystemPaths } from './lib/createSystem'
import { printPrismaSchema } from './lib/core/prisma-schema-printer'
import type { System } from './lib/system'
import { printGeneratedTypes } from './lib/typescript-schema-printer'

export function getFormattedGraphQLSchema(schema: string) {
Expand All @@ -30,32 +28,29 @@ async function readFileOrUndefined(path: string) {

export async function validateArtifacts(cwd: string, system: System) {
const paths = system.getPaths(cwd)
const artifacts = await getArtifacts(system)
const artifacts = await buildArtifacts(system)
const [writtenGraphQLSchema, writtenPrismaSchema] = await Promise.all([
readFileOrUndefined(paths.schema.graphql),
readFileOrUndefined(paths.schema.prisma),
])

if (writtenGraphQLSchema !== artifacts.graphql && writtenPrismaSchema !== artifacts.prisma) {
console.error('Your Prisma and GraphQL schemas are not up to date')
throw new ExitError(1)
throw new Error('Your Prisma and GraphQL schemas are not up to date')
}

if (writtenGraphQLSchema !== artifacts.graphql) {
console.error('Your GraphQL schema is not up to date')
throw new ExitError(1)
throw new Error('Your GraphQL schema is not up to date')
}

if (writtenPrismaSchema !== artifacts.prisma) {
console.error('Your Prisma schema is not up to date')
throw new ExitError(1)
throw new Error('Your Prisma schema is not up to date')
}
}

export async function getArtifacts(system: System) {
const lists = initialiseLists(system.config)
// exported for tests
export async function buildArtifacts(system: System) {
const prismaSchema = await formatSchema({
schemas: [[system.config.db.prismaSchemaPath, printPrismaSchema(system.config, lists)]],
schemas: [[system.config.db.prismaSchemaPath, printPrismaSchema(system.config, system.lists)]],
})

return {
Expand All @@ -65,15 +60,15 @@ export async function getArtifacts(system: System) {
}

export async function generateArtifacts(cwd: string, system: System) {
const paths = getSystemPaths(cwd, system.config)
const artifacts = await getArtifacts(system)
const paths = system.getPaths(cwd)
const artifacts = await buildArtifacts(system)
await fs.writeFile(paths.schema.graphql, artifacts.graphql)
await fs.writeFile(paths.schema.prisma, artifacts.prisma)
return artifacts
}

export async function generateTypes(cwd: string, system: System) {
const paths = getSystemPaths(cwd, system.config)
const paths = system.getPaths(cwd)
const schema = printGeneratedTypes(
paths.types.relativePrismaPath,
system.graphQLSchemaSudo,
Expand All @@ -84,7 +79,7 @@ export async function generateTypes(cwd: string, system: System) {
}

export async function generatePrismaClient(cwd: string, system: System) {
const paths = getSystemPaths(cwd, system.config)
const paths = system.getPaths(cwd)
const generators = await getGenerators({
schemaPath: paths.schema.prisma,
})
Expand Down
12 changes: 1 addition & 11 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
import type { BaseKeystoneTypeInfo, KeystoneConfig, KeystoneContext } from './types'
import { createSystem } from './lib/createSystem'

export function getContext<TypeInfo extends BaseKeystoneTypeInfo>(
config: KeystoneConfig<TypeInfo>,
PrismaModule: unknown
): KeystoneContext<TypeInfo> {
const system = createSystem(config)
const { context } = system.getKeystone(PrismaModule)
return context
}
export { getContext } from './lib/system'
5 changes: 1 addition & 4 deletions packages/core/src/fields/types/relationship/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { g } from '../../..'
import {
type ListMetaSource,
getAdminMetaForRelationshipField,
} from '../../../lib/create-admin-meta'
import { type ListMetaSource, getAdminMetaForRelationshipField } from '../../../lib/admin-meta'
import type { JSONValue } from '../../../types'
import {
type BaseListTypeInfo,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/lib/coerceAndValidateForGraphQLInput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { GraphQLSchema, VariableDefinitionNode, GraphQLInputType, GraphQLError } from 'graphql'
import type { GraphQLError, GraphQLInputType, GraphQLSchema, VariableDefinitionNode } from 'graphql'
import { Kind } from 'graphql'
import { getVariableValues } from 'graphql/execution/values'
import { getTypeNodeForType } from './context/executeGraphQLFieldToSource'

import { getTypeNodeForType } from './context/graphql'

const argName = 'where'

Expand Down
39 changes: 18 additions & 21 deletions packages/core/src/lib/context/api.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { type GraphQLSchema } from 'graphql'
import type { GraphQLSchema } from 'graphql'
import type { KeystoneContext } from '../../types'
import type { InitialisedList } from '../core/initialise-lists'
import { type KeystoneContext } from '../../types'
import { executeGraphQLFieldToSource } from './executeGraphQLFieldToSource'
import { executeGraphQLFieldWithSelection } from './executeGraphQLFieldWithSelection'
import { makeContextDbFn, makeContextQueryFn } from './graphql'

export function getQueryFactory(list: InitialisedList, schema: GraphQLSchema) {
const queryType = schema.getQueryType()!
const mutationType = schema.getMutationType()!

function f(operation: 'query' | 'mutation', fieldName: string) {
const exec = executeGraphQLFieldWithSelection(schema, operation, fieldName)
return (
_args: {
query?: string
} & Record<string, any> = {},
context: KeystoneContext
) => {
const { query, ...args } = _args
return exec(args, query ?? 'id', context) as Promise<any>
const rootType = operation === 'query' ? queryType : mutationType
const field = rootType.getFields()[fieldName]
if (field) return makeContextQueryFn(schema, operation, field)

// if omitted
return () => {
throw new Error(`This ${operation} is not supported by the GraphQL schema: ${fieldName}()`)
}
}

Expand Down Expand Up @@ -63,15 +63,12 @@ export function getDbFactory(list: InitialisedList, schema: GraphQLSchema) {
function f(operation: 'query' | 'mutation', fieldName: string) {
const rootType = operation === 'query' ? queryType : mutationType
const field = rootType.getFields()[fieldName]
if (field === undefined) {
return () => {
// This will be triggered if the field is missing due to `omit` configuration.
// The GraphQL equivalent would be a bad user input error.
throw new Error(`This ${operation} is not supported by the GraphQL schema: ${fieldName}()`)
}
}
if (field) return makeContextDbFn(field)

return executeGraphQLFieldToSource(field)
// if omitted
return () => {
throw new Error(`This ${operation} is not supported by the GraphQL schema: ${fieldName}()`)
}
}

const fcache = {
Expand Down
95 changes: 0 additions & 95 deletions packages/core/src/lib/context/executeGraphQLFieldWithSelection.ts

This file was deleted.

Loading