From f64d0fc606bacca61e6092008ab1a2f11bb0e1c1 Mon Sep 17 00:00:00 2001 From: Marek Rybczynski Date: Wed, 16 Aug 2023 20:55:27 +0900 Subject: [PATCH] add config.graphql.schemaPath --- .changeset/add-graphql-schema-path.md | 5 + docs/pages/docs/config/config.md | 1 + packages/core/src/artifacts.ts | 6 +- packages/core/src/types/config/index.ts | 6 + tests/cli-tests/artifacts.test.ts | 13 ++ .../different_schema.graphql | 209 ++++++++++++++++++ .../fixtures/custom-graphql-path/keystone.ts | 22 ++ .../custom-graphql-path/schema.prisma | 17 ++ .../custom-prisma-project/schema.graphql | 2 +- tests/cli-tests/utils.tsx | 5 + 10 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 .changeset/add-graphql-schema-path.md create mode 100644 tests/cli-tests/fixtures/custom-graphql-path/different_schema.graphql create mode 100644 tests/cli-tests/fixtures/custom-graphql-path/keystone.ts create mode 100644 tests/cli-tests/fixtures/custom-graphql-path/schema.prisma diff --git a/.changeset/add-graphql-schema-path.md b/.changeset/add-graphql-schema-path.md new file mode 100644 index 00000000000..a9b7e103b85 --- /dev/null +++ b/.changeset/add-graphql-schema-path.md @@ -0,0 +1,5 @@ +--- +'@keystone-6/core': minor +--- + +Adds `config.graphql.schemaPath` diff --git a/docs/pages/docs/config/config.md b/docs/pages/docs/config/config.md index 3366cf5050b..638443442c7 100644 --- a/docs/pages/docs/config/config.md +++ b/docs/pages/docs/config/config.md @@ -330,6 +330,7 @@ Options: - `false` - Add `ApolloServerPluginLandingPageDisabled` to the Apollo Server plugins - `'apollo'` - Do not add any plugins to the Apollo config, this will use [Apollo Sandbox](https://www.apollographql.com/docs/apollo-server/testing/build-run-queries/#apollo-sandbox) - `apolloConfig` (default: `undefined`): Allows you to pass [extra options](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructor) into the `ApolloServer` constructor. +- `schemaPath` (default: `schema.graphql`): The path of the generated GraphQL API schema. ```typescript export default config({ diff --git a/packages/core/src/artifacts.ts b/packages/core/src/artifacts.ts index 07b03345ddf..3277c678961 100644 --- a/packages/core/src/artifacts.ts +++ b/packages/core/src/artifacts.ts @@ -100,6 +100,10 @@ export function getSystemPaths(cwd: string, config: KeystoneConfig) { ? `./${posixify(path.relative(path.dirname(builtTypesPath), prismaClientPath))}` : '@prisma/client'; + const graphqlSchemaPath = config.graphql?.schemaPath + ? path.join(cwd, config.graphql.schemaPath) + : path.join(cwd, 'schema.graphql'); + return { config: getBuiltKeystoneConfigurationPath(cwd), admin: path.join(cwd, '.keystone/admin'), @@ -110,7 +114,7 @@ export function getSystemPaths(cwd: string, config: KeystoneConfig) { schema: { types: builtTypesPath, prisma: path.join(cwd, 'schema.prisma'), - graphql: path.join(cwd, 'schema.graphql'), + graphql: graphqlSchemaPath, }, }; } diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index 6c3c5a7ebac..f4ac924a659 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -282,6 +282,12 @@ export type GraphQLConfig { @@ -96,4 +97,16 @@ describe('postinstall', () => { expect(await getFiles(tmp, ['node_modules/.keystone/**/*'])).toMatchSnapshot(); expect(recording()).toMatchInlineSnapshot(`"? GraphQL and Prisma schemas are up to date"`); }); + test('customising graphQL schema through schemaPath works', async () => { + const tmp = await testdir({ + ...symlinkKeystoneDeps, + 'keystone.js': customGraphqlPathKeystoneConfig, + }); + const recording = recordConsole(); + await runCommand(tmp, ['postinstall', '--fix']); + const schemas = ['schema.prisma', 'different_schema.graphql']; + const files = await getFiles(tmp, schemas); + expect(files).toEqual(await getFiles(`${__dirname}/fixtures/custom-graphql-path`, schemas)); + expect(recording()).toMatchInlineSnapshot(`"? Generated GraphQL and Prisma schemas"`); + }); }); diff --git a/tests/cli-tests/fixtures/custom-graphql-path/different_schema.graphql b/tests/cli-tests/fixtures/custom-graphql-path/different_schema.graphql new file mode 100644 index 00000000000..4d83de34c37 --- /dev/null +++ b/tests/cli-tests/fixtures/custom-graphql-path/different_schema.graphql @@ -0,0 +1,209 @@ +# This file is automatically generated by Keystone, do not modify it manually. +# Modify your Keystone config when you want to change this. + +type Todo { + id: ID! + title: String +} + +input TodoWhereUniqueInput { + id: ID +} + +input TodoWhereInput { + AND: [TodoWhereInput!] + OR: [TodoWhereInput!] + NOT: [TodoWhereInput!] + id: IDFilter + title: StringFilter +} + +input IDFilter { + equals: ID + in: [ID!] + notIn: [ID!] + lt: ID + lte: ID + gt: ID + gte: ID + not: IDFilter +} + +input StringFilter { + equals: String + in: [String!] + notIn: [String!] + lt: String + lte: String + gt: String + gte: String + contains: String + startsWith: String + endsWith: String + not: NestedStringFilter +} + +input NestedStringFilter { + equals: String + in: [String!] + notIn: [String!] + lt: String + lte: String + gt: String + gte: String + contains: String + startsWith: String + endsWith: String + not: NestedStringFilter +} + +input TodoOrderByInput { + id: OrderDirection + title: OrderDirection +} + +enum OrderDirection { + asc + desc +} + +input TodoUpdateInput { + title: String +} + +input TodoUpdateArgs { + where: TodoWhereUniqueInput! + data: TodoUpdateInput! +} + +input TodoCreateInput { + title: String +} + +""" +The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). +""" +scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf") + +type Mutation { + createTodo(data: TodoCreateInput!): Todo + createTodos(data: [TodoCreateInput!]!): [Todo] + updateTodo(where: TodoWhereUniqueInput!, data: TodoUpdateInput!): Todo + updateTodos(data: [TodoUpdateArgs!]!): [Todo] + deleteTodo(where: TodoWhereUniqueInput!): Todo + deleteTodos(where: [TodoWhereUniqueInput!]!): [Todo] +} + +type Query { + todos(where: TodoWhereInput! = {}, orderBy: [TodoOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: TodoWhereUniqueInput): [Todo!] + todo(where: TodoWhereUniqueInput!): Todo + todosCount(where: TodoWhereInput! = {}): Int + keystone: KeystoneMeta! +} + +type KeystoneMeta { + adminMeta: KeystoneAdminMeta! +} + +type KeystoneAdminMeta { + lists: [KeystoneAdminUIListMeta!]! + list(key: String!): KeystoneAdminUIListMeta +} + +type KeystoneAdminUIListMeta { + key: String! + itemQueryName: String! + listQueryName: String! + hideCreate: Boolean! + hideDelete: Boolean! + path: String! + label: String! + singular: String! + plural: String! + description: String + initialColumns: [String!]! + pageSize: Int! + labelField: String! + fields: [KeystoneAdminUIFieldMeta!]! + groups: [KeystoneAdminUIFieldGroupMeta!]! + initialSort: KeystoneAdminUISort + isHidden: Boolean! + isSingleton: Boolean! +} + +type KeystoneAdminUIFieldMeta { + path: String! + label: String! + description: String + isOrderable: Boolean! + isFilterable: Boolean! + isNonNull: [KeystoneAdminUIFieldMetaIsNonNull!] + fieldMeta: JSON + viewsIndex: Int! + customViewsIndex: Int + createView: KeystoneAdminUIFieldMetaCreateView! + listView: KeystoneAdminUIFieldMetaListView! + itemView(id: ID): KeystoneAdminUIFieldMetaItemView + search: QueryMode +} + +enum KeystoneAdminUIFieldMetaIsNonNull { + read + create + update +} + +type KeystoneAdminUIFieldMetaCreateView { + fieldMode: KeystoneAdminUIFieldMetaCreateViewFieldMode! +} + +enum KeystoneAdminUIFieldMetaCreateViewFieldMode { + edit + hidden +} + +type KeystoneAdminUIFieldMetaListView { + fieldMode: KeystoneAdminUIFieldMetaListViewFieldMode! +} + +enum KeystoneAdminUIFieldMetaListViewFieldMode { + read + hidden +} + +type KeystoneAdminUIFieldMetaItemView { + fieldMode: KeystoneAdminUIFieldMetaItemViewFieldMode + fieldPosition: KeystoneAdminUIFieldMetaItemViewFieldPosition +} + +enum KeystoneAdminUIFieldMetaItemViewFieldMode { + edit + read + hidden +} + +enum KeystoneAdminUIFieldMetaItemViewFieldPosition { + form + sidebar +} + +enum QueryMode { + default + insensitive +} + +type KeystoneAdminUIFieldGroupMeta { + label: String! + description: String + fields: [KeystoneAdminUIFieldMeta!]! +} + +type KeystoneAdminUISort { + field: String! + direction: KeystoneAdminUISortDirection! +} + +enum KeystoneAdminUISortDirection { + ASC + DESC +} diff --git a/tests/cli-tests/fixtures/custom-graphql-path/keystone.ts b/tests/cli-tests/fixtures/custom-graphql-path/keystone.ts new file mode 100644 index 00000000000..649e255f4eb --- /dev/null +++ b/tests/cli-tests/fixtures/custom-graphql-path/keystone.ts @@ -0,0 +1,22 @@ +import { list, config } from '@keystone-6/core'; +import { allowAll } from '@keystone-6/core/access'; +import { text } from '@keystone-6/core/fields'; + +export default config({ + db: { + provider: 'sqlite', + url: 'file:./app.db', + }, + ui: { isDisabled: true }, + lists: { + Todo: list({ + access: allowAll, + fields: { + title: text(), + }, + }), + }, + graphql: { + schemaPath: 'different_schema.graphql', + }, +}); diff --git a/tests/cli-tests/fixtures/custom-graphql-path/schema.prisma b/tests/cli-tests/fixtures/custom-graphql-path/schema.prisma new file mode 100644 index 00000000000..c562497cf5c --- /dev/null +++ b/tests/cli-tests/fixtures/custom-graphql-path/schema.prisma @@ -0,0 +1,17 @@ +// This file is automatically generated by Keystone, do not modify it manually. +// Modify your Keystone config when you want to change this. + +datasource sqlite { + url = env("DATABASE_URL") + shadowDatabaseUrl = env("SHADOW_DATABASE_URL") + provider = "sqlite" +} + +generator client { + provider = "prisma-client-js" +} + +model Todo { + id String @id @default(cuid()) + title String @default("") +} diff --git a/tests/cli-tests/fixtures/custom-prisma-project/schema.graphql b/tests/cli-tests/fixtures/custom-prisma-project/schema.graphql index 2967440a834..51a4bdd115c 100644 --- a/tests/cli-tests/fixtures/custom-prisma-project/schema.graphql +++ b/tests/cli-tests/fixtures/custom-prisma-project/schema.graphql @@ -95,7 +95,7 @@ type Mutation { } type Query { - todos(where: TodoWhereInput! = {}, orderBy: [TodoOrderByInput!]! = [], take: Int, skip: Int! = 0): [Todo!] + todos(where: TodoWhereInput! = {}, orderBy: [TodoOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: TodoWhereUniqueInput): [Todo!] todo(where: TodoWhereUniqueInput!): Todo todosCount(where: TodoWhereInput! = {}): Int keystone: KeystoneMeta! diff --git a/tests/cli-tests/utils.tsx b/tests/cli-tests/utils.tsx index eb481b9d0ce..cd9d7ed1ce6 100644 --- a/tests/cli-tests/utils.tsx +++ b/tests/cli-tests/utils.tsx @@ -40,6 +40,11 @@ export const customPrismaKeystoneConfig = fs.readFileSync( 'utf8' ); +export const customGraphqlPathKeystoneConfig = fs.readFileSync( + `${__dirname}/fixtures/custom-graphql-path/keystone.ts`, + 'utf8' +); + export function recordConsole() { let oldConsole = { ...console }; const contents: string[] = [];