-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor KeystoneContext definition for docs (#4942)
- Loading branch information
Showing
5 changed files
with
126 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@keystone-next/types': patch | ||
--- | ||
|
||
Refactored `KeystoneContext` definition to make documentation easier. No functional changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { IncomingMessage } from 'http'; | ||
import { GraphQLSchema, ExecutionResult, DocumentNode } from 'graphql'; | ||
import { BaseKeystone } from './base'; | ||
import type { BaseGeneratedListTypes } from './utils'; | ||
|
||
export type KeystoneContext = { | ||
req?: IncomingMessage; | ||
lists: KeystoneListsAPI<any>; | ||
graphql: KeystoneGraphQLAPI<any>; | ||
sudo: () => KeystoneContext; | ||
exitSudo: () => KeystoneContext; | ||
withSession: (session: any) => KeystoneContext; | ||
totalResults: number; | ||
maxTotalResults: number; | ||
schemaName: 'public'; | ||
/** @deprecated */ | ||
gqlNames: (listKey: string) => Record<string, string>; // TODO: actual keys | ||
/** @deprecated */ | ||
executeGraphQL: any; // TODO: type this | ||
keystone: BaseKeystone; | ||
} & AccessControlContext & | ||
Partial<SessionContext<any>> & | ||
DatabaseAPIs; | ||
|
||
// List item API | ||
|
||
export type KeystoneListsAPI< | ||
KeystoneListsTypeInfo extends Record<string, BaseGeneratedListTypes> | ||
> = { | ||
[Key in keyof KeystoneListsTypeInfo]: { | ||
findMany( | ||
args: KeystoneListsTypeInfo[Key]['args']['listQuery'] & ResolveFields | ||
): Promise<readonly KeystoneListsTypeInfo[Key]['backing'][]>; | ||
findOne( | ||
args: { readonly where: { readonly id: string } } & ResolveFields | ||
): Promise<KeystoneListsTypeInfo[Key]['backing'] | null>; | ||
count(args: KeystoneListsTypeInfo[Key]['args']['listQuery']): Promise<number>; | ||
updateOne( | ||
args: { | ||
readonly id: string; | ||
readonly data: KeystoneListsTypeInfo[Key]['inputs']['update']; | ||
} & ResolveFields | ||
): Promise<KeystoneListsTypeInfo[Key]['backing'] | null>; | ||
updateMany( | ||
args: { | ||
readonly data: readonly { | ||
readonly id: string; | ||
readonly data: KeystoneListsTypeInfo[Key]['inputs']['update']; | ||
}[]; | ||
} & ResolveFields | ||
): Promise<(KeystoneListsTypeInfo[Key]['backing'] | null)[] | null>; | ||
createOne( | ||
args: { readonly data: KeystoneListsTypeInfo[Key]['inputs']['create'] } & ResolveFields | ||
): Promise<KeystoneListsTypeInfo[Key]['backing'] | null>; | ||
createMany( | ||
args: { | ||
readonly data: readonly { readonly data: KeystoneListsTypeInfo[Key]['inputs']['update'] }[]; | ||
} & ResolveFields | ||
): Promise<(KeystoneListsTypeInfo[Key]['backing'] | null)[] | null>; | ||
deleteOne( | ||
args: { readonly id: string } & ResolveFields | ||
): Promise<KeystoneListsTypeInfo[Key]['backing'] | null>; | ||
deleteMany( | ||
args: { readonly ids: readonly string[] } & ResolveFields | ||
): Promise<(KeystoneListsTypeInfo[Key]['backing'] | null)[] | null>; | ||
}; | ||
}; | ||
|
||
type ResolveFields = { readonly resolveFields?: false | string }; | ||
|
||
// GraphQL API | ||
|
||
export type KeystoneGraphQLAPI< | ||
// this is here because it will be used soon | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
KeystoneListsTypeInfo extends Record<string, BaseGeneratedListTypes> | ||
> = { | ||
schema: GraphQLSchema; | ||
run: (args: GraphQLExecutionArguments) => Promise<Record<string, any>>; | ||
raw: (args: GraphQLExecutionArguments) => Promise<ExecutionResult>; | ||
}; | ||
|
||
type GraphQLExecutionArguments = { | ||
context?: any; | ||
query: string | DocumentNode; | ||
variables: Record<string, any>; | ||
}; | ||
|
||
// Access control API | ||
|
||
export type AccessControlContext = { | ||
getListAccessControlForUser: any; // TODO | ||
getFieldAccessControlForUser: any; // TODO | ||
}; | ||
|
||
// Session API | ||
|
||
export type SessionContext<T> = { | ||
// Note: session is typed like this to acknowledge the default session shape | ||
// if you're using keystone's built-in session implementation, but we don't | ||
// actually know what it will look like. | ||
session?: { itemId: string; listKey: string; data?: Record<string, any> } | any; | ||
startSession(data: T): Promise<string>; | ||
endSession(): Promise<void>; | ||
}; | ||
|
||
// DatabaseAPIs is used to provide access to the underlying database abstraction through | ||
// context and other developer-facing APIs in Keystone, so they can be used easily. | ||
|
||
// The implementation is very basic, and assumes there's a single adapter keyed by the constructor | ||
// name. Since there's no option _not_ to do that using the new config, we probably don't need | ||
// anything more sophisticated than this. | ||
export type DatabaseAPIs = { | ||
knex?: any; | ||
mongoose?: any; | ||
prisma?: any; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6c949db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: