@contember/engine-http 0.12.0-alpha.8 → 0.12.0-beta.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-http",
3
- "version": "0.12.0-alpha.8",
3
+ "version": "0.12.0-beta.2",
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": "^0.12.0-alpha.8",
13
- "@contember/engine-content-api": "^0.12.0-alpha.8",
14
- "@contember/engine-plugins": "^0.12.0-alpha.8",
15
- "@contember/engine-system-api": "^0.12.0-alpha.8",
16
- "@contember/engine-tenant-api": "^0.12.0-alpha.8",
17
- "@contember/graphql-utils": "^0.12.0-alpha.8",
18
- "@contember/schema": "^0.12.0-alpha.8",
19
- "@contember/schema-utils": "^0.12.0-alpha.8",
12
+ "@contember/database": "^0.12.0-beta.2",
13
+ "@contember/engine-content-api": "^0.12.0-beta.2",
14
+ "@contember/engine-plugins": "^0.12.0-beta.2",
15
+ "@contember/engine-system-api": "^0.12.0-beta.2",
16
+ "@contember/engine-tenant-api": "^0.12.0-beta.2",
17
+ "@contember/graphql-utils": "^0.12.0-beta.2",
18
+ "@contember/schema": "^0.12.0-beta.2",
19
+ "@contember/schema-utils": "^0.12.0-beta.2",
20
20
  "@graphql-tools/merge": "^6.2.14",
21
21
  "@graphql-tools/schema": "^7.1.5",
22
22
  "@koa/cors": "^3.1.0",
@@ -1,16 +1,13 @@
1
1
  import { ContentSchemaResolver } from './ContentSchemaResolver'
2
2
  import { GraphQlSchemaFactory } from './GraphQlSchemaFactory'
3
3
  import { ContentQueryHandlerFactory, KoaState } from './ContentQueryHandlerFactory'
4
- import LRUCache from 'lru-cache'
5
4
  import { GraphQLSchema } from 'graphql'
6
5
  import { DatabaseContext } from '@contember/engine-system-api'
7
6
  import { KoaMiddleware } from '../koa'
8
7
  import { StageConfig } from '../ProjectConfig'
9
8
 
10
9
  export class ContentQueryHandlerProvider {
11
- private cache = new LRUCache<GraphQLSchema, KoaMiddleware<KoaState>>({
12
- max: 100,
13
- })
10
+ private cache = new WeakMap<GraphQLSchema, KoaMiddleware<KoaState>>()
14
11
 
15
12
  constructor(
16
13
  private readonly contentSchemaFactory: ContentSchemaResolver,
@@ -15,16 +15,16 @@ import { GraphQLSchemaContributor } from '@contember/engine-plugins'
15
15
  import { JSONType } from '@contember/graphql-utils'
16
16
 
17
17
  type Context = { schema: Schema; identity: Identity }
18
+
19
+ type CacheEntry = {
20
+ graphQlSchema: GraphQLSchema
21
+ permissions: Acl.Permissions
22
+ contributorsCacheKey: string
23
+ verifier: (identity: Identity) => boolean
24
+ }
25
+
18
26
  class GraphQlSchemaFactory {
19
- private cache: {
20
- schema: Schema
21
- cacheKey: string
22
- entries: {
23
- graphQlSchema: GraphQLSchema
24
- permissions: Acl.Permissions
25
- verifier: (identity: Identity) => boolean
26
- }[]
27
- }[] = []
27
+ private cache = new WeakMap<Schema, CacheEntry[]>()
28
28
 
29
29
  constructor(
30
30
  private readonly graphqlSchemaBuilderFactory: GraphQlSchemaBuilderFactory,
@@ -37,24 +37,17 @@ class GraphQlSchemaFactory {
37
37
  }
38
38
 
39
39
  public create(schema: Schema, identity: Identity): [GraphQLSchema, Acl.Permissions] {
40
+ let cacheEntries = this.cache.get(schema)
40
41
  const contributorsCacheKey = this.getContributorsCacheKey({ schema, identity })
41
- let schemaCacheEntry = this.cache.find(
42
- it => it.schema.model === schema.model && it.schema.acl === schema.acl && contributorsCacheKey === it.cacheKey,
43
- )
44
- if (!schemaCacheEntry) {
45
- schemaCacheEntry = {
46
- schema,
47
- cacheKey: contributorsCacheKey,
48
- entries: [],
49
- }
50
- this.cache.push(schemaCacheEntry)
51
- } else {
52
- const entry = schemaCacheEntry.entries.find(it => it.verifier(identity))
53
- if (entry) {
42
+ if (cacheEntries !== undefined) {
43
+ const entry = cacheEntries.find(it => it.contributorsCacheKey === contributorsCacheKey && it.verifier(identity))
44
+ if (entry !== undefined) {
54
45
  return [entry.graphQlSchema, entry.permissions]
55
46
  }
47
+ } else {
48
+ cacheEntries = []
49
+ this.cache.set(schema, cacheEntries)
56
50
  }
57
-
58
51
  const { permissions, verifier } = this.permissionFactory.createPermissions(schema, identity)
59
52
 
60
53
  const authorizator = new StaticAuthorizator(permissions)
@@ -83,7 +76,7 @@ class GraphQlSchemaFactory {
83
76
  Json: JSONType,
84
77
  },
85
78
  })
86
- schemaCacheEntry.entries.push({ graphQlSchema, verifier, permissions })
79
+ cacheEntries.push({ graphQlSchema, verifier, permissions, contributorsCacheKey })
87
80
 
88
81
  return [graphQlSchema, permissions]
89
82
  }
@@ -31,7 +31,9 @@ export const createGraphQLQueryHandler = <Context, KoaState>({
31
31
  listeners,
32
32
  }: FactoryArgs<Context, KoaState>): GraphQLQueryHandler<KoaState> => {
33
33
  let schemaValidated = false
34
- const documentCache = new LRUCache<string, DocumentNode>()
34
+ const documentCache = new LRUCache<string, DocumentNode>({
35
+ max: 200,
36
+ })
35
37
  return async ctx => {
36
38
  const listenersQueue = listeners
37
39
 
@@ -5,46 +5,7 @@ import {
5
5
  import accepts from 'accepts'
6
6
  import { get, KoaContext, KoaMiddleware, KoaRequestState } from '../koa'
7
7
 
8
- const sandboxTabs: PlaygroundRenderPageOptions['tabs'] = [
9
- {
10
- endpoint: '/content/sandbox/live',
11
- name: 'Create a post',
12
- variables: JSON.stringify({
13
- post: {
14
- title: 'Hello Contember',
15
- content:
16
- 'Contember is a headless, GraphQL-first platform for building custom web applications. Prototype yours in a day, launch it in a week and run it for years.',
17
- },
18
- }),
19
- query: `mutation($post: PostCreateInput!) {
20
- createPost(data: $post) {
21
- ok
22
- errorMessage
23
- node {
24
- id
25
- title
26
- }
27
- }
28
- }`,
29
- headers: {
30
- Authorization: 'Bearer 0000000000000000000000000000000000000000',
31
- },
32
- },
33
- {
34
- endpoint: '/content/sandbox/live',
35
- name: 'List posts',
36
- query: `query {
37
- listPost {
38
- title
39
- }
40
- }`,
41
- headers: {
42
- Authorization: 'Bearer 0000000000000000000000000000000000000000',
43
- },
44
- },
45
- ]
46
-
47
- export const createPlaygroundMiddleware = (sandbox = false): KoaMiddleware<KoaRequestState> => {
8
+ export const createPlaygroundMiddleware = (): KoaMiddleware<KoaRequestState> => {
48
9
  return get('/', (ctx: KoaContext<KoaRequestState>, next) => {
49
10
  const accept = accepts(ctx.req)
50
11
  const types = accept.types() as string[]
@@ -55,9 +16,6 @@ export const createPlaygroundMiddleware = (sandbox = false): KoaMiddleware<KoaRe
55
16
  const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
56
17
  endpoint: ctx.originalUrl,
57
18
  }
58
- if (sandbox) {
59
- playgroundRenderPageOptions.tabs = sandboxTabs
60
- }
61
19
 
62
20
  ctx.set('Content-Type', 'text/html')
63
21
  ctx.body = renderPlaygroundPage(playgroundRenderPageOptions)