@contember/engine-http 1.0.0-rc.1 → 1.0.0-rc.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/content/GraphQlSchemaFactory.js +1 -1
- package/dist/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/src/graphql/execution.d.ts.map +1 -1
- package/dist/src/graphql/execution.js +25 -4
- package/dist/src/graphql/execution.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -9
- package/src/content/GraphQlSchemaFactory.ts +2 -2
- package/src/graphql/execution.ts +29 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.12",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@apollographql/graphql-playground-html": "^1.6.24",
|
|
12
|
-
"@contember/database": "^1.0.0-rc.
|
|
13
|
-
"@contember/engine-content-api": "^1.0.0-rc.
|
|
14
|
-
"@contember/engine-plugins": "^1.0.0-rc.
|
|
15
|
-
"@contember/engine-system-api": "^1.0.0-rc.
|
|
16
|
-
"@contember/engine-tenant-api": "^1.0.0-rc.
|
|
17
|
-
"@contember/graphql-utils": "^1.0.0-rc.
|
|
18
|
-
"@contember/schema": "^1.0.0-rc.
|
|
19
|
-
"@contember/schema-utils": "^1.0.0-rc.
|
|
12
|
+
"@contember/database": "^1.0.0-rc.12",
|
|
13
|
+
"@contember/engine-content-api": "^1.0.0-rc.12",
|
|
14
|
+
"@contember/engine-plugins": "^1.0.0-rc.12",
|
|
15
|
+
"@contember/engine-system-api": "^1.0.0-rc.12",
|
|
16
|
+
"@contember/engine-tenant-api": "^1.0.0-rc.12",
|
|
17
|
+
"@contember/graphql-utils": "^1.0.0-rc.12",
|
|
18
|
+
"@contember/schema": "^1.0.0-rc.12",
|
|
19
|
+
"@contember/schema-utils": "^1.0.0-rc.12",
|
|
20
20
|
"@graphql-tools/merge": "^6.2.14",
|
|
21
21
|
"@graphql-tools/schema": "^7.1.5",
|
|
22
22
|
"@koa/cors": "^3.1.0",
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
Identity,
|
|
7
7
|
IntrospectionSchemaDefinitionFactory,
|
|
8
8
|
PermissionsByIdentityFactory,
|
|
9
|
-
|
|
9
|
+
Authorizator,
|
|
10
10
|
IntrospectionSchemaFactory,
|
|
11
11
|
} from '@contember/engine-content-api'
|
|
12
12
|
import { mergeSchemas } from '@graphql-tools/merge'
|
|
@@ -50,7 +50,7 @@ export class GraphQlSchemaFactory {
|
|
|
50
50
|
}
|
|
51
51
|
const { permissions, verifier } = this.permissionFactory.createPermissions(schema, identity)
|
|
52
52
|
|
|
53
|
-
const authorizator = new
|
|
53
|
+
const authorizator = new Authorizator(permissions)
|
|
54
54
|
const dataSchemaBuilder = this.graphqlSchemaBuilderFactory.create(schema.model, authorizator)
|
|
55
55
|
const contentSchemaFactory = new IntrospectionSchemaDefinitionFactory(
|
|
56
56
|
new IntrospectionSchemaFactory(
|
package/src/graphql/execution.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DocumentNode, execute, GraphQLError, GraphQLSchema, parse, validate, validateSchema } from 'graphql'
|
|
2
2
|
import { KoaContext } from '../koa'
|
|
3
3
|
import LRUCache from 'lru-cache'
|
|
4
|
+
import { createHash } from 'crypto'
|
|
4
5
|
|
|
5
6
|
export interface GraphQLListener<Context> {
|
|
6
7
|
onStart?: (ctx: {}) => Omit<GraphQLListener<Context>, 'onStart'> | void
|
|
@@ -25,17 +26,37 @@ interface FactoryArgs<Context, KoaState> {
|
|
|
25
26
|
|
|
26
27
|
export type GraphQLQueryHandler<KoaState> = (context: KoaContext<KoaState>) => any
|
|
27
28
|
|
|
29
|
+
const hitCacheMaxAgeSeconds = 10 * 60
|
|
30
|
+
const documentCacheMaxAgeSeconds = hitCacheMaxAgeSeconds * 10
|
|
31
|
+
const pruneIntervalSeconds = documentCacheMaxAgeSeconds / 2
|
|
32
|
+
const documentCacheMax = 100
|
|
33
|
+
const hitCacheMax = documentCacheMax * 2
|
|
34
|
+
|
|
28
35
|
export const createGraphQLQueryHandler = <Context, KoaState>({
|
|
29
36
|
schema,
|
|
30
37
|
contextFactory,
|
|
31
38
|
listeners,
|
|
32
39
|
}: FactoryArgs<Context, KoaState>): GraphQLQueryHandler<KoaState> => {
|
|
33
40
|
let schemaValidated = false
|
|
41
|
+
const hitCache = new LRUCache<string, true>({
|
|
42
|
+
max: hitCacheMax,
|
|
43
|
+
maxAge: hitCacheMaxAgeSeconds * 1000,
|
|
44
|
+
})
|
|
34
45
|
const documentCache = new LRUCache<string, DocumentNode>({
|
|
35
|
-
max:
|
|
46
|
+
max: documentCacheMax,
|
|
47
|
+
maxAge: documentCacheMaxAgeSeconds * 1000,
|
|
36
48
|
})
|
|
49
|
+
let lastPrune = Date.now()
|
|
50
|
+
|
|
37
51
|
return async ctx => {
|
|
38
|
-
|
|
52
|
+
|
|
53
|
+
const now = Date.now()
|
|
54
|
+
if ((now - lastPrune) > pruneIntervalSeconds * 1000) {
|
|
55
|
+
documentCache.prune()
|
|
56
|
+
lastPrune = now
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const listenersQueue = [...listeners]
|
|
39
60
|
|
|
40
61
|
listenersQueue.forEach(it => {
|
|
41
62
|
it.onStart && listenersQueue.push(it.onStart({}) || {})
|
|
@@ -66,7 +87,8 @@ export const createGraphQLQueryHandler = <Context, KoaState>({
|
|
|
66
87
|
errors: { message: 'Missing request query' },
|
|
67
88
|
})
|
|
68
89
|
}
|
|
69
|
-
|
|
90
|
+
const queryHash = createHash('md5').update(request.query).digest('hex')
|
|
91
|
+
let doc = documentCache.get(queryHash)
|
|
70
92
|
if (!doc) {
|
|
71
93
|
try {
|
|
72
94
|
doc = parse(request.query)
|
|
@@ -74,7 +96,10 @@ export const createGraphQLQueryHandler = <Context, KoaState>({
|
|
|
74
96
|
if (validationResult.length) {
|
|
75
97
|
return respond(400, { errors: validationResult })
|
|
76
98
|
}
|
|
77
|
-
|
|
99
|
+
if (hitCache.get(queryHash)) {
|
|
100
|
+
documentCache.set(queryHash, doc)
|
|
101
|
+
}
|
|
102
|
+
hitCache.set(queryHash, true)
|
|
78
103
|
} catch (e) {
|
|
79
104
|
if (e instanceof GraphQLError) {
|
|
80
105
|
return respond(400, { errors: [e] })
|