-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathcheck.ts
59 lines (49 loc) · 1.68 KB
/
check.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
import {Model} from "@subsquid/openreader/lib/model"
import {assertNotNull} from "@subsquid/util-internal"
import {PluginDefinition} from "apollo-server-core"
import {GraphQLSchema, OperationDefinitionNode} from "graphql"
export interface HttpHeaders extends Iterable<[string, string]> {
get(name: string): string | null
has(name: string): boolean
entries(): Iterator<[string, string]>
keys(): Iterator<string>
}
export interface HttpRequest {
readonly url: string
readonly method: string
readonly headers: HttpHeaders
}
export interface RequestCheckContext {
http: HttpRequest
operation: OperationDefinitionNode
operationName: string | null
schema: GraphQLSchema
model: Model
}
export interface RequestCheckFunction {
(req: RequestCheckContext): Promise<boolean | string>
}
export function createCheckPlugin(requestCheck: RequestCheckFunction, model: Model): PluginDefinition {
return {
async requestDidStart() {
return {
async responseForOperation(ctx) {
let ok = await requestCheck({
http: assertNotNull(ctx.request.http),
operation: ctx.operation,
operationName: ctx.operationName,
schema: ctx.schema,
model
})
if (typeof ok == 'string') {
return {errors: [{message: ok}]}
} else if (ok) {
return null
} else {
return {errors: [{message: 'not allowed'}]}
}
}
}
}
}
}