@contember/engine-http 2.2.0-alpha.19 → 2.2.0-alpha.20
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/CLAUDE.md +2 -0
- package/dist/development/index.cjs +8 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +9 -1
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/MasterContainer.cjs +28 -14
- package/dist/development/src/MasterContainer.cjs.map +1 -1
- package/dist/development/src/MasterContainer.js +28 -14
- package/dist/development/src/MasterContainer.js.map +1 -1
- package/dist/development/src/config/configSchema.cjs +50 -0
- package/dist/development/src/config/configSchema.cjs.map +1 -1
- package/dist/development/src/config/configSchema.js +50 -0
- package/dist/development/src/config/configSchema.js.map +1 -1
- package/dist/development/src/config/configTemplate.cjs +8 -0
- package/dist/development/src/config/configTemplate.cjs.map +1 -1
- package/dist/development/src/config/configTemplate.js +8 -0
- package/dist/development/src/config/configTemplate.js.map +1 -1
- package/dist/development/src/content/ContentApiControllerFactory.cjs +12 -1
- package/dist/development/src/content/ContentApiControllerFactory.cjs.map +1 -1
- package/dist/development/src/content/ContentApiControllerFactory.js +12 -1
- package/dist/development/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/development/src/content/ContentApiSpecificCache.cjs +20 -5
- package/dist/development/src/content/ContentApiSpecificCache.cjs.map +1 -1
- package/dist/development/src/content/ContentApiSpecificCache.js +20 -5
- package/dist/development/src/content/ContentApiSpecificCache.js.map +1 -1
- package/dist/development/src/content/ContentGraphQLEngine.cjs +17 -0
- package/dist/development/src/content/ContentGraphQLEngine.cjs.map +1 -0
- package/dist/development/src/content/ContentGraphQLEngine.js +17 -0
- package/dist/development/src/content/ContentGraphQLEngine.js.map +1 -0
- package/dist/development/src/content/ContentQueryHandlerFactory.cjs +4 -1
- package/dist/development/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
- package/dist/development/src/content/ContentQueryHandlerFactory.js +5 -2
- package/dist/development/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/development/src/content/ContentRequestLog.cjs +97 -0
- package/dist/development/src/content/ContentRequestLog.cjs.map +1 -0
- package/dist/development/src/content/ContentRequestLog.js +97 -0
- package/dist/development/src/content/ContentRequestLog.js.map +1 -0
- package/dist/development/src/content/GraphQlSchemaFactory.cjs +121 -45
- package/dist/development/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.js +122 -46
- package/dist/development/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/development/src/graphql/execution.cjs +18 -5
- package/dist/development/src/graphql/execution.cjs.map +1 -1
- package/dist/development/src/graphql/execution.js +19 -6
- package/dist/development/src/graphql/execution.js.map +1 -1
- package/dist/development/src/project/ProjectContainer.cjs +2 -0
- package/dist/development/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainer.js +2 -0
- package/dist/development/src/project/ProjectContainer.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs +2 -0
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.js +2 -0
- package/dist/development/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/development/src/system/ContentQueryExecutor.cjs +23 -6
- package/dist/development/src/system/ContentQueryExecutor.cjs.map +1 -1
- package/dist/development/src/system/ContentQueryExecutor.js +25 -8
- package/dist/development/src/system/ContentQueryExecutor.js.map +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/index.cjs +8 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +9 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/MasterContainer.cjs +28 -14
- package/dist/production/src/MasterContainer.cjs.map +1 -1
- package/dist/production/src/MasterContainer.js +28 -14
- package/dist/production/src/MasterContainer.js.map +1 -1
- package/dist/production/src/config/configSchema.cjs +50 -0
- package/dist/production/src/config/configSchema.cjs.map +1 -1
- package/dist/production/src/config/configSchema.js +50 -0
- package/dist/production/src/config/configSchema.js.map +1 -1
- package/dist/production/src/config/configTemplate.cjs +8 -0
- package/dist/production/src/config/configTemplate.cjs.map +1 -1
- package/dist/production/src/config/configTemplate.js +8 -0
- package/dist/production/src/config/configTemplate.js.map +1 -1
- package/dist/production/src/content/ContentApiControllerFactory.cjs +12 -1
- package/dist/production/src/content/ContentApiControllerFactory.cjs.map +1 -1
- package/dist/production/src/content/ContentApiControllerFactory.js +12 -1
- package/dist/production/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/production/src/content/ContentApiSpecificCache.cjs +20 -5
- package/dist/production/src/content/ContentApiSpecificCache.cjs.map +1 -1
- package/dist/production/src/content/ContentApiSpecificCache.js +20 -5
- package/dist/production/src/content/ContentApiSpecificCache.js.map +1 -1
- package/dist/production/src/content/ContentGraphQLEngine.cjs +17 -0
- package/dist/production/src/content/ContentGraphQLEngine.cjs.map +1 -0
- package/dist/production/src/content/ContentGraphQLEngine.js +17 -0
- package/dist/production/src/content/ContentGraphQLEngine.js.map +1 -0
- package/dist/production/src/content/ContentQueryHandlerFactory.cjs +4 -1
- package/dist/production/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
- package/dist/production/src/content/ContentQueryHandlerFactory.js +5 -2
- package/dist/production/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/production/src/content/ContentRequestLog.cjs +97 -0
- package/dist/production/src/content/ContentRequestLog.cjs.map +1 -0
- package/dist/production/src/content/ContentRequestLog.js +97 -0
- package/dist/production/src/content/ContentRequestLog.js.map +1 -0
- package/dist/production/src/content/GraphQlSchemaFactory.cjs +121 -45
- package/dist/production/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.js +122 -46
- package/dist/production/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/production/src/graphql/execution.cjs +18 -5
- package/dist/production/src/graphql/execution.cjs.map +1 -1
- package/dist/production/src/graphql/execution.js +19 -6
- package/dist/production/src/graphql/execution.js.map +1 -1
- package/dist/production/src/project/ProjectContainer.cjs +2 -0
- package/dist/production/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainer.js +2 -0
- package/dist/production/src/project/ProjectContainer.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs +2 -0
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.js +2 -0
- package/dist/production/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/production/src/system/ContentQueryExecutor.cjs +23 -6
- package/dist/production/src/system/ContentQueryExecutor.cjs.map +1 -1
- package/dist/production/src/system/ContentQueryExecutor.js +25 -8
- package/dist/production/src/system/ContentQueryExecutor.js.map +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts +2 -0
- package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/contentRequestLog.test.d.ts +2 -0
- package/dist/tests/cases/unit/contentRequestLog.test.d.ts.map +1 -0
- package/dist/tests/contentControllerHarness.d.ts +29 -0
- package/dist/tests/contentControllerHarness.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/MasterContainer.d.ts +10 -1
- package/dist/types/MasterContainer.d.ts.map +1 -1
- package/dist/types/config/configSchema.d.ts +10 -0
- package/dist/types/config/configSchema.d.ts.map +1 -1
- package/dist/types/config/configTemplate.d.ts.map +1 -1
- package/dist/types/content/ContentApiControllerFactory.d.ts +3 -1
- package/dist/types/content/ContentApiControllerFactory.d.ts.map +1 -1
- package/dist/types/content/ContentApiSpecificCache.d.ts +2 -0
- package/dist/types/content/ContentApiSpecificCache.d.ts.map +1 -1
- package/dist/types/content/ContentGraphQLEngine.d.ts +18 -0
- package/dist/types/content/ContentGraphQLEngine.d.ts.map +1 -0
- package/dist/types/content/ContentQueryHandlerFactory.d.ts +3 -2
- package/dist/types/content/ContentQueryHandlerFactory.d.ts.map +1 -1
- package/dist/types/content/ContentRequestLog.d.ts +49 -0
- package/dist/types/content/ContentRequestLog.d.ts.map +1 -0
- package/dist/types/content/GraphQlSchemaFactory.d.ts +14 -4
- package/dist/types/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/types/content/index.d.ts +2 -0
- package/dist/types/content/index.d.ts.map +1 -1
- package/dist/types/graphql/execution.d.ts +21 -3
- package/dist/types/graphql/execution.d.ts.map +1 -1
- package/dist/types/system/ContentQueryExecutor.d.ts +4 -1
- package/dist/types/system/ContentQueryExecutor.d.ts.map +1 -1
- package/dist/types/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -18
- package/src/MasterContainer.ts +31 -14
- package/src/config/configSchema.ts +63 -0
- package/src/config/configTemplate.ts +8 -0
- package/src/content/ContentApiControllerFactory.ts +15 -2
- package/src/content/ContentApiSpecificCache.ts +22 -7
- package/src/content/ContentGraphQLEngine.ts +24 -0
- package/src/content/ContentQueryHandlerFactory.ts +16 -4
- package/src/content/ContentRequestLog.ts +138 -0
- package/src/content/GraphQlSchemaFactory.ts +173 -54
- package/src/content/index.ts +2 -0
- package/src/graphql/execution.ts +35 -7
- package/src/project/ProjectContainer.ts +1 -1
- package/src/system/ContentQueryExecutor.ts +43 -10
- package/src/system/SystemGraphQLHandlerFactory.ts +8 -2
- package/src/tenant/TenantGraphQLHandlerFactory.ts +2 -2
- package/src/tsconfig.json +1 -0
- package/tests/cases/unit/__snapshots__/contentGraphqlCompat.test.ts.snap +14295 -0
- package/tests/cases/unit/contentGraphqlCompat.test.ts +372 -0
- package/tests/cases/unit/contentRequestLog.test.ts +141 -0
- package/tests/cases/unit/forcePrimary.test.ts +93 -284
- package/tests/cases/unit/graphQlSchemaFactory.test.ts +227 -35
- package/tests/cases/unit/requestMemoryBudget.test.ts +96 -78
- package/tests/cases/unit/serverConfig.test.ts +133 -0
- package/tests/contentControllerHarness.ts +222 -0
- package/tests/tsconfig.json +3 -1
|
@@ -2,15 +2,21 @@ import {
|
|
|
2
2
|
Authorizator,
|
|
3
3
|
Context,
|
|
4
4
|
ContextualPermissions,
|
|
5
|
+
createGraphQLJsBridge,
|
|
6
|
+
createJsonIntrospectionExtension,
|
|
5
7
|
EntityRulesResolver,
|
|
6
8
|
GraphQlSchemaBuilderFactory,
|
|
7
9
|
IntrospectionSchemaDefinitionFactory,
|
|
8
10
|
IntrospectionSchemaFactory,
|
|
11
|
+
JsonContentSchema,
|
|
12
|
+
JsonContentSchemaBuilder,
|
|
13
|
+
JsonContentSchemaShared,
|
|
9
14
|
PermissionFactory,
|
|
10
15
|
} from '@contember/engine-content-api'
|
|
11
16
|
import { Acl, Schema } from '@contember/schema'
|
|
12
17
|
import { GraphQLFieldConfig, GraphQLNamedType, GraphQLSchema } from 'graphql'
|
|
13
18
|
import { createHash } from 'node:crypto'
|
|
19
|
+
import { ContentGraphQLEngineResolver, ContentGraphQLSchema } from './ContentGraphQLEngine.js'
|
|
14
20
|
import { GraphQLSchemaContributor } from './GraphQLSchemaContributor.js'
|
|
15
21
|
import { Identity } from './Identity.js'
|
|
16
22
|
import { ContentApiSpecificCache } from './ContentApiSpecificCache.js'
|
|
@@ -19,78 +25,191 @@ import { ProjectConfig } from '../project/config.js'
|
|
|
19
25
|
export interface GraphQLSchemaFactoryResult {
|
|
20
26
|
permissions: Acl.Permissions
|
|
21
27
|
allPermissions?: Acl.Permissions
|
|
22
|
-
schema:
|
|
28
|
+
schema: ContentGraphQLSchema
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Schemas of one model built against one shared state. Its interner keeps everything it interned, also the data of
|
|
33
|
+
* schemas the cache has evicted, so the state is replaced once evictions dominate and freed when none is in use.
|
|
34
|
+
*/
|
|
35
|
+
interface SharedGeneration {
|
|
36
|
+
readonly shared: JsonContentSchemaShared
|
|
37
|
+
readonly built: WeakRef<JsonContentSchema>[]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// A new generation interns the role-independent part again, which costs about as much as the unique data of ~10 roles.
|
|
41
|
+
const MIN_EVICTED_FOR_NEW_GENERATION = 16
|
|
42
|
+
|
|
43
|
+
// A large model with permissive roles takes seconds to build; other requests of the process wait at most one slice.
|
|
44
|
+
const BUILD_SLICE_MS = 10
|
|
45
|
+
|
|
46
|
+
/** Between slices, `setImmediate` lets the event loop run I/O; a microtask (`process.nextTick`, a promise) would not. */
|
|
47
|
+
const runInSlices = async <T>(steps: Generator<void, T>): Promise<T> => {
|
|
48
|
+
let sliceEnd = performance.now() + BUILD_SLICE_MS
|
|
49
|
+
for (;;) {
|
|
50
|
+
const step = steps.next()
|
|
51
|
+
if (step.done) {
|
|
52
|
+
return step.value
|
|
53
|
+
}
|
|
54
|
+
if (performance.now() >= sliceEnd) {
|
|
55
|
+
await new Promise<void>(resolve => setImmediate(resolve))
|
|
56
|
+
sliceEnd = performance.now() + BUILD_SLICE_MS
|
|
57
|
+
}
|
|
58
|
+
}
|
|
23
59
|
}
|
|
24
60
|
|
|
25
61
|
export class GraphQlSchemaFactory {
|
|
26
62
|
private readonly permissionsKeys = new WeakMap<Schema, Map<string, string>>()
|
|
63
|
+
/** Weak, so that only the cached schemas keep their generation alive (via `generationOf`). */
|
|
64
|
+
private readonly generations = new WeakMap<Schema, WeakRef<SharedGeneration>>()
|
|
65
|
+
private readonly generationOf = new WeakMap<JsonContentSchema, SharedGeneration>()
|
|
66
|
+
/** Builds in progress, so that concurrent requests of one role combination build it once. */
|
|
67
|
+
private readonly building = new WeakMap<Schema, Map<string, Promise<GraphQLSchemaFactoryResult>>>()
|
|
27
68
|
|
|
28
69
|
constructor(
|
|
29
70
|
private readonly cache: ContentApiSpecificCache<Schema, GraphQLSchemaFactoryResult>,
|
|
30
71
|
private readonly graphqlSchemaBuilderFactory: GraphQlSchemaBuilderFactory,
|
|
31
72
|
private readonly permissionFactory: PermissionFactory,
|
|
32
73
|
private readonly schemaContributors: GraphQLSchemaContributor[],
|
|
74
|
+
private readonly engineResolver: ContentGraphQLEngineResolver = new ContentGraphQLEngineResolver(),
|
|
33
75
|
) {}
|
|
34
76
|
|
|
35
|
-
public create(schema: Schema, identity: Identity, project: ProjectConfig): GraphQLSchemaFactoryResult {
|
|
77
|
+
public async create(schema: Schema, identity: Identity, project: ProjectConfig): Promise<GraphQLSchemaFactoryResult> {
|
|
36
78
|
const { permissionsKey, computedPermissions } = this.getPermissionsKey(schema, identity.projectRoles)
|
|
37
79
|
const contributorsKey = this.schemaContributors.map(it => it.getCacheKey?.({ schema, identity, project }) ?? '').join('\xff')
|
|
38
|
-
const
|
|
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
|
-
|
|
80
|
+
const engine = this.engineResolver.resolve(project.slug)
|
|
81
|
+
const cacheKey = `${engine}\xff\xff${permissionsKey}\xff\xff${contributorsKey}`
|
|
82
|
+
|
|
83
|
+
const cached = this.cache.get(schema, cacheKey)
|
|
84
|
+
if (cached) {
|
|
85
|
+
return cached
|
|
86
|
+
}
|
|
87
|
+
let building = this.building.get(schema)
|
|
88
|
+
if (!building) {
|
|
89
|
+
building = new Map()
|
|
90
|
+
this.building.set(schema, building)
|
|
91
|
+
}
|
|
92
|
+
const inProgress = building.get(cacheKey)
|
|
93
|
+
if (inProgress) {
|
|
94
|
+
return await inProgress
|
|
95
|
+
}
|
|
96
|
+
const build = engine === 'graphql-js'
|
|
97
|
+
? Promise.resolve(this.buildGraphQLJs(schema, identity, project, computedPermissions))
|
|
98
|
+
: this.buildGraphQLJson(schema, identity, project, computedPermissions)
|
|
99
|
+
building.set(cacheKey, build)
|
|
100
|
+
try {
|
|
101
|
+
return this.cache.set(schema, cacheKey, await build)
|
|
102
|
+
} finally {
|
|
103
|
+
building.delete(cacheKey)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The graphql-js schema, built as before graphql-json existed. */
|
|
108
|
+
private buildGraphQLJs(
|
|
109
|
+
schema: Schema,
|
|
110
|
+
identity: Identity,
|
|
111
|
+
project: ProjectConfig,
|
|
112
|
+
computedPermissions: ContextualPermissions | undefined,
|
|
113
|
+
): GraphQLSchemaFactoryResult {
|
|
114
|
+
const { root: permissions, all: allPermissions } = computedPermissions ?? this.permissionFactory.createContextual(schema, identity.projectRoles)
|
|
115
|
+
|
|
116
|
+
const customPrimary = schema.acl.customPrimary ?? false
|
|
117
|
+
const refreshMaterializedView = this.permissionFactory.isRefreshMaterializedViewAllowed(schema, identity.projectRoles)
|
|
118
|
+
// Types are shaped by the nested set, so a field granted only through a relation still exists
|
|
119
|
+
// on the shared type; the root query and mutation fields are gated on the root set alone.
|
|
120
|
+
const authorizator = new Authorizator(allPermissions, customPrimary, refreshMaterializedView)
|
|
121
|
+
const rootAuthorizator = new Authorizator(permissions, customPrimary, refreshMaterializedView)
|
|
122
|
+
const dataSchemaBuilder = this.graphqlSchemaBuilderFactory.create(schema.model, authorizator, rootAuthorizator)
|
|
123
|
+
const introspectionSchemaFactory = new IntrospectionSchemaDefinitionFactory(
|
|
124
|
+
new IntrospectionSchemaFactory(
|
|
125
|
+
schema.model,
|
|
126
|
+
new EntityRulesResolver(schema.validation, schema.model),
|
|
127
|
+
authorizator,
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
const introspectionSchema = introspectionSchemaFactory.createConfig()
|
|
131
|
+
|
|
132
|
+
const otherSchemas = this.schemaContributors
|
|
133
|
+
.map(it => it.createSchema({ schema, identity, project }))
|
|
134
|
+
.filter(<T>(it: T | undefined): it is T => it !== undefined)
|
|
135
|
+
|
|
136
|
+
const queries = new Map<string, GraphQLFieldConfig<any, Context, any>>()
|
|
137
|
+
const mutations = new Map<string, GraphQLFieldConfig<any, Context, any>>()
|
|
138
|
+
const types: GraphQLNamedType[] = []
|
|
139
|
+
|
|
140
|
+
for (const schema of [introspectionSchema, ...otherSchemas]) {
|
|
141
|
+
if (schema instanceof GraphQLSchema) {
|
|
142
|
+
for (const [field, config] of Object.entries(schema.getQueryType()?.toConfig().fields ?? {})) {
|
|
143
|
+
queries.set(field, config)
|
|
144
|
+
}
|
|
145
|
+
for (const [field, config] of Object.entries(schema.getQueryType()?.toConfig().fields ?? {})) {
|
|
146
|
+
mutations.set(field, config)
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
for (const [field, config] of Object.entries(schema.query?.toConfig().fields ?? {})) {
|
|
150
|
+
queries.set(field, config)
|
|
83
151
|
}
|
|
152
|
+
for (const [field, config] of Object.entries(schema.mutation?.toConfig().fields ?? {})) {
|
|
153
|
+
mutations.set(field, config)
|
|
154
|
+
}
|
|
155
|
+
types.push(...(schema.types ?? []))
|
|
84
156
|
}
|
|
157
|
+
}
|
|
85
158
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
return { schema: graphQlSchema, permissions, allPermissions }
|
|
159
|
+
const graphQlSchema = dataSchemaBuilder.build({
|
|
160
|
+
mutations,
|
|
161
|
+
queries,
|
|
162
|
+
types,
|
|
93
163
|
})
|
|
164
|
+
|
|
165
|
+
return { schema: { engine: 'graphql-js', schema: graphQlSchema }, permissions, allPermissions }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private async buildGraphQLJson(
|
|
169
|
+
schema: Schema,
|
|
170
|
+
identity: Identity,
|
|
171
|
+
project: ProjectConfig,
|
|
172
|
+
computedPermissions: ContextualPermissions | undefined,
|
|
173
|
+
): Promise<GraphQLSchemaFactoryResult> {
|
|
174
|
+
const { root: permissions, all: allPermissions } = computedPermissions ?? this.permissionFactory.createContextual(schema, identity.projectRoles)
|
|
175
|
+
|
|
176
|
+
const customPrimary = schema.acl.customPrimary ?? false
|
|
177
|
+
const refreshMaterializedView = this.permissionFactory.isRefreshMaterializedViewAllowed(schema, identity.projectRoles)
|
|
178
|
+
// Types are shaped by the nested set, so a field granted only through a relation still exists
|
|
179
|
+
// on the shared type; the root query and mutation fields are gated on the root set alone.
|
|
180
|
+
const authorizator = new Authorizator(allPermissions, customPrimary, refreshMaterializedView)
|
|
181
|
+
const rootAuthorizator = new Authorizator(permissions, customPrimary, refreshMaterializedView)
|
|
182
|
+
const introspectionSchemaFactory = new IntrospectionSchemaFactory(
|
|
183
|
+
schema.model,
|
|
184
|
+
new EntityRulesResolver(schema.validation, schema.model),
|
|
185
|
+
authorizator,
|
|
186
|
+
)
|
|
187
|
+
const otherSchemas = this.schemaContributors
|
|
188
|
+
.map(it => it.createSchema({ schema, identity, project }))
|
|
189
|
+
.filter(<T>(it: T | undefined): it is T => it !== undefined)
|
|
190
|
+
|
|
191
|
+
const generation = this.getGeneration(schema)
|
|
192
|
+
const builder = new JsonContentSchemaBuilder(schema.model, authorizator, rootAuthorizator, generation.shared)
|
|
193
|
+
const jsonSchema = await runInSlices(builder.buildSteps([
|
|
194
|
+
createJsonIntrospectionExtension(introspectionSchemaFactory),
|
|
195
|
+
...otherSchemas.map(createGraphQLJsBridge),
|
|
196
|
+
]))
|
|
197
|
+
generation.built.push(new WeakRef(jsonSchema))
|
|
198
|
+
this.generationOf.set(jsonSchema, generation)
|
|
199
|
+
return { schema: { engine: 'graphql-json', schema: jsonSchema }, permissions, allPermissions }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private getGeneration(schema: Schema): SharedGeneration {
|
|
203
|
+
const current = this.generations.get(schema)?.deref()
|
|
204
|
+
if (current) {
|
|
205
|
+
const evicted = current.built.filter(it => it.deref() === undefined).length
|
|
206
|
+
if (evicted < MIN_EVICTED_FOR_NEW_GENERATION || evicted < current.built.length - evicted) {
|
|
207
|
+
return current
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const generation: SharedGeneration = { shared: new JsonContentSchemaShared(), built: [] }
|
|
211
|
+
this.generations.set(schema, new WeakRef(generation))
|
|
212
|
+
return generation
|
|
94
213
|
}
|
|
95
214
|
|
|
96
215
|
// Role combinations with identical permissions share one GraphQL schema. The key is memoized per combination,
|
package/src/content/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './ContentApiControllerFactory.js'
|
|
2
|
+
export * from './ContentGraphQLEngine.js'
|
|
2
3
|
export * from './ContentQueryHandlerFactory.js'
|
|
4
|
+
export * from './ContentRequestLog.js'
|
|
3
5
|
export * from './GraphQLSchemaContributor.js'
|
|
4
6
|
export * from './ContentSchemaResolver.js'
|
|
5
7
|
export * from './GraphQlSchemaFactory.js'
|
package/src/graphql/execution.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DocumentNode,
|
|
3
3
|
execute,
|
|
4
|
+
ExecutionResult,
|
|
4
5
|
GraphQLError,
|
|
5
6
|
GraphQLSchema,
|
|
6
7
|
Kind,
|
|
7
|
-
OperationDefinitionNode,
|
|
8
8
|
OperationTypeNode,
|
|
9
9
|
parse,
|
|
10
10
|
validate,
|
|
@@ -17,6 +17,7 @@ import { logger } from '@contember/logger'
|
|
|
17
17
|
import { ForbiddenError } from '@contember/graphql-utils'
|
|
18
18
|
import { UserError } from '@contember/engine-content-api'
|
|
19
19
|
import { RequestMemoryBudget, RequestMemoryBudgetExceededError } from '@contember/database'
|
|
20
|
+
import { execute as executeJson, ResolverMap, ScalarMap, Schema as JsonSchema, validate as validateJson } from '@contember/graphql-json'
|
|
20
21
|
|
|
21
22
|
export interface GraphQLListener<Context> {
|
|
22
23
|
onStart?: (ctx: {}) => Omit<GraphQLListener<Context>, 'onStart'> | void
|
|
@@ -34,8 +35,36 @@ export interface GraphQLListener<Context> {
|
|
|
34
35
|
}) => Omit<GraphQLListener<Context>, 'onStart' | 'onExecute' | 'onResponse' | 'onShutdown'> | void
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
/** The GraphQL engine a handler runs on. */
|
|
39
|
+
export interface GraphQLExecutor<Context> {
|
|
40
|
+
validateSchema(): readonly GraphQLError[]
|
|
41
|
+
validate(document: DocumentNode): readonly GraphQLError[]
|
|
42
|
+
execute(args: {
|
|
43
|
+
document: DocumentNode
|
|
44
|
+
operationName: string | null
|
|
45
|
+
variableValues: Record<string, unknown> | undefined
|
|
46
|
+
contextValue: Context
|
|
47
|
+
}): Promise<Pick<ExecutionResult, 'data' | 'errors'>>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const createGraphQLJsExecutor = <Context>(schema: GraphQLSchema): GraphQLExecutor<Context> => ({
|
|
51
|
+
validateSchema: () => validateSchema(schema),
|
|
52
|
+
validate: document => validate(schema, document),
|
|
53
|
+
execute: async args => await execute({ schema, ...args }),
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export const createGraphQLJsonExecutor = <Context>(
|
|
57
|
+
{ schema, resolvers, scalars }: { schema: JsonSchema; resolvers: readonly ResolverMap<Context>[]; scalars: ScalarMap },
|
|
58
|
+
): GraphQLExecutor<Context> => ({
|
|
59
|
+
// the content builder output is validated as a graphql-js schema in tests; there is no runtime schema validation
|
|
60
|
+
validateSchema: () => [],
|
|
61
|
+
validate: document => validateJson(schema, document, { scalars }),
|
|
62
|
+
execute: async ({ document, operationName, variableValues, contextValue }) =>
|
|
63
|
+
await executeJson({ schema, document, resolvers, scalars, operationName: operationName ?? undefined, variableValues, contextValue }),
|
|
64
|
+
})
|
|
65
|
+
|
|
37
66
|
interface FactoryArgs<Context> {
|
|
38
|
-
|
|
67
|
+
executor: GraphQLExecutor<Context>
|
|
39
68
|
listeners: GraphQLListener<Context>[]
|
|
40
69
|
getMemoryBudget?: (context: Context) => RequestMemoryBudget | undefined
|
|
41
70
|
}
|
|
@@ -53,7 +82,7 @@ const documentCacheMax = 100
|
|
|
53
82
|
const hitCacheMax = documentCacheMax * 2
|
|
54
83
|
|
|
55
84
|
export const createGraphQLQueryHandler = <Context>({
|
|
56
|
-
|
|
85
|
+
executor,
|
|
57
86
|
listeners,
|
|
58
87
|
getMemoryBudget,
|
|
59
88
|
}: FactoryArgs<Context>): GraphQLQueryHandler<Context> => {
|
|
@@ -85,7 +114,7 @@ export const createGraphQLQueryHandler = <Context>({
|
|
|
85
114
|
}
|
|
86
115
|
try {
|
|
87
116
|
if (!schemaValidated) {
|
|
88
|
-
const validationResult = validateSchema(
|
|
117
|
+
const validationResult = executor.validateSchema()
|
|
89
118
|
if (validationResult.length) {
|
|
90
119
|
return respond(400, {
|
|
91
120
|
errors: validationResult,
|
|
@@ -103,7 +132,7 @@ export const createGraphQLQueryHandler = <Context>({
|
|
|
103
132
|
let doc = documentCache.get(queryHash)
|
|
104
133
|
if (!doc) {
|
|
105
134
|
doc = parse(resolvedRequest.query)
|
|
106
|
-
const validationResult = validate(
|
|
135
|
+
const validationResult = executor.validate(doc)
|
|
107
136
|
if (validationResult.length) {
|
|
108
137
|
return respond(400, { errors: validationResult })
|
|
109
138
|
}
|
|
@@ -120,8 +149,7 @@ export const createGraphQLQueryHandler = <Context>({
|
|
|
120
149
|
listenersQueue.forEach(it => {
|
|
121
150
|
it.onExecute && listenersQueue.push(it.onExecute({ context, document, operation }) || {})
|
|
122
151
|
})
|
|
123
|
-
const executionResult = await execute({
|
|
124
|
-
schema,
|
|
152
|
+
const executionResult = await executor.execute({
|
|
125
153
|
document,
|
|
126
154
|
operationName: operationName,
|
|
127
155
|
variableValues: resolvedRequest.variables,
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
StageCreator,
|
|
9
9
|
SystemMigrationsRunner,
|
|
10
10
|
} from '@contember/engine-system-api'
|
|
11
|
-
import {
|
|
11
|
+
import { PermissionFactory } from '@contember/engine-content-api'
|
|
12
12
|
import { Logger } from '@contember/logger'
|
|
13
13
|
import { ProjectConfig } from './config.js'
|
|
14
14
|
import { ContentSchemaResolver } from '../content/index.js'
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Authorizator,
|
|
3
|
+
ExecutionContainerFactory,
|
|
4
|
+
GraphQlSchemaBuilderFactory,
|
|
5
|
+
JsonContentSchema,
|
|
6
|
+
JsonContentSchemaBuilder,
|
|
7
|
+
} from '@contember/engine-content-api'
|
|
8
|
+
import { execute, validate } from '@contember/graphql-json'
|
|
2
9
|
import {
|
|
3
10
|
ContentQueryExecutor,
|
|
4
11
|
ContentQueryExecutorContext,
|
|
@@ -7,7 +14,8 @@ import {
|
|
|
7
14
|
} from '@contember/engine-system-api'
|
|
8
15
|
import { logger } from '@contember/logger'
|
|
9
16
|
import { AllowAllPermissionFactory } from '@contember/schema-utils'
|
|
10
|
-
import { graphql, GraphQLError } from 'graphql'
|
|
17
|
+
import { ExecutionResult, graphql, GraphQLError, parse } from 'graphql'
|
|
18
|
+
import { ContentGraphQLEngineResolver, ContentGraphQLSchema } from '../content/ContentGraphQLEngine.js'
|
|
11
19
|
import { ContentGraphqlContext } from '../content/ContentGraphqlContext.js'
|
|
12
20
|
import { extractOriginalError } from '../graphql/index.js'
|
|
13
21
|
|
|
@@ -15,6 +23,7 @@ export class ContentQueryExecutorImpl implements ContentQueryExecutor {
|
|
|
15
23
|
constructor(
|
|
16
24
|
private readonly executionContainerFactory: ExecutionContainerFactory,
|
|
17
25
|
private readonly graphqlSchemaBuilderFactory: GraphQlSchemaBuilderFactory,
|
|
26
|
+
private readonly engineResolver: ContentGraphQLEngineResolver = new ContentGraphQLEngineResolver(),
|
|
18
27
|
) {
|
|
19
28
|
}
|
|
20
29
|
|
|
@@ -24,8 +33,9 @@ export class ContentQueryExecutorImpl implements ContentQueryExecutor {
|
|
|
24
33
|
): Promise<ContentQueryExecutorResult> {
|
|
25
34
|
const permissions = new AllowAllPermissionFactory().create(schema.model, true)
|
|
26
35
|
const authorizator = new Authorizator(permissions, true, false)
|
|
27
|
-
const
|
|
28
|
-
|
|
36
|
+
const dataSchema: ContentGraphQLSchema = this.engineResolver.resolve(project.slug) === 'graphql-js'
|
|
37
|
+
? { engine: 'graphql-js', schema: this.graphqlSchemaBuilderFactory.create(schema.model, authorizator, authorizator).build() }
|
|
38
|
+
: { engine: 'graphql-json', schema: new JsonContentSchemaBuilder(schema.model, authorizator, authorizator).build() }
|
|
29
39
|
|
|
30
40
|
const identityId = identity.id
|
|
31
41
|
|
|
@@ -57,12 +67,14 @@ export class ContentQueryExecutorImpl implements ContentQueryExecutor {
|
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
try {
|
|
60
|
-
const result =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
70
|
+
const result = dataSchema.engine === 'graphql-js'
|
|
71
|
+
? await graphql({
|
|
72
|
+
schema: dataSchema.schema,
|
|
73
|
+
source: query,
|
|
74
|
+
variableValues: variables,
|
|
75
|
+
contextValue: ctx,
|
|
76
|
+
})
|
|
77
|
+
: await this.executeGraphQLJson(dataSchema.schema, query, variables, ctx)
|
|
66
78
|
if (result.errors?.length) {
|
|
67
79
|
return { ok: false, errors: result.errors.map(formatErrorMessage) }
|
|
68
80
|
}
|
|
@@ -71,6 +83,27 @@ export class ContentQueryExecutorImpl implements ContentQueryExecutor {
|
|
|
71
83
|
return { ok: false, errors: [formatErrorMessage(e)] }
|
|
72
84
|
}
|
|
73
85
|
}
|
|
86
|
+
|
|
87
|
+
private async executeGraphQLJson(
|
|
88
|
+
dataSchema: JsonContentSchema,
|
|
89
|
+
query: string,
|
|
90
|
+
variables: Record<string, unknown> | undefined,
|
|
91
|
+
ctx: ContentGraphqlContext,
|
|
92
|
+
): Promise<Pick<ExecutionResult, 'data' | 'errors'>> {
|
|
93
|
+
const document = parse(query)
|
|
94
|
+
const validationErrors = validate(dataSchema.schema, document, { scalars: dataSchema.scalars })
|
|
95
|
+
if (validationErrors.length) {
|
|
96
|
+
return { errors: validationErrors }
|
|
97
|
+
}
|
|
98
|
+
return await execute({
|
|
99
|
+
schema: dataSchema.schema,
|
|
100
|
+
document,
|
|
101
|
+
resolvers: dataSchema.resolvers,
|
|
102
|
+
scalars: dataSchema.scalars,
|
|
103
|
+
variableValues: variables,
|
|
104
|
+
contextValue: ctx,
|
|
105
|
+
})
|
|
106
|
+
}
|
|
74
107
|
}
|
|
75
108
|
|
|
76
109
|
const formatErrorMessage = (e: any): string => {
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { devTypeDefs, ResolverFactory, SystemResolverContext, typeDefs } from '@contember/engine-system-api'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
createDbQueriesListener,
|
|
4
|
+
createGraphQLJsExecutor,
|
|
5
|
+
createGraphQLQueryHandler,
|
|
6
|
+
GraphQLListener,
|
|
7
|
+
GraphQLQueryHandler,
|
|
8
|
+
} from '../graphql/index.js'
|
|
3
9
|
import { DocumentNode } from 'graphql'
|
|
4
10
|
import { mergeTypeDefs } from '@graphql-tools/merge'
|
|
5
11
|
import { makeExecutableSchema } from '@graphql-tools/schema'
|
|
@@ -38,7 +44,7 @@ export class SystemGraphQLHandlerFactory {
|
|
|
38
44
|
listeners.push(createDbQueriesListener(context => context.db.client, this.debugMode))
|
|
39
45
|
}
|
|
40
46
|
return createGraphQLQueryHandler<SystemGraphQLContext>({
|
|
41
|
-
schema,
|
|
47
|
+
executor: createGraphQLJsExecutor(schema),
|
|
42
48
|
listeners,
|
|
43
49
|
})
|
|
44
50
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { makeExecutableSchema } from '@graphql-tools/schema'
|
|
2
2
|
import { Schema, TenantResolverContext, typeDefs } from '@contember/engine-tenant-api'
|
|
3
|
-
import { createGraphQLQueryHandler, GraphQLQueryHandler } from '../graphql/index.js'
|
|
3
|
+
import { createGraphQLJsExecutor, createGraphQLQueryHandler, GraphQLQueryHandler } from '../graphql/index.js'
|
|
4
4
|
|
|
5
5
|
export type TenantGraphQLContext = TenantResolverContext & { identityId: string }
|
|
6
6
|
|
|
@@ -14,7 +14,7 @@ export class TenantGraphQLHandlerFactory {
|
|
|
14
14
|
resolverValidationOptions: { requireResolversForResolveType: 'ignore' },
|
|
15
15
|
})
|
|
16
16
|
return createGraphQLQueryHandler<TenantGraphQLContext>({
|
|
17
|
-
schema,
|
|
17
|
+
executor: createGraphQLJsExecutor(schema),
|
|
18
18
|
listeners: [],
|
|
19
19
|
})
|
|
20
20
|
}
|
package/src/tsconfig.json
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
{ "path": "../../engine-tenant-api/src" },
|
|
14
14
|
{ "path": "../../engine-system-api/src" },
|
|
15
15
|
{ "path": "../../engine-content-api/src" },
|
|
16
|
+
{ "path": "../../graphql-json/src" },
|
|
16
17
|
{ "path": "../../graphql-utils/src" },
|
|
17
18
|
{ "path": "../../logger/src" },
|
|
18
19
|
{ "path": "../../schema/src" },
|