@graphql-mesh/fusion-composition 0.0.1-alpha-20231115135952-ce851df6a

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/cjs/index.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRenameFieldTransform = exports.createRenameTypeTransform = exports.composeSubgraphs = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const schema_1 = require("@graphql-tools/schema");
6
+ const utils_1 = require("@graphql-tools/utils");
7
+ const defaultRootTypeNames = {
8
+ query: 'Query',
9
+ mutation: 'Mutation',
10
+ subscription: 'Subscription',
11
+ };
12
+ function composeSubgraphs(subgraphs) {
13
+ const annotatedSubgraphs = [];
14
+ for (const subgraphConfig of subgraphs) {
15
+ const { name: subgraphName, schema, transforms } = subgraphConfig;
16
+ const rootTypeMap = (0, utils_1.getRootTypeMap)(schema);
17
+ const typeToOperationType = new Map();
18
+ for (const [operationType, rootType] of rootTypeMap) {
19
+ typeToOperationType.set(rootType.name, operationType);
20
+ }
21
+ const annotatedSubgraph = (0, utils_1.mapSchema)(schema, {
22
+ [utils_1.MapperKind.TYPE]: type => {
23
+ if ((0, graphql_1.isSpecifiedScalarType)(type)) {
24
+ return type;
25
+ }
26
+ const operationType = typeToOperationType.get(type.name);
27
+ if (operationType) {
28
+ return new (Object.getPrototypeOf(type).constructor)({
29
+ ...type.toConfig(),
30
+ name: defaultRootTypeNames[operationType],
31
+ });
32
+ }
33
+ return new (Object.getPrototypeOf(type).constructor)({
34
+ ...type.toConfig(),
35
+ extensions: {
36
+ ...type.extensions,
37
+ directives: {
38
+ ...(type.extensions?.directives || {}),
39
+ source: {
40
+ subgraph: subgraphName,
41
+ name: type.name,
42
+ },
43
+ },
44
+ },
45
+ });
46
+ },
47
+ [utils_1.MapperKind.FIELD]: (fieldConfig, fieldName) => ({
48
+ ...fieldConfig,
49
+ extensions: {
50
+ ...fieldConfig.extensions,
51
+ directives: {
52
+ ...(fieldConfig.extensions?.directives || {}),
53
+ source: {
54
+ subgraph: subgraphName,
55
+ name: fieldName,
56
+ },
57
+ },
58
+ },
59
+ }),
60
+ [utils_1.MapperKind.ENUM_VALUE]: (valueConfig, _typeName, _schema, externalValue) => ({
61
+ ...valueConfig,
62
+ extensions: {
63
+ ...valueConfig.extensions,
64
+ directives: {
65
+ ...(valueConfig.extensions?.directives || {}),
66
+ source: {
67
+ subgraph: subgraphName,
68
+ name: externalValue,
69
+ },
70
+ },
71
+ },
72
+ }),
73
+ [utils_1.MapperKind.ROOT_FIELD]: (fieldConfig, fieldName, typeName) => {
74
+ const operationType = typeToOperationType.get(typeName);
75
+ const operationName = operationType === 'query' ? fieldName : `${operationType}${fieldName}`;
76
+ const variableDefinitions = [];
77
+ const rootFieldArgs = [];
78
+ if (fieldConfig.args) {
79
+ for (const argName in fieldConfig.args) {
80
+ variableDefinitions.push(`$${argName}: ${fieldConfig.args[argName].type}`);
81
+ rootFieldArgs.push(`${argName}: $${argName}`);
82
+ }
83
+ }
84
+ const variableDefinitionsString = variableDefinitions.length
85
+ ? `(${variableDefinitions.join(', ')})`
86
+ : '';
87
+ const rootFieldArgsString = rootFieldArgs.length ? `(${rootFieldArgs.join(', ')})` : '';
88
+ const operationString = `${operationType} ${operationName}${variableDefinitionsString} { ${fieldName}${rootFieldArgsString} }`;
89
+ return {
90
+ ...fieldConfig,
91
+ extensions: {
92
+ ...fieldConfig.extensions,
93
+ directives: {
94
+ ...(fieldConfig.extensions?.directives || {}),
95
+ resolver: {
96
+ subgraph: subgraphName,
97
+ operation: operationString,
98
+ },
99
+ },
100
+ },
101
+ };
102
+ },
103
+ });
104
+ let transformedSubgraph = annotatedSubgraph;
105
+ if (transforms?.length) {
106
+ for (const transform of transforms) {
107
+ transformedSubgraph = transform(transformedSubgraph, subgraphConfig);
108
+ }
109
+ }
110
+ annotatedSubgraphs.push(transformedSubgraph);
111
+ }
112
+ return (0, schema_1.mergeSchemas)({
113
+ schemas: annotatedSubgraphs,
114
+ assumeValidSDL: true,
115
+ assumeValid: true,
116
+ });
117
+ }
118
+ exports.composeSubgraphs = composeSubgraphs;
119
+ function createRenameTypeTransform(renameFn, kind = utils_1.MapperKind.TYPE) {
120
+ return function renameTypeTransform(schema, subgraphConfig) {
121
+ return (0, utils_1.mapSchema)(schema, {
122
+ [kind]: (type) => (0, graphql_1.isSpecifiedScalarType)(type)
123
+ ? type
124
+ : new (Object.getPrototypeOf(type).constructor)({
125
+ ...type.toConfig(),
126
+ name: renameFn(type, subgraphConfig) || type.name,
127
+ }),
128
+ });
129
+ };
130
+ }
131
+ exports.createRenameTypeTransform = createRenameTypeTransform;
132
+ function createRenameFieldTransform(renameFn, kind = utils_1.MapperKind.FIELD) {
133
+ return function renameFieldTransform(schema, subgraphConfig) {
134
+ return (0, utils_1.mapSchema)(schema, {
135
+ [kind]: (field, fieldName, typeName) => [renameFn(field, fieldName, typeName, subgraphConfig) || fieldName, field],
136
+ });
137
+ };
138
+ }
139
+ exports.createRenameFieldTransform = createRenameFieldTransform;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/esm/index.js ADDED
@@ -0,0 +1,133 @@
1
+ import { isSpecifiedScalarType, } from 'graphql';
2
+ import { mergeSchemas } from '@graphql-tools/schema';
3
+ import { getRootTypeMap, MapperKind, mapSchema } from '@graphql-tools/utils';
4
+ const defaultRootTypeNames = {
5
+ query: 'Query',
6
+ mutation: 'Mutation',
7
+ subscription: 'Subscription',
8
+ };
9
+ export function composeSubgraphs(subgraphs) {
10
+ const annotatedSubgraphs = [];
11
+ for (const subgraphConfig of subgraphs) {
12
+ const { name: subgraphName, schema, transforms } = subgraphConfig;
13
+ const rootTypeMap = getRootTypeMap(schema);
14
+ const typeToOperationType = new Map();
15
+ for (const [operationType, rootType] of rootTypeMap) {
16
+ typeToOperationType.set(rootType.name, operationType);
17
+ }
18
+ const annotatedSubgraph = mapSchema(schema, {
19
+ [MapperKind.TYPE]: type => {
20
+ if (isSpecifiedScalarType(type)) {
21
+ return type;
22
+ }
23
+ const operationType = typeToOperationType.get(type.name);
24
+ if (operationType) {
25
+ return new (Object.getPrototypeOf(type).constructor)({
26
+ ...type.toConfig(),
27
+ name: defaultRootTypeNames[operationType],
28
+ });
29
+ }
30
+ return new (Object.getPrototypeOf(type).constructor)({
31
+ ...type.toConfig(),
32
+ extensions: {
33
+ ...type.extensions,
34
+ directives: {
35
+ ...(type.extensions?.directives || {}),
36
+ source: {
37
+ subgraph: subgraphName,
38
+ name: type.name,
39
+ },
40
+ },
41
+ },
42
+ });
43
+ },
44
+ [MapperKind.FIELD]: (fieldConfig, fieldName) => ({
45
+ ...fieldConfig,
46
+ extensions: {
47
+ ...fieldConfig.extensions,
48
+ directives: {
49
+ ...(fieldConfig.extensions?.directives || {}),
50
+ source: {
51
+ subgraph: subgraphName,
52
+ name: fieldName,
53
+ },
54
+ },
55
+ },
56
+ }),
57
+ [MapperKind.ENUM_VALUE]: (valueConfig, _typeName, _schema, externalValue) => ({
58
+ ...valueConfig,
59
+ extensions: {
60
+ ...valueConfig.extensions,
61
+ directives: {
62
+ ...(valueConfig.extensions?.directives || {}),
63
+ source: {
64
+ subgraph: subgraphName,
65
+ name: externalValue,
66
+ },
67
+ },
68
+ },
69
+ }),
70
+ [MapperKind.ROOT_FIELD]: (fieldConfig, fieldName, typeName) => {
71
+ const operationType = typeToOperationType.get(typeName);
72
+ const operationName = operationType === 'query' ? fieldName : `${operationType}${fieldName}`;
73
+ const variableDefinitions = [];
74
+ const rootFieldArgs = [];
75
+ if (fieldConfig.args) {
76
+ for (const argName in fieldConfig.args) {
77
+ variableDefinitions.push(`$${argName}: ${fieldConfig.args[argName].type}`);
78
+ rootFieldArgs.push(`${argName}: $${argName}`);
79
+ }
80
+ }
81
+ const variableDefinitionsString = variableDefinitions.length
82
+ ? `(${variableDefinitions.join(', ')})`
83
+ : '';
84
+ const rootFieldArgsString = rootFieldArgs.length ? `(${rootFieldArgs.join(', ')})` : '';
85
+ const operationString = `${operationType} ${operationName}${variableDefinitionsString} { ${fieldName}${rootFieldArgsString} }`;
86
+ return {
87
+ ...fieldConfig,
88
+ extensions: {
89
+ ...fieldConfig.extensions,
90
+ directives: {
91
+ ...(fieldConfig.extensions?.directives || {}),
92
+ resolver: {
93
+ subgraph: subgraphName,
94
+ operation: operationString,
95
+ },
96
+ },
97
+ },
98
+ };
99
+ },
100
+ });
101
+ let transformedSubgraph = annotatedSubgraph;
102
+ if (transforms?.length) {
103
+ for (const transform of transforms) {
104
+ transformedSubgraph = transform(transformedSubgraph, subgraphConfig);
105
+ }
106
+ }
107
+ annotatedSubgraphs.push(transformedSubgraph);
108
+ }
109
+ return mergeSchemas({
110
+ schemas: annotatedSubgraphs,
111
+ assumeValidSDL: true,
112
+ assumeValid: true,
113
+ });
114
+ }
115
+ export function createRenameTypeTransform(renameFn, kind = MapperKind.TYPE) {
116
+ return function renameTypeTransform(schema, subgraphConfig) {
117
+ return mapSchema(schema, {
118
+ [kind]: (type) => isSpecifiedScalarType(type)
119
+ ? type
120
+ : new (Object.getPrototypeOf(type).constructor)({
121
+ ...type.toConfig(),
122
+ name: renameFn(type, subgraphConfig) || type.name,
123
+ }),
124
+ });
125
+ };
126
+ }
127
+ export function createRenameFieldTransform(renameFn, kind = MapperKind.FIELD) {
128
+ return function renameFieldTransform(schema, subgraphConfig) {
129
+ return mapSchema(schema, {
130
+ [kind]: (field, fieldName, typeName) => [renameFn(field, fieldName, typeName, subgraphConfig) || fieldName, field],
131
+ });
132
+ };
133
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@graphql-mesh/fusion-composition",
3
+ "version": "0.0.1-alpha-20231115135952-ce851df6a",
4
+ "description": "Basic composition utility for Fusion spec",
5
+ "sideEffects": false,
6
+ "peerDependencies": {
7
+ "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
8
+ },
9
+ "dependencies": {
10
+ "@graphql-tools/schema": "^10.0.0",
11
+ "@graphql-tools/utils": "^10.0.8",
12
+ "tslib": "^2.4.0"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "ardatan/graphql-mesh",
17
+ "directory": "packages/fusion/composition"
18
+ },
19
+ "license": "MIT",
20
+ "engines": {
21
+ "node": ">=16.0.0"
22
+ },
23
+ "main": "cjs/index.js",
24
+ "module": "esm/index.js",
25
+ "typings": "typings/index.d.ts",
26
+ "typescript": {
27
+ "definition": "typings/index.d.ts"
28
+ },
29
+ "type": "module",
30
+ "exports": {
31
+ ".": {
32
+ "require": {
33
+ "types": "./typings/index.d.cts",
34
+ "default": "./cjs/index.js"
35
+ },
36
+ "import": {
37
+ "types": "./typings/index.d.ts",
38
+ "default": "./esm/index.js"
39
+ },
40
+ "default": {
41
+ "types": "./typings/index.d.ts",
42
+ "default": "./esm/index.js"
43
+ }
44
+ },
45
+ "./*": {
46
+ "require": {
47
+ "types": "./typings/*.d.cts",
48
+ "default": "./cjs/*.js"
49
+ },
50
+ "import": {
51
+ "types": "./typings/*.d.ts",
52
+ "default": "./esm/*.js"
53
+ },
54
+ "default": {
55
+ "types": "./typings/*.d.ts",
56
+ "default": "./esm/*.js"
57
+ }
58
+ },
59
+ "./package.json": "./package.json"
60
+ }
61
+ }
@@ -0,0 +1,14 @@
1
+ import { GraphQLFieldConfig, GraphQLInputFieldConfig, GraphQLNamedType, GraphQLSchema } from 'graphql';
2
+ import { MapperKind } from '@graphql-tools/utils';
3
+ export interface SubgraphConfig {
4
+ name: string;
5
+ schema: GraphQLSchema;
6
+ transforms?: SubgraphTransform[];
7
+ }
8
+ type SubgraphTransform = (schema: GraphQLSchema, subgraphConfig: SubgraphConfig) => GraphQLSchema;
9
+ export declare function composeSubgraphs(subgraphs: SubgraphConfig[]): GraphQLSchema;
10
+ type MapperTypeKind = MapperKind.ROOT_OBJECT | MapperKind.OBJECT_TYPE | MapperKind.INTERFACE_TYPE | MapperKind.UNION_TYPE | MapperKind.ENUM_TYPE | MapperKind.INPUT_OBJECT_TYPE | MapperKind.SCALAR_TYPE | MapperKind.TYPE;
11
+ export declare function createRenameTypeTransform(renameFn: (type: GraphQLNamedType, subgraphConfig: SubgraphConfig) => string, kind?: MapperTypeKind): SubgraphTransform;
12
+ export type MapperFieldKind = MapperKind.FIELD | MapperKind.ROOT_FIELD | MapperKind.OBJECT_FIELD | MapperKind.INTERFACE_FIELD | MapperKind.INPUT_OBJECT_FIELD;
13
+ export declare function createRenameFieldTransform(renameFn: (field: GraphQLFieldConfig<any, any> | GraphQLInputFieldConfig, fieldName: string, typeName: string, subgraphConfig: SubgraphConfig) => string, kind?: MapperFieldKind): SubgraphTransform;
14
+ export {};
@@ -0,0 +1,14 @@
1
+ import { GraphQLFieldConfig, GraphQLInputFieldConfig, GraphQLNamedType, GraphQLSchema } from 'graphql';
2
+ import { MapperKind } from '@graphql-tools/utils';
3
+ export interface SubgraphConfig {
4
+ name: string;
5
+ schema: GraphQLSchema;
6
+ transforms?: SubgraphTransform[];
7
+ }
8
+ type SubgraphTransform = (schema: GraphQLSchema, subgraphConfig: SubgraphConfig) => GraphQLSchema;
9
+ export declare function composeSubgraphs(subgraphs: SubgraphConfig[]): GraphQLSchema;
10
+ type MapperTypeKind = MapperKind.ROOT_OBJECT | MapperKind.OBJECT_TYPE | MapperKind.INTERFACE_TYPE | MapperKind.UNION_TYPE | MapperKind.ENUM_TYPE | MapperKind.INPUT_OBJECT_TYPE | MapperKind.SCALAR_TYPE | MapperKind.TYPE;
11
+ export declare function createRenameTypeTransform(renameFn: (type: GraphQLNamedType, subgraphConfig: SubgraphConfig) => string, kind?: MapperTypeKind): SubgraphTransform;
12
+ export type MapperFieldKind = MapperKind.FIELD | MapperKind.ROOT_FIELD | MapperKind.OBJECT_FIELD | MapperKind.INTERFACE_FIELD | MapperKind.INPUT_OBJECT_FIELD;
13
+ export declare function createRenameFieldTransform(renameFn: (field: GraphQLFieldConfig<any, any> | GraphQLInputFieldConfig, fieldName: string, typeName: string, subgraphConfig: SubgraphConfig) => string, kind?: MapperFieldKind): SubgraphTransform;
14
+ export {};