Skip to content

Commit

Permalink
prefer type unknown to any
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Mar 6, 2024
1 parent 993e08e commit c7aecb4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
print
} from 'graphql'
import {
type KeystoneConfig,
type KeystoneContext,
type KeystoneGraphQLAPI,
type KeystoneConfig
} from '../../types'

import { type InitialisedList } from '../core/initialise-lists'
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/context/executeGraphQLFieldToRootVal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function getRootValGivenOutputType (originalType: OutputType, value: any): any {
return value[rawField]
}

export function executeGraphQLFieldToRootVal (field: GraphQLField<any, any>) {
export function executeGraphQLFieldToRootVal (field: GraphQLField<any, unknown>) {
const { argumentNodes, variableDefinitions } = getVariablesForGraphQLField(field)
const document: DocumentNode = {
kind: Kind.DOCUMENT,
Expand Down Expand Up @@ -174,7 +174,7 @@ export function executeGraphQLFieldToRootVal (field: GraphQLField<any, any>) {

const type = getTypeForField(field.type)

const fieldConfig: RequiredButStillAllowUndefined<GraphQLFieldConfig<any, any>> = {
const fieldConfig: RequiredButStillAllowUndefined<GraphQLFieldConfig<unknown, unknown>> = {
args: argsToArgsConfig(field.args),
astNode: undefined,
deprecationReason: field.deprecationReason,
Expand All @@ -195,7 +195,7 @@ export function executeGraphQLFieldToRootVal (field: GraphQLField<any, any>) {
})

return async (
args: Record<string, any>,
args: Record<string, unknown>,
context: KeystoneContext,
rootValue: Record<string, string> = {}
) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/lib/core/mutations/access-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import type { InitialisedList } from '../initialise-lists'
import { cannotForItem, cannotForItemFields } from '../access-control'
import {
type InputFilter,
type UniqueInputFilter,
resolveUniqueWhereInput,
resolveWhereInput,
type UniqueInputFilter,
type UniquePrismaFilter,
} from '../where-inputs'
import {
type UniquePrismaFilter,
} from '../../../types/prisma'

async function getFilteredItem (
list: InitialisedList,
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/lib/core/queries/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import {
} from '../../../types'
import { getOperationAccess, getAccessFilters } from '../access-control'
import {
type PrismaFilter,
type UniquePrismaFilter,
type UniqueInputFilter,
type InputFilter,
resolveUniqueWhereInput,
resolveWhereInput,
} from '../where-inputs'
import {
type PrismaFilter,
type UniquePrismaFilter,
} from '../../../types/prisma'

import { limitsExceededError, userInputError } from '../graphql-errors'
import { type InitialisedList } from '../initialise-lists'
import { getDBFieldKeyForFieldOnMultiField } from '../utils'
Expand Down Expand Up @@ -202,7 +205,7 @@ async function resolveOrderBy (
}

export async function count (
{ where }: { where: Record<string, any> },
{ where }: { where: Record<string, unknown> },
list: InitialisedList,
context: KeystoneContext,
info: GraphQLResolveInfo,
Expand Down
71 changes: 1 addition & 70 deletions packages/core/src/lib/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,7 @@
import pluralize from 'pluralize'
import type { BaseItem, KeystoneConfig } from '../../types'
import { type KeystoneConfig } from '../../types'
import { getGqlNames } from '../../types/utils'
import { humanize } from '../utils'
import type { PrismaFilter, UniquePrismaFilter } from './where-inputs'

declare const prisma: unique symbol

// note prisma "promises" aren't really Promises, they have `then`, `catch` and `finally` but they don't start executation immediately
// so if you don't call .then/catch/finally/use it in $transaction, the operation will never happen
export type PrismaPromise<T> = Promise<T> & { [prisma]: true }

type PrismaModel = {
count: (arg: {
where?: PrismaFilter
take?: number
skip?: number
// this is technically wrong because relation orderBy but we're not doing that yet so it's fine
orderBy?: readonly Record<string, 'asc' | 'desc'>[]
}) => PrismaPromise<number>
findMany: (arg: {
where?: PrismaFilter
take?: number
skip?: number
cursor?: UniquePrismaFilter
// this is technically wrong because relation orderBy but we're not doing that yet so it's fine
orderBy?: readonly Record<string, 'asc' | 'desc'>[]
include?: Record<string, boolean>
select?: Record<string, any>
}) => PrismaPromise<BaseItem[]>
delete: (arg: { where: UniquePrismaFilter }) => PrismaPromise<BaseItem>
deleteMany: (arg: { where: PrismaFilter }) => PrismaPromise<BaseItem>
findUnique: (args: {
where: UniquePrismaFilter
include?: Record<string, any>
select?: Record<string, any>
}) => PrismaPromise<BaseItem | null>
findFirst: (args: {
where: PrismaFilter
include?: Record<string, any>
select?: Record<string, any>
}) => PrismaPromise<BaseItem | null>
create: (args: {
data: Record<string, any>
include?: Record<string, any>
select?: Record<string, any>
}) => PrismaPromise<BaseItem>
update: (args: {
where: UniquePrismaFilter
data: Record<string, any>
include?: Record<string, any>
select?: Record<string, any>
}) => PrismaPromise<BaseItem>
}

export type UnwrapPromise<TPromise extends Promise<any>> = TPromise extends Promise<infer T>
? T
: never

export type UnwrapPromises<T extends Promise<any>[]> = {
// unsure about this conditional
[Key in keyof T]: Key extends number ? UnwrapPromise<T[Key]> : never;
}

// please do not make this type be the value of KeystoneContext['prisma']
// this type is meant for generic usage, KeystoneContext should be generic over a PrismaClient
// and we should generate a KeystoneContext type in node_modules/.keystone/types which passes in the user's PrismaClient type
// so that users get right PrismaClient types specifically for their project
export type PrismaClient = {
$disconnect(): Promise<void>
$connect(): Promise<void>
$transaction<T extends PrismaPromise<any>[]>(promises: [...T]): UnwrapPromises<T>
} & Record<string, PrismaModel>

// this is wrong
// all the things should be generic over the id type
Expand Down
26 changes: 9 additions & 17 deletions packages/core/src/lib/core/where-inputs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { DBField, KeystoneContext } from '../../types'
import {
type DBField,
type KeystoneContext,
} from '../../types'
import {
type PrismaFilter,
type UniquePrismaFilter,
} from '../../types/prisma'
import { userInputError } from './graphql-errors'
import type { InitialisedList } from './initialise-lists'
import { type InitialisedList } from './initialise-lists'
import { getDBFieldKeyForFieldOnMultiField } from './utils'

export type InputFilter = Record<string, any> & {
Expand All @@ -9,23 +16,8 @@ export type InputFilter = Record<string, any> & {
OR?: InputFilter[]
NOT?: InputFilter[]
}
export type PrismaFilter = Record<string, any> & {
_____?: 'prisma filter'
AND?: PrismaFilter[] | PrismaFilter
OR?: PrismaFilter[] | PrismaFilter
NOT?: PrismaFilter[] | PrismaFilter
// just so that if you pass an array to something expecting a PrismaFilter, you get an error
length?: undefined
// so that if you pass a promise, you get an error
then?: undefined
}

export type UniqueInputFilter = Record<string, any> & { _____?: 'unique input filter' }
export type UniquePrismaFilter = Record<string, any> & {
_____?: 'unique prisma filter'
// so that if you pass a promise, you get an error
then?: undefined
}

export async function resolveUniqueWhereInput (
inputFilter: UniqueInputFilter,
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/types/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type PrismaFilter = Record<string, any> & {
_____?: 'prisma filter'
AND?: PrismaFilter[] | PrismaFilter
OR?: PrismaFilter[] | PrismaFilter
NOT?: PrismaFilter[] | PrismaFilter
// just so that if you pass an array to something expecting a PrismaFilter, you get an error
length?: undefined
// so that if you pass a promise, you get an error
then?: undefined
}

export type UniquePrismaFilter = Record<string, any> & {
_____?: 'unique prisma filter'
// so that if you pass a promise, you get an error
then?: undefined
}
4 changes: 3 additions & 1 deletion packages/core/src/types/type-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { type KeystoneContext } from './context'
import {
type KeystoneContext
} from './context'
import { type BaseItem } from './next-fields'

type GraphQLInput = Record<string, any>
Expand Down

0 comments on commit c7aecb4

Please sign in to comment.