@kiwano/core 1.2.0 → 1.4.0

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.
@@ -38,8 +38,8 @@ export declare class MultiPlugin implements Plugin {
38
38
  afterFinalizeArgument(builder: ArgumentBuilder, context: FinalizeContext, info: ArgumentBuilderInfo): Promise<void>;
39
39
  beforeBuild(rootBuilder: SchemaBuilder): void;
40
40
  afterBuild(rootBuilder: SchemaBuilder, schema: GraphQLSchema): void;
41
- beforeBuildSchema(builder: SchemaBuilder): void;
42
- afterBuildSchema(builder: SchemaBuilder, schema: GraphQLSchema): void;
41
+ beforeBuildSchema(builder: SchemaBuilder, rootBuilder: SchemaBuilder): void;
42
+ afterBuildSchema(builder: SchemaBuilder, schema: GraphQLSchema, rootBuilder: SchemaBuilder): void;
43
43
  beforeBuildObjectType(builder: ObjectTypeBuilder, context: BuildContext, info: ObjectTypeBuilderInfo): void;
44
44
  afterBuildObjectType(builder: ObjectTypeBuilder, context: BuildContext, info: ObjectTypeBuilderInfo, objectType: GraphQLObjectType): void;
45
45
  beforeBuildInputObjectType(builder: InputObjectTypeBuilder, context: BuildContext, info: InputObjectTypeBuilderInfo): void;
@@ -88,11 +88,11 @@ class MultiPlugin {
88
88
  afterBuild(rootBuilder, schema) {
89
89
  this.executeSync('afterBuild', plugin => plugin.afterBuild(rootBuilder, schema));
90
90
  }
91
- beforeBuildSchema(builder) {
92
- this.executeSync('beforeBuildSchema', plugin => plugin.beforeBuildSchema(builder));
91
+ beforeBuildSchema(builder, rootBuilder) {
92
+ this.executeSync('beforeBuildSchema', plugin => plugin.beforeBuildSchema(builder, rootBuilder));
93
93
  }
94
- afterBuildSchema(builder, schema) {
95
- this.executeSync('afterBuildSchema', plugin => plugin.afterBuildSchema(builder, schema));
94
+ afterBuildSchema(builder, schema, rootBuilder) {
95
+ this.executeSync('afterBuildSchema', plugin => plugin.afterBuildSchema(builder, schema, rootBuilder));
96
96
  }
97
97
  beforeBuildObjectType(builder, context, info) {
98
98
  this.executeSync('beforeBuildObjectType', plugin => plugin.beforeBuildObjectType(builder, context, info));
@@ -43,8 +43,8 @@ export declare class AclPlugin implements Plugin {
43
43
  middleware(config?: AclValidateConfigType): (req: any, res: any, next: any) => void;
44
44
  rolePath(path: string): this;
45
45
  onForbidden(handler: (resource: string) => void): this;
46
- beforeBuildSchema(builder: SchemaBuilder): void;
47
- afterBuildSchema(builder: SchemaBuilder, schema: GraphQLSchema): void;
46
+ beforeBuildSchema(builder: SchemaBuilder, rootBuilder: SchemaBuilder): void;
47
+ afterBuildSchema(builder: SchemaBuilder, schema: GraphQLSchema, rootBuilder: SchemaBuilder): void;
48
48
  protected addRules(resource: string, allowed: Set<string>, denied: Set<string>): void;
49
49
  protected _findRules(resource: string, collection: Map<string, AclRuleConfig>): AclRuleConfig[];
50
50
  protected _learnRoles(roles: Set<AclRoleIdentifier>): void;
@@ -129,18 +129,19 @@ class AclPlugin {
129
129
  this._options.onForbidden = handler;
130
130
  return this;
131
131
  }
132
- beforeBuildSchema(builder) {
132
+ beforeBuildSchema(builder, rootBuilder) {
133
133
  // Add middleware
134
- builder.use((0, middleware_1.graphQLAclMiddleware)(this, null, this._options));
134
+ builder.use((0, middleware_1.graphQLAclMiddleware)(this, rootBuilder.name || rootBuilder.tag.toString(), this._options));
135
135
  }
136
- afterBuildSchema(builder, schema) {
136
+ afterBuildSchema(builder, schema, rootBuilder) {
137
137
  // Add rules
138
+ const schemaId = rootBuilder.name || rootBuilder.tag.toString();
138
139
  for (let type of builder.getObjectTypes()) {
139
140
  const typeInfo = type.info();
140
- this.addRules(`${type.name}.*`, typeInfo.allowedRoles, typeInfo.deniedRoles);
141
+ this.addRules(`${schemaId}:${type.name}.*`, typeInfo.allowedRoles, typeInfo.deniedRoles);
141
142
  for (let field of type.info().fields) {
142
143
  const fieldInfo = field.info();
143
- this.addRules(`${type.name}.${field.name}`, fieldInfo.allowedRoles, fieldInfo.deniedRoles);
144
+ this.addRules(`${schemaId}:${type.name}.${field.name}`, fieldInfo.allowedRoles, fieldInfo.deniedRoles);
144
145
  }
145
146
  }
146
147
  }
@@ -7,5 +7,5 @@ export interface AclMiddlewareOptions {
7
7
  }
8
8
  export declare const defaultAclMiddlewareOptions: AclMiddlewareOptions;
9
9
  export declare function expressAclMiddleware(acl: AclPlugin, config?: AclValidateConfigType, options?: AclMiddlewareOptions): (req: any, res: any, next: any) => void;
10
- export declare function graphQLAclMiddleware(acl: AclPlugin, config?: AclValidateConfigType, options?: AclMiddlewareOptions): Middleware;
10
+ export declare function graphQLAclMiddleware(acl: AclPlugin, schemaName: string, options?: AclMiddlewareOptions): Middleware;
11
11
  export declare function getPathResource(path: Path): string;
@@ -24,15 +24,12 @@ function expressAclMiddleware(acl, config = null, options = null) {
24
24
  };
25
25
  }
26
26
  exports.expressAclMiddleware = expressAclMiddleware;
27
- function graphQLAclMiddleware(acl, config = null, options = null) {
27
+ function graphQLAclMiddleware(acl, schemaName, options = null) {
28
28
  const fullOptions = getOptions(options);
29
29
  return (resolve, root, args, context, info) => {
30
30
  var _a;
31
31
  const pathResource = getPathResource(info.path);
32
- let parsedConfig = config;
33
- if (!config) {
34
- parsedConfig = { resource: pathResource };
35
- }
32
+ let parsedConfig = { resource: `${schemaName}:${pathResource}` };
36
33
  const role = (_a = (0, lodash_1.get)(context, fullOptions.rolePath)) !== null && _a !== void 0 ? _a : null;
37
34
  const allowed = acl.validate(parsedConfig, role);
38
35
  if (!allowed) {
@@ -32,8 +32,8 @@ export interface Plugin {
32
32
  afterFinalizeArgument?(builder: ArgumentBuilder, context: FinalizeContext, info: ArgumentBuilderInfo): OptionalPromise;
33
33
  beforeBuild?(rootBuilder: AbstractSchemaBuilder<any>): any;
34
34
  afterBuild?(rootBuilder: AbstractSchemaBuilder<any>, schema: GraphQLSchema): any;
35
- beforeBuildSchema?(builder: AbstractSchemaBuilder<any>): any;
36
- afterBuildSchema?(builder: AbstractSchemaBuilder<any>, schema: GraphQLSchema): any;
35
+ beforeBuildSchema?(builder: AbstractSchemaBuilder<any>, rootBuilder: AbstractSchemaBuilder<any>): any;
36
+ afterBuildSchema?(builder: AbstractSchemaBuilder<any>, schema: GraphQLSchema, rootBuilder: AbstractSchemaBuilder<any>): any;
37
37
  beforeBuildObjectType?(builder: ObjectTypeBuilder, context: BuildContext, info: ObjectTypeBuilderInfo): any;
38
38
  afterBuildObjectType?(builder: ObjectTypeBuilder, context: BuildContext, info: ObjectTypeBuilderInfo, objectType: GraphQLObjectType): any;
39
39
  beforeBuildInputObjectType?(builder: InputObjectTypeBuilder, context: BuildContext, info: InputObjectTypeBuilderInfo): any;
package/dist/schema.d.ts CHANGED
@@ -12,6 +12,7 @@ import { Plugin } from "./plugin";
12
12
  import { Configurator, Middleware, OptionalPromise } from "./common";
13
13
  export declare abstract class AbstractSchemaBuilder<NS extends NamingStrategy> {
14
14
  protected _name?: string;
15
+ protected _tag: number;
15
16
  protected _plugins: Plugin[];
16
17
  protected _middleware: Middleware[];
17
18
  protected _subSchemas: AbstractSchemaBuilder<any>[];
@@ -90,6 +91,7 @@ export declare abstract class AbstractSchemaBuilder<NS extends NamingStrategy> {
90
91
  hasType(name: string, deep?: boolean): boolean;
91
92
  findResolver(typeName: string, fieldName: string): GraphQLFieldResolver<any, any>;
92
93
  get name(): string;
94
+ get tag(): number;
93
95
  finalize(rootSchema?: AbstractSchemaBuilder<any>): Promise<void>;
94
96
  finalizeSchema(): Promise<void>;
95
97
  build(): Promise<GraphQLSchema>;
package/dist/schema.js CHANGED
@@ -30,6 +30,7 @@ class AbstractSchemaBuilder {
30
30
  this._deniedMutationRoles = new Set();
31
31
  this._queryObject = new objectType_1.ObjectTypeBuilder('Query');
32
32
  this._name = name;
33
+ this._tag = Math.round(Math.random() * 1000000);
33
34
  this._addDefaultScalars();
34
35
  }
35
36
  naming(strategy) {
@@ -227,6 +228,9 @@ class AbstractSchemaBuilder {
227
228
  get name() {
228
229
  return (0, Builder_1.resolveName)(this._name);
229
230
  }
231
+ get tag() {
232
+ return this._tag;
233
+ }
230
234
  async finalize(rootSchema) {
231
235
  const resolvedRootSchema = rootSchema || this;
232
236
  // Assign naming strategy to sub schemas as default
@@ -263,15 +267,19 @@ class AbstractSchemaBuilder {
263
267
  this._plugins.forEach(plugin => enumType.use(plugin));
264
268
  }
265
269
  // Apply rules
266
- for (let objectType of this.getObjectTypes()) {
270
+ for (let objectType of Array.from(this._objectTypes.values())) {
267
271
  objectType.allow(...Array.from(this._allowedRoles)).deny(...Array.from(this._deniedRoles));
268
272
  }
273
+ const fullAllowedQueryRoles = [...Array.from(this._allowedRoles), ...Array.from(this._allowedQueryRoles)];
274
+ const fullDeniedQueryRoles = [...Array.from(this._deniedRoles), ...Array.from(this._deniedQueryRoles)];
269
275
  for (let queryField of this._queryObject.info().fields) {
270
- queryField.allow(...Array.from(this._allowedQueryRoles)).deny(...Array.from(this._deniedQueryRoles));
276
+ queryField.allow(...fullAllowedQueryRoles).deny(...fullDeniedQueryRoles);
271
277
  }
272
278
  if (this._mutationObject) {
279
+ const fullAllowedMutationRoles = [...Array.from(this._allowedRoles), ...Array.from(this._allowedMutationRoles)];
280
+ const fullDeniedMutationRoles = [...Array.from(this._deniedRoles), ...Array.from(this._deniedMutationRoles)];
273
281
  for (let mutationField of this._mutationObject.info().fields) {
274
- mutationField.allow(...Array.from(this._allowedMutationRoles)).deny(...Array.from(this._deniedMutationRoles));
282
+ mutationField.allow(...fullAllowedMutationRoles).deny(...fullDeniedMutationRoles);
275
283
  }
276
284
  }
277
285
  // Finalize types
@@ -305,8 +313,8 @@ class AbstractSchemaBuilder {
305
313
  return schema;
306
314
  }
307
315
  async buildSchema(resolvedTypes, rootSchema) {
308
- this._executePluginsSync('beforeBuildSchema', plugin => plugin.beforeBuildSchema(this));
309
316
  const resolvedRootSchema = rootSchema || this;
317
+ this._executePluginsSync('beforeBuildSchema', plugin => plugin.beforeBuildSchema(this, resolvedRootSchema));
310
318
  const context = new Builder_1.BuildContext(this, resolvedRootSchema, resolvedTypes);
311
319
  // Schema
312
320
  const schemaConfig = Object.assign(Object.assign({}, (this._customConfig || {})), { query: this._queryObject.build(context) });
@@ -324,7 +332,7 @@ class AbstractSchemaBuilder {
324
332
  const mergedSchema = (0, schema_1.mergeSchemas)({
325
333
  schemas: [fullSchema, ...builtSubSchemas]
326
334
  });
327
- this._executePluginsSync('afterBuildSchema', plugin => plugin.afterBuildSchema(this, mergedSchema));
335
+ this._executePluginsSync('afterBuildSchema', plugin => plugin.afterBuildSchema(this, mergedSchema, resolvedRootSchema));
328
336
  return mergedSchema;
329
337
  }
330
338
  get compiledResolvers() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiwano/core",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -31,19 +31,18 @@
31
31
  "access": "public"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/lodash": "^4.14.197",
35
- "graphql": "^16.8.0",
36
- "rimraf": "^5.0.1",
37
- "typescript": "^5.1.6"
34
+ "@types/lodash": "^4.17.0",
35
+ "graphql": "^16.8.1",
36
+ "rimraf": "^5.0.5",
37
+ "typescript": "^5.4.3"
38
38
  },
39
39
  "dependencies": {
40
- "@graphql-tools/schema": "^10.0.0",
40
+ "@graphql-tools/schema": "^10.0.3",
41
41
  "graphql-middleware": "^6.1.35",
42
42
  "lodash": "^4.17.21",
43
43
  "pluralize": "^8.0.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
47
- },
48
- "gitHead": "8c06d628d7cfaddff14bbd8fc08fefb1d9be0118"
47
+ }
49
48
  }