@graphql-mesh/merger-bare 0.12.9 → 0.13.0-alpha-49a0ce051.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.
Files changed (4) hide show
  1. package/index.d.ts +2 -2
  2. package/index.js +32 -86
  3. package/index.mjs +33 -87
  4. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { MeshMerger, MeshMergerContext, MeshMergerOptions } from '@graphql-mesh/types';
2
+ import { GraphQLSchema } from 'graphql';
2
3
  export default class BareMerger implements MeshMerger {
3
4
  name: string;
4
5
  private logger;
5
6
  constructor(options: MeshMergerOptions);
6
- handleSingleSource({ rawSources, typeDefs, resolvers, transforms }: MeshMergerContext): import("graphql").GraphQLSchema;
7
- getUnifiedSchema(mergerContext: MeshMergerContext): Promise<import("graphql").GraphQLSchema>;
7
+ getUnifiedSchema({ rawSources, typeDefs, resolvers, transforms }: MeshMergerContext): Promise<GraphQLSchema>;
8
8
  }
package/index.js CHANGED
@@ -3,114 +3,60 @@
3
3
  const wrap = require('@graphql-tools/wrap');
4
4
  const utils = require('@graphql-mesh/utils');
5
5
  const schema = require('@graphql-tools/schema');
6
- const utils$1 = require('@graphql-tools/utils');
7
- const graphql = require('graphql');
8
6
 
9
7
  class BareMerger {
10
8
  constructor(options) {
11
9
  this.name = 'bare';
12
10
  this.logger = options.logger;
13
11
  }
14
- handleSingleSource({ rawSources, typeDefs, resolvers, transforms }) {
15
- const [source] = rawSources;
16
- let schema$1 = source.schema;
17
- let wrapTransforms = [];
18
- let noWrapTransforms = [];
19
- if (transforms === null || transforms === void 0 ? void 0 : transforms.length) {
20
- const transformGroups = utils.groupTransforms(transforms);
21
- wrapTransforms = transformGroups.wrapTransforms;
22
- noWrapTransforms = transformGroups.noWrapTransforms;
23
- }
24
- if (source.executor || source.transforms.length) {
25
- const firstRoundTransforms = [...source.transforms];
26
- if (!typeDefs && !resolvers) {
27
- firstRoundTransforms.push(...wrapTransforms, ...noWrapTransforms);
28
- }
29
- schema$1 = wrap.wrapSchema({
30
- ...source,
31
- schema: schema$1,
32
- transforms: firstRoundTransforms,
33
- batch: true,
34
- });
35
- }
36
- if (typeDefs || resolvers) {
37
- this.logger.debug(() => `Applying additionalTypeDefs`);
38
- typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.forEach(typeDef => {
39
- schema$1 = graphql.extendSchema(schema$1, typeDef);
40
- });
41
- if (resolvers) {
42
- this.logger.debug(() => `Applying additionalResolvers`);
43
- for (const resolversObj of utils$1.asArray(resolvers)) {
44
- schema$1 = schema.addResolversToSchema({
45
- schema: schema$1,
46
- resolvers: resolversObj,
47
- updateResolversInPlace: true,
48
- });
49
- }
50
- }
51
- if (wrapTransforms.length) {
52
- schema$1 = wrap.wrapSchema({
53
- schema: schema$1,
54
- transforms: [...wrapTransforms, ...noWrapTransforms],
55
- batch: true,
56
- });
57
- }
58
- else if (noWrapTransforms.length) {
59
- schema$1 = utils.applySchemaTransforms(schema$1, undefined, schema$1, noWrapTransforms);
60
- }
61
- }
62
- schema$1.extensions = schema$1.extensions || {};
63
- Object.defineProperty(schema$1.extensions, 'sourceMap', {
64
- get: () => new Map([[source, schema$1]]),
65
- });
66
- return schema$1;
67
- }
68
- async getUnifiedSchema(mergerContext) {
69
- if (mergerContext.rawSources.length === 1) {
70
- return this.handleSingleSource(mergerContext);
71
- }
72
- const { rawSources, typeDefs, resolvers, transforms } = mergerContext;
12
+ async getUnifiedSchema({ rawSources, typeDefs, resolvers, transforms }) {
73
13
  const sourceMap = new Map();
14
+ this.logger.debug(() => `Applying transforms for each source`);
74
15
  const schemas = rawSources.map(source => {
75
16
  let schema = source.schema;
76
- if (source.executor || source.transforms.length) {
17
+ let sourceLevelSchema = source.schema;
18
+ const handlerLevelTransformGroups = utils.groupTransforms(source.transforms || []);
19
+ if (handlerLevelTransformGroups.wrapTransforms.length > 0 || source.executor) {
77
20
  schema = wrap.wrapSchema({
21
+ batch: true,
78
22
  ...source,
79
23
  schema,
80
- transforms: source.transforms,
81
- batch: true,
82
24
  });
83
25
  }
84
- sourceMap.set(source, schema);
26
+ else {
27
+ schema = utils.applySchemaTransforms(schema, undefined, schema, handlerLevelTransformGroups.noWrapTransforms);
28
+ }
29
+ // After that step, it will be considered as root level schema
30
+ sourceLevelSchema = schema;
31
+ sourceMap.set(source, sourceLevelSchema);
85
32
  return schema;
86
33
  });
87
- let schema$1 = schema.mergeSchemas({
88
- schemas,
89
- typeDefs,
90
- resolvers,
91
- });
92
- let wrapTransforms = [];
93
- let noWrapTransforms = [];
94
- if (transforms === null || transforms === void 0 ? void 0 : transforms.length) {
95
- const transformGroups = utils.groupTransforms(transforms);
96
- wrapTransforms = transformGroups.wrapTransforms;
97
- noWrapTransforms = transformGroups.noWrapTransforms;
98
- }
99
- if (wrapTransforms.length) {
100
- schema$1 = wrap.wrapSchema({
101
- schema: schema$1,
102
- transforms: [...wrapTransforms, ...noWrapTransforms],
34
+ this.logger.debug(() => `Merging sources`);
35
+ let unifiedSchema = schemas.length === 1 && !(typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.length) && !resolvers
36
+ ? schemas[0]
37
+ : schema.mergeSchemas({
38
+ schemas,
39
+ typeDefs,
40
+ resolvers,
41
+ });
42
+ this.logger.debug(() => `Handling root level transforms`);
43
+ const rootLevelTransformGroups = utils.groupTransforms(transforms || []);
44
+ if (rootLevelTransformGroups.wrapTransforms.length > 0) {
45
+ unifiedSchema = wrap.wrapSchema({
46
+ schema: unifiedSchema,
103
47
  batch: true,
48
+ transforms,
104
49
  });
105
50
  }
106
- else if (noWrapTransforms.length) {
107
- schema$1 = utils.applySchemaTransforms(schema$1, undefined, schema$1, noWrapTransforms);
51
+ else {
52
+ unifiedSchema = utils.applySchemaTransforms(unifiedSchema, undefined, unifiedSchema, rootLevelTransformGroups.noWrapTransforms);
108
53
  }
109
- schema$1.extensions = schema$1.extensions || {};
110
- Object.defineProperty(schema$1.extensions, 'sourceMap', {
54
+ this.logger.debug(() => `Attaching sources to the unified schema`);
55
+ unifiedSchema.extensions = unifiedSchema.extensions || {};
56
+ Object.defineProperty(unifiedSchema.extensions, 'sourceMap', {
111
57
  get: () => sourceMap,
112
58
  });
113
- return schema$1;
59
+ return unifiedSchema;
114
60
  }
115
61
  }
116
62
 
package/index.mjs CHANGED
@@ -1,114 +1,60 @@
1
1
  import { wrapSchema } from '@graphql-tools/wrap';
2
2
  import { groupTransforms, applySchemaTransforms } from '@graphql-mesh/utils';
3
- import { addResolversToSchema, mergeSchemas } from '@graphql-tools/schema';
4
- import { asArray } from '@graphql-tools/utils';
5
- import { extendSchema } from 'graphql';
3
+ import { mergeSchemas } from '@graphql-tools/schema';
6
4
 
7
5
  class BareMerger {
8
6
  constructor(options) {
9
7
  this.name = 'bare';
10
8
  this.logger = options.logger;
11
9
  }
12
- handleSingleSource({ rawSources, typeDefs, resolvers, transforms }) {
13
- const [source] = rawSources;
14
- let schema = source.schema;
15
- let wrapTransforms = [];
16
- let noWrapTransforms = [];
17
- if (transforms === null || transforms === void 0 ? void 0 : transforms.length) {
18
- const transformGroups = groupTransforms(transforms);
19
- wrapTransforms = transformGroups.wrapTransforms;
20
- noWrapTransforms = transformGroups.noWrapTransforms;
21
- }
22
- if (source.executor || source.transforms.length) {
23
- const firstRoundTransforms = [...source.transforms];
24
- if (!typeDefs && !resolvers) {
25
- firstRoundTransforms.push(...wrapTransforms, ...noWrapTransforms);
26
- }
27
- schema = wrapSchema({
28
- ...source,
29
- schema,
30
- transforms: firstRoundTransforms,
31
- batch: true,
32
- });
33
- }
34
- if (typeDefs || resolvers) {
35
- this.logger.debug(() => `Applying additionalTypeDefs`);
36
- typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.forEach(typeDef => {
37
- schema = extendSchema(schema, typeDef);
38
- });
39
- if (resolvers) {
40
- this.logger.debug(() => `Applying additionalResolvers`);
41
- for (const resolversObj of asArray(resolvers)) {
42
- schema = addResolversToSchema({
43
- schema,
44
- resolvers: resolversObj,
45
- updateResolversInPlace: true,
46
- });
47
- }
48
- }
49
- if (wrapTransforms.length) {
50
- schema = wrapSchema({
51
- schema,
52
- transforms: [...wrapTransforms, ...noWrapTransforms],
53
- batch: true,
54
- });
55
- }
56
- else if (noWrapTransforms.length) {
57
- schema = applySchemaTransforms(schema, undefined, schema, noWrapTransforms);
58
- }
59
- }
60
- schema.extensions = schema.extensions || {};
61
- Object.defineProperty(schema.extensions, 'sourceMap', {
62
- get: () => new Map([[source, schema]]),
63
- });
64
- return schema;
65
- }
66
- async getUnifiedSchema(mergerContext) {
67
- if (mergerContext.rawSources.length === 1) {
68
- return this.handleSingleSource(mergerContext);
69
- }
70
- const { rawSources, typeDefs, resolvers, transforms } = mergerContext;
10
+ async getUnifiedSchema({ rawSources, typeDefs, resolvers, transforms }) {
71
11
  const sourceMap = new Map();
12
+ this.logger.debug(() => `Applying transforms for each source`);
72
13
  const schemas = rawSources.map(source => {
73
14
  let schema = source.schema;
74
- if (source.executor || source.transforms.length) {
15
+ let sourceLevelSchema = source.schema;
16
+ const handlerLevelTransformGroups = groupTransforms(source.transforms || []);
17
+ if (handlerLevelTransformGroups.wrapTransforms.length > 0 || source.executor) {
75
18
  schema = wrapSchema({
19
+ batch: true,
76
20
  ...source,
77
21
  schema,
78
- transforms: source.transforms,
79
- batch: true,
80
22
  });
81
23
  }
82
- sourceMap.set(source, schema);
24
+ else {
25
+ schema = applySchemaTransforms(schema, undefined, schema, handlerLevelTransformGroups.noWrapTransforms);
26
+ }
27
+ // After that step, it will be considered as root level schema
28
+ sourceLevelSchema = schema;
29
+ sourceMap.set(source, sourceLevelSchema);
83
30
  return schema;
84
31
  });
85
- let schema = mergeSchemas({
86
- schemas,
87
- typeDefs,
88
- resolvers,
89
- });
90
- let wrapTransforms = [];
91
- let noWrapTransforms = [];
92
- if (transforms === null || transforms === void 0 ? void 0 : transforms.length) {
93
- const transformGroups = groupTransforms(transforms);
94
- wrapTransforms = transformGroups.wrapTransforms;
95
- noWrapTransforms = transformGroups.noWrapTransforms;
96
- }
97
- if (wrapTransforms.length) {
98
- schema = wrapSchema({
99
- schema,
100
- transforms: [...wrapTransforms, ...noWrapTransforms],
32
+ this.logger.debug(() => `Merging sources`);
33
+ let unifiedSchema = schemas.length === 1 && !(typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.length) && !resolvers
34
+ ? schemas[0]
35
+ : mergeSchemas({
36
+ schemas,
37
+ typeDefs,
38
+ resolvers,
39
+ });
40
+ this.logger.debug(() => `Handling root level transforms`);
41
+ const rootLevelTransformGroups = groupTransforms(transforms || []);
42
+ if (rootLevelTransformGroups.wrapTransforms.length > 0) {
43
+ unifiedSchema = wrapSchema({
44
+ schema: unifiedSchema,
101
45
  batch: true,
46
+ transforms,
102
47
  });
103
48
  }
104
- else if (noWrapTransforms.length) {
105
- schema = applySchemaTransforms(schema, undefined, schema, noWrapTransforms);
49
+ else {
50
+ unifiedSchema = applySchemaTransforms(unifiedSchema, undefined, unifiedSchema, rootLevelTransformGroups.noWrapTransforms);
106
51
  }
107
- schema.extensions = schema.extensions || {};
108
- Object.defineProperty(schema.extensions, 'sourceMap', {
52
+ this.logger.debug(() => `Attaching sources to the unified schema`);
53
+ unifiedSchema.extensions = unifiedSchema.extensions || {};
54
+ Object.defineProperty(unifiedSchema.extensions, 'sourceMap', {
109
55
  get: () => sourceMap,
110
56
  });
111
- return schema;
57
+ return unifiedSchema;
112
58
  }
113
59
  }
114
60
 
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@graphql-mesh/merger-bare",
3
- "version": "0.12.9",
3
+ "version": "0.13.0-alpha-49a0ce051.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "*"
7
7
  },
8
8
  "dependencies": {
9
9
  "@graphql-mesh/types": "0.57.1",
10
- "@graphql-mesh/utils": "0.25.0",
10
+ "@graphql-mesh/utils": "0.26.0-alpha-49a0ce051.0",
11
11
  "@graphql-tools/schema": "8.3.1",
12
12
  "@graphql-tools/utils": "8.6.0",
13
- "@graphql-tools/wrap": "8.3.2",
13
+ "@graphql-tools/wrap": "8.3.3",
14
14
  "fetchache": "0.1.1"
15
15
  },
16
16
  "license": "MIT",