Skip to content

Commit

Permalink
Add config.experimental.contextInitialisedLists (#6403)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Aug 25, 2021
1 parent af71f07 commit 2a901a1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-ladybugs-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': minor
---

Added the experimental config option `config.experimental.contextInitialisedLists`, which adds the internal data structure `experimental.initialisedLists` to the `context` object. This is a temporary addition to the API which will be removed in a future release once a more controlled API is available. It should be used with caution, as it will contain breaking change in `patch` level releases.
7 changes: 7 additions & 0 deletions packages/keystone/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../types';

import { PrismaClient } from '../core/utils';
import { InitialisedList } from '../core/types-for-lists';
import { getDbAPIFactory, itemAPIForList } from './itemAPI';
import { createImagesContext } from './createImagesContext';
import { createFilesContext } from './createFilesContext';
Expand All @@ -19,12 +20,14 @@ export function makeCreateContext({
prismaClient,
gqlNamesByList,
config,
lists,
}: {
graphQLSchema: GraphQLSchema;
internalSchema: GraphQLSchema;
config: KeystoneConfig;
prismaClient: PrismaClient;
gqlNamesByList: Record<string, GqlNames>;
lists: Record<string, InitialisedList>;
}) {
const images = createImagesContext(config);
const files = createFilesContext(config);
Expand Down Expand Up @@ -99,6 +102,10 @@ export function makeCreateContext({
images,
files,
};
if (config.experimental?.contextInitialisedLists) {
contextToReturn.experimental = { initialisedLists: lists };
}

const dbAPIFactories = schemaName === 'public' ? publicDbApiFactories : internalDbApiFactories;
for (const listKey of Object.keys(gqlNamesByList)) {
dbAPI[listKey] = dbAPIFactories[listKey](contextToReturn);
Expand Down
1 change: 1 addition & 0 deletions packages/keystone/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function createSystem(config: KeystoneConfig) {
gqlNamesByList: Object.fromEntries(
Object.entries(lists).map(([listKey, list]) => [listKey, getGqlNames(list)])
),
lists,
});

return {
Expand Down
4 changes: 4 additions & 0 deletions packages/keystone/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export type KeystoneConfig = {
generateNextGraphqlAPI?: boolean;
/** Config options for Keystone Cloud */
keystoneCloud?: KeystoneCloudConfig;
/** Adds the internal data structure `experimental.initialisedLists` to the context object.
* This is not a stable API and may contain breaking changes in `patch` level releases.
*/
contextInitialisedLists?: boolean;
};
};

Expand Down
7 changes: 7 additions & 0 deletions packages/keystone/src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IncomingMessage } from 'http';
import { Readable } from 'stream';
import { GraphQLSchema, ExecutionResult, DocumentNode } from 'graphql';
import { InitialisedList } from '../lib/core/types-for-lists';
import type { BaseGeneratedListTypes, GqlNames } from './utils';

export type KeystoneContext = {
Expand All @@ -20,6 +21,12 @@ export type KeystoneContext = {
schemaName: 'public' | 'internal';
/** @deprecated */
gqlNames: (listKey: string) => GqlNames;
experimental?: {
/** @deprecated This value is only available if you have config.experimental.contextInitialisedLists = true.
* This is not a stable API and may contain breaking changes in `patch` level releases.
*/
initialisedLists: Record<string, InitialisedList>;
};
} & Partial<SessionContext<any>>;

// List item API
Expand Down

0 comments on commit 2a901a1

Please sign in to comment.