@steedos/service-api 2.5.20-beta.8 → 2.5.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.
Files changed (2) hide show
  1. package/index.js +36 -7
  2. package/package.json +6 -6
package/index.js CHANGED
@@ -328,9 +328,9 @@ module.exports = {
328
328
  * @param {Object.<string, any>} args - Arguments passed to GraphQL child resolver
329
329
  * @returns {string} Key to the dataloader instance
330
330
  */
331
- getObjectDataLoaderMapKey(objectName, referenceToField = '_id') {
331
+ getObjectDataLoaderMapKey(objectName, referenceToField = '_id', spaceId) {
332
332
  if (objectName) {
333
- const key = `object:${objectName}.${referenceToField}`;
333
+ const key = `${spaceId}_object:${objectName}.${referenceToField}`;
334
334
  if(!ObjectDataLoaderMapKeys[objectName]){
335
335
  ObjectDataLoaderMapKeys[objectName] = [] ;
336
336
  }
@@ -345,9 +345,12 @@ module.exports = {
345
345
  async objectDataLoaderHandler(actionName, staticParams, rootParams, graphqlCtx) {
346
346
  const rootKeys = Object.keys(rootParams);
347
347
  const {root, args, context, resolveInfo} = graphqlCtx;
348
+ const userSession = context.ctx.meta.user;
349
+ const spaceId = userSession.spaceId;
348
350
  const dataLoaderMapKey = this.getObjectDataLoaderMapKey(
349
351
  staticParams.__objectName || staticParams.objectName
350
352
  , staticParams.referenceToField || '_id'
353
+ , spaceId
351
354
  );
352
355
  const objectDataLoaders = this.objectDataLoaders;
353
356
  // if a dataLoader batching parameter is specified, then all root params can be data loaded;
@@ -370,7 +373,8 @@ module.exports = {
370
373
  batchedParamKey,
371
374
  staticParams,
372
375
  args,
373
- { hashCacheKey: dataLoaderUseAllRootKeys } // must hash the cache key if not loading scalar
376
+ { hashCacheKey: dataLoaderUseAllRootKeys }, // must hash the cache key if not loading scalar
377
+ spaceId
374
378
  );
375
379
  objectDataLoaders.set(dataLoaderMapKey, dataLoader);
376
380
  }
@@ -465,10 +469,13 @@ module.exports = {
465
469
  if(useObjectDataLoader){
466
470
  return await this.objectDataLoaderHandler(actionName, staticParams, rootParams, {root, args, context, resolveInfo});
467
471
  }else if (useDataLoader) {
472
+ const userSession = context.ctx.meta.user;
473
+ const spaceId = userSession.spaceId;
468
474
  const dataLoaderMapKey = this.getDataLoaderMapKey(
469
475
  actionName,
470
476
  staticParams,
471
- args
477
+ args,
478
+ spaceId
472
479
  );
473
480
  // if a dataLoader batching parameter is specified, then all root params can be data loaded;
474
481
  // otherwise use only the primary rootParam
@@ -490,7 +497,8 @@ module.exports = {
490
497
  batchedParamKey,
491
498
  staticParams,
492
499
  args,
493
- { hashCacheKey: dataLoaderUseAllRootKeys } // must hash the cache key if not loading scalar
500
+ { hashCacheKey: dataLoaderUseAllRootKeys }, // must hash the cache key if not loading scalar
501
+ spaceId
494
502
  );
495
503
  context.dataLoaders.set(dataLoaderMapKey, dataLoader);
496
504
  }
@@ -903,6 +911,25 @@ module.exports = {
903
911
  }
904
912
  },
905
913
 
914
+ /**
915
+ * Get the unique key assigned to the DataLoader map
916
+ * @param {string} actionName - Fully qualified action name to bind to dataloader
917
+ * @param {Object.<string, any>} staticParams - Static parameters to use in dataloader
918
+ * @param {Object.<string, any>} args - Arguments passed to GraphQL child resolver
919
+ * @returns {string} Key to the dataloader instance
920
+ */
921
+ getDataLoaderMapKey(actionName, staticParams, args, spaceId) {
922
+ if (Object.keys(staticParams).length > 0 || Object.keys(args).length > 0) {
923
+ // create a unique hash of the static params and the arguments to ensure a unique DataLoader instance
924
+ const actionParams = _.defaultsDeep({}, args, staticParams);
925
+ const paramsHash = hash(actionParams);
926
+ return `${spaceId}_${actionName}:${paramsHash}`;
927
+ }
928
+
929
+ // if no static params or arguments are present then the action name can serve as the key
930
+ return actionName;
931
+ },
932
+
906
933
  /**
907
934
  * Build a DataLoader instance
908
935
  *
@@ -921,11 +948,13 @@ module.exports = {
921
948
  batchedParamKey,
922
949
  staticParams,
923
950
  args,
924
- { hashCacheKey = false } = {}
951
+ { hashCacheKey = false } = {},
952
+ spaceId
925
953
  ) {
926
954
  const batchLoadFn = keys => {
927
955
  const rootParams = { [batchedParamKey]: keys };
928
- return ctx.call(actionName, _.defaultsDeep({}, args, rootParams, staticParams));
956
+ // !!! 这里的ctx是buildDataLoader时传进来的参数,在后续触发执行batchLoadFn时ctx.meta中的信息依然是旧的,故此函数体中代码不要使用ctx.meta中的信息
957
+ return ctx.call(actionName, _.defaultsDeep({}, args, rootParams, staticParams), { meta: { ...ctx.meta, spaceId } });
929
958
  };
930
959
 
931
960
  const dataLoaderOptions = this.dataLoaderOptions.get(actionName) || {};
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@steedos/service-api",
3
- "version": "2.5.20-beta.8",
3
+ "version": "2.5.20",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
7
- "@steedos/auth": "2.5.20-beta.8",
8
- "@steedos/router": "2.5.20-beta.8",
9
- "@steedos/service-object-graphql": "2.5.20-beta.8",
10
- "@steedos/service-ui": "2.5.20-beta.8",
7
+ "@steedos/auth": "2.5.20",
8
+ "@steedos/router": "2.5.20",
9
+ "@steedos/service-object-graphql": "2.5.20",
10
+ "@steedos/service-ui": "2.5.20",
11
11
  "graphql": "^15.8.0",
12
12
  "graphql-iso-date": "^3.6.1",
13
13
  "graphql-type-json": "^0.3.2",
@@ -22,7 +22,7 @@
22
22
  "publishConfig": {
23
23
  "access": "public"
24
24
  },
25
- "gitHead": "9501bbf23858aa74a7bb9dcf53f3c7ce4dc765b1",
25
+ "gitHead": "1e2c9a88c8eeddc492481195d2d8c5e3519680e8",
26
26
  "devDependencies": {
27
27
  "@types/graphql-iso-date": "^3.4.0",
28
28
  "@types/react-dev-utils": "^9.0.11"