-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtypes.ts
254 lines (212 loc) · 7.44 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { Prisma, PrismaClient } from '@prisma/client'
import type {
AppSyncIdentity,
AppSyncIdentityCognito,
AppSyncIdentityIAM,
AppSyncIdentityLambda,
AppSyncIdentityOIDC,
AppSyncResolverEvent,
AppSyncResolverHandler,
} from 'aws-lambda'
import type { Actions, ActionsAliases, Authorizations } from './consts'
export type logLevel = 'INFO' | 'WARN' | 'ERROR'
export type PrismaAppSyncOptionsType = {
connectionString?: string
sanitize?: boolean
logLevel?: logLevel
defaultPagination?: number | false
maxDepth?: number
maxReqPerUserMinute?: number | false
}
export type Options = Required<PrismaAppSyncOptionsType> & {
modelsMapping: any
fieldsMapping: any
}
export type InjectedConfig = {
modelsMapping?: { [modelVariant: string]: { prismaRef: string; singular: string; plural: string } }
fieldsMapping?: { [fieldPath: string]: { type: string; isRelation: boolean } }
operations?: string
}
export type RuntimeConfig = {
modelsMapping: { [modelVariant: string]: { prismaRef: string; singular: string; plural: string } }
fieldsMapping: { [fieldPath: string]: { type: string; isRelation: boolean } }
operations: string[]
}
export type Action = typeof Actions[keyof typeof Actions] | string
export type ActionsAlias = typeof ActionsAliases[keyof typeof ActionsAliases] | 'custom' | null
export type ActionsAliasStr = keyof typeof ActionsAliases
export type Context = {
action: Action
alias: ActionsAlias
model: Model
}
export type Model = { prismaRef: string; singular: string; plural: string } | null
export type { AppSyncResolverHandler, AppSyncResolverEvent, AppSyncIdentity }
/**
* ### QueryParams
*
* @example
* ```
* {
* type: 'Query',
* operation: 'getPost',
* context: { action: 'get', alias: 'access', model: 'Post' },
* fields: ['title', 'status'],
* paths: ['get/post/title', 'get/post/status'],
* args: { where: { id: 5 } },
* prismaArgs: {
* where: { id: 5 },
* select: { title: true, status: true },
* },
* authorization: 'API_KEY',
* identity: { ... },
* }
* ```
*/
export type QueryParams<T = any> = {
type: GraphQLType
operation: string
context: Context
fields: string[]
paths: string[]
args: T
prismaArgs: PrismaArgs
authorization: Authorization
identity: Identity
headers: any
}
export type Authorization = typeof Authorizations[keyof typeof Authorizations] | null
export type PrismaGet = Pick<Required<PrismaArgs>, 'where'> & Pick<PrismaArgs, 'select'>
export type PrismaList = Pick<PrismaArgs, 'where' | 'orderBy' | 'select' | 'skip' | 'take'>
export type PrismaCount = Pick<PrismaArgs, 'where' | 'orderBy' | 'select' | 'skip' | 'take'>
export type PrismaCreate = Pick<Required<PrismaArgs>, 'data'> & Pick<PrismaArgs, 'select'>
export type PrismaCreateMany = Pick<Required<PrismaArgs>, 'data'> & Pick<PrismaArgs, 'skipDuplicates'>
export type PrismaUpdate = Pick<Required<PrismaArgs>, 'data' | 'where'> & Pick<PrismaArgs, 'select'>
export type PrismaUpdateMany = Pick<Required<PrismaArgs>, 'data' | 'where'>
export type PrismaUpsert = Pick<Required<PrismaArgs>, 'where'> &
Pick<PrismaArgs, 'select'> & Pick<PrismaArgs, 'update'> & Pick<PrismaArgs, 'create'>
export type PrismaDelete = Pick<Required<PrismaArgs>, 'where'> & Pick<PrismaArgs, 'select'>
export type PrismaDeleteMany = Pick<Required<PrismaArgs>, 'where'>
export type QueryBuilder = {
prismaGet: (...prismaArgs: PrismaArgs[]) => PrismaGet
prismaList: (...prismaArgs: PrismaArgs[]) => PrismaList
prismaCount: (...prismaArgs: PrismaArgs[]) => PrismaCount
prismaCreate: (...prismaArgs: PrismaArgs[]) => PrismaCreate
prismaCreateMany: (...prismaArgs: PrismaArgs[]) => PrismaCreateMany
prismaUpdate: (...prismaArgs: PrismaArgs[]) => PrismaUpdate
prismaUpdateMany: (...prismaArgs: PrismaArgs[]) => PrismaUpdateMany
prismaUpsert: (...prismaArgs: PrismaArgs[]) => PrismaUpsert
prismaDelete: (...prismaArgs: PrismaArgs[]) => PrismaDelete
prismaDeleteMany: (...prismaArgs: PrismaArgs[]) => PrismaDeleteMany
}
export type QueryParamsCustom<T = any> = QueryParams<T> & {
prismaClient: PrismaClient
}
export type BeforeHookParams = QueryParams & {
prismaClient: PrismaClient
}
/**
* ### AfterHookParams
*
* @example
* ```
* {
* type: 'Query',
* operation: 'getPost',
* context: { action: 'get', alias: 'access', model: 'Post' },
* fields: ['title', 'status'],
* paths: ['get/post/title', 'get/post/status'],
* args: { where: { id: 5 } },
* prismaArgs: {
* where: { id: 5 },
* select: { title: true, status: true },
* },
* authorization: 'API_KEY',
* identity: { ... },
* result: { title: 'Hello World', status: 'PUBLISHED' }
* }
* ```
*/
export type AfterHookParams = QueryParams & {
prismaClient: PrismaClient
result: any | any[]
}
export type ShieldContext = {
action: Action
model: string
}
export type Reason = string | ((context: ShieldContext) => string)
export type ShieldRule = boolean | ((context: ShieldContext) => boolean | Promise<boolean>) | any
export type Shield = {
[matcher: string]:
| boolean
| {
rule: ShieldRule
reason?: Reason
}
}
export type HooksProps = {
before: BeforeHookParams
after: AfterHookParams
}
export type HooksReturn = {
before: Promise<BeforeHookParams>
after: Promise<AfterHookParams>
}
export type HookPath<Operations extends string, CustomResolvers> = Operations | CustomResolvers
export type HooksParameter<
HookType extends 'before' | 'after',
Operations extends string,
CustomResolvers extends string,
> = `${HookType}:${HookPath<Operations, CustomResolvers>}` | `${HookType}:**`
export type HooksParameters<
HookType extends 'before' | 'after',
Operations extends string,
CustomResolvers extends string,
> = {
[matcher in HooksParameter<HookType, Operations, CustomResolvers>]?: (
props: HooksProps[HookType],
) => HooksReturn[HookType]
}
export type Hooks<Operations extends string, CustomResolvers extends string> =
| HooksParameters<'before', Operations, CustomResolvers>
| HooksParameters<'after', Operations, CustomResolvers>
export type ShieldAuthorization = {
canAccess: boolean
reason: Reason
prismaFilter: any
matcher: string
globPattern: string
}
export type ResolveParams<Operations extends string, CustomResolvers extends string> = {
event: AppSyncEvent
resolvers?: {
[resolver in CustomResolvers]: ((props: QueryParamsCustom) => Promise<any>) | boolean
}
shield?: (props: QueryParams) => Shield
hooks?: Hooks<Operations, CustomResolvers>
}
// Prisma-related Types
export { PrismaClient, Prisma }
export type PrismaArgs = {
where?: any
create?: any
update?: any
data?: any
select?: any
orderBy?: any
skip?: number | undefined
take?: number | undefined
skipDuplicates?: boolean | undefined
}
export type PrismaOperator = keyof Required<PrismaArgs>
export type AppSyncEvent = AppSyncResolverEvent<any>
export type GraphQLType = 'Query' | 'Mutation' | 'Subscription'
export type API_KEY = null | {
[key: string]: any
}
export type AWS_LAMBDA = AppSyncIdentityLambda
export type AWS_IAM = AppSyncIdentityIAM
export type AMAZON_COGNITO_USER_POOLS = AppSyncIdentityCognito
export type OPENID_CONNECT = AppSyncIdentityOIDC
export type Identity = API_KEY | AWS_LAMBDA | AWS_IAM | AMAZON_COGNITO_USER_POOLS | OPENID_CONNECT