Skip to content

Commit

Permalink
Add graphql.excludeKeystoneMeta configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
marekryb authored and dcousens committed Aug 21, 2023
1 parent 294a96d commit 3302f98
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 125 deletions.
1 change: 0 additions & 1 deletion docs/pages/docs/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ Options:
- `'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.
- `excludeKeystoneMeta` (default: `undefined`): Allows you to exclude `keystone` query used by the Admin UI from the GraphQL API. If set to `true`, Admin UI will not be operational, so use it only in instances where you do not need it (when setting ui.isDisabled and/or using keystone in custom setup like `examples/framework-nextjs-*-directory`).

```typescript
export default config({
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/lib/createGraphQLSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ export function createGraphQLSchema(
}),
}
: {},
query: adminMeta
? {
keystone: graphql.field({
type: graphql.nonNull(KeystoneMeta),
resolve: () => ({ adminMeta }),
}),
}
: {},
query:
adminMeta && !config.ui?.isDisabled
? {
keystone: graphql.field({
type: graphql.nonNull(KeystoneMeta),
resolve: () => ({ adminMeta }),
}),
}
: {},
},
sudo
);
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ export type GraphQLConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneTy
*/
debug?: boolean;

// The path to GraphQL schema. Default: 'schema.graphql'.
/**
* The path to GraphQL schema
* @default 'schema.graphql'
*/
schemaPath?: string;
};

Expand Down
14 changes: 13 additions & 1 deletion tests/cli-tests/artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
symlinkKeystoneDeps,
testdir,
customGraphqlPathKeystoneConfig,
customUiDiabledKeystoneConfig,
} from './utils';

describe.each(['postinstall', ['build', '--frozen']])('%s', command => {
Expand Down Expand Up @@ -104,9 +105,20 @@ describe('postinstall', () => {
});
const recording = recordConsole();
await runCommand(tmp, ['postinstall', '--fix']);
const schemas = ['schema.prisma', 'different_schema.graphql'];
const schemas = ['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"`);
});
test('disabling ui makes keystone query and KeystoneAdmin types disappear from schema', async () => {
const tmp = await testdir({
...symlinkKeystoneDeps,
'keystone.js': customUiDiabledKeystoneConfig,
});
const recording = recordConsole();
await runCommand(tmp, ['postinstall', '--fix']);
const files = await getFiles(tmp, schemasMatch);
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/custom-ui-disabled`, schemasMatch));
expect(recording()).toMatchInlineSnapshot(`"? Generated GraphQL and Prisma schemas"`);
});
});
1 change: 0 additions & 1 deletion tests/cli-tests/fixtures/basic-project/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default config({
provider: 'sqlite',
url: 'file:./app.db',
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
Expand Down
108 changes: 0 additions & 108 deletions tests/cli-tests/fixtures/custom-graphql-path/different_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -98,112 +98,4 @@ 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
}
1 change: 0 additions & 1 deletion tests/cli-tests/fixtures/custom-prisma-project/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default config({
.replace(/(\ngenerator[^\n]+\{[^\}]+)\}/, '$1 previewFeatures = ["multiSchema"]\n}')
.replace(/(datasource[^\n]+\{[^\}]+)\}/, '$1 schemas = ["first", "second"]\n}'),
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ export default config({
},
}),
},
graphql: {
excludeKeystoneMeta: true,
},
});
5 changes: 5 additions & 0 deletions tests/cli-tests/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const customGraphqlPathKeystoneConfig = fs.readFileSync(
'utf8'
);

export const customUiDiabledKeystoneConfig = fs.readFileSync(
`${__dirname}/fixtures/custom-ui-disabled/keystone.ts`,
'utf8'
);

export function recordConsole() {
let oldConsole = { ...console };
const contents: string[] = [];
Expand Down

0 comments on commit 3302f98

Please sign in to comment.