@graphql-tools/wrap 9.3.9 → 9.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.
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MoveRootField = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
6
|
+
const defaultRootTypeNames = {
|
|
7
|
+
query: 'Query',
|
|
8
|
+
mutation: 'Mutation',
|
|
9
|
+
subscription: 'Subscription',
|
|
10
|
+
};
|
|
11
|
+
class MoveRootField {
|
|
12
|
+
constructor(from) {
|
|
13
|
+
this.from = from;
|
|
14
|
+
this.to = {
|
|
15
|
+
query: {},
|
|
16
|
+
mutation: {},
|
|
17
|
+
subscription: {},
|
|
18
|
+
};
|
|
19
|
+
for (const operation in this.from) {
|
|
20
|
+
const removedFields = this.from[operation];
|
|
21
|
+
for (const fieldName in removedFields) {
|
|
22
|
+
const newOperation = removedFields[fieldName];
|
|
23
|
+
this.to[newOperation][fieldName] = operation;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
transformSchema(schema, _subschemaConfig) {
|
|
28
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
29
|
+
const rootTypeMap = (0, utils_1.getRootTypeMap)(schema);
|
|
30
|
+
const newRootFieldsMap = {
|
|
31
|
+
query: ((_b = (_a = rootTypeMap.get(graphql_1.OperationTypeNode.QUERY)) === null || _a === void 0 ? void 0 : _a.toConfig()) === null || _b === void 0 ? void 0 : _b.fields) || {},
|
|
32
|
+
mutation: ((_d = (_c = rootTypeMap.get(graphql_1.OperationTypeNode.MUTATION)) === null || _c === void 0 ? void 0 : _c.toConfig()) === null || _d === void 0 ? void 0 : _d.fields) || {},
|
|
33
|
+
subscription: ((_f = (_e = rootTypeMap.get(graphql_1.OperationTypeNode.SUBSCRIPTION)) === null || _e === void 0 ? void 0 : _e.toConfig()) === null || _f === void 0 ? void 0 : _f.fields) || {},
|
|
34
|
+
};
|
|
35
|
+
for (const operation in this.from) {
|
|
36
|
+
const removedFields = this.from[operation];
|
|
37
|
+
for (const fieldName in removedFields) {
|
|
38
|
+
const fieldConfig = newRootFieldsMap[operation][fieldName];
|
|
39
|
+
(_g = newRootFieldsMap[operation]) === null || _g === void 0 ? true : delete _g[fieldName];
|
|
40
|
+
const newOperation = removedFields[fieldName];
|
|
41
|
+
newRootFieldsMap[newOperation][fieldName] = fieldConfig;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const schemaConfig = schema.toConfig();
|
|
45
|
+
for (const rootType in newRootFieldsMap) {
|
|
46
|
+
const newRootFields = newRootFieldsMap[rootType];
|
|
47
|
+
if (!schemaConfig[rootType] && Object.keys(newRootFields).length > 0) {
|
|
48
|
+
schemaConfig[rootType] = new graphql_1.GraphQLObjectType({
|
|
49
|
+
name: defaultRootTypeNames[rootType],
|
|
50
|
+
fields: newRootFields,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return (0, utils_1.mapSchema)(new graphql_1.GraphQLSchema(schemaConfig), {
|
|
55
|
+
[utils_1.MapperKind.QUERY]: type => {
|
|
56
|
+
const queryConfig = type.toConfig();
|
|
57
|
+
queryConfig.fields = newRootFieldsMap.query;
|
|
58
|
+
return new graphql_1.GraphQLObjectType(queryConfig);
|
|
59
|
+
},
|
|
60
|
+
[utils_1.MapperKind.MUTATION]: type => {
|
|
61
|
+
const mutationConfig = type.toConfig();
|
|
62
|
+
mutationConfig.fields = newRootFieldsMap.mutation;
|
|
63
|
+
return new graphql_1.GraphQLObjectType(mutationConfig);
|
|
64
|
+
},
|
|
65
|
+
[utils_1.MapperKind.SUBSCRIPTION]: type => {
|
|
66
|
+
const subscriptionConfig = type.toConfig();
|
|
67
|
+
subscriptionConfig.fields = newRootFieldsMap.subscription;
|
|
68
|
+
return new graphql_1.GraphQLObjectType(subscriptionConfig);
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
transformRequest(originalRequest, delegationContext) {
|
|
73
|
+
const newOperation = this.to[delegationContext.operation][delegationContext.fieldName];
|
|
74
|
+
if (newOperation && newOperation !== delegationContext.operation) {
|
|
75
|
+
return {
|
|
76
|
+
...originalRequest,
|
|
77
|
+
document: (0, graphql_1.visit)(originalRequest.document, {
|
|
78
|
+
[graphql_1.Kind.OPERATION_DEFINITION]: node => {
|
|
79
|
+
return {
|
|
80
|
+
...node,
|
|
81
|
+
operation: newOperation,
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return originalRequest;
|
|
88
|
+
}
|
|
89
|
+
transformResult(result, delegationContext) {
|
|
90
|
+
var _a, _b;
|
|
91
|
+
if ((_a = result.data) === null || _a === void 0 ? void 0 : _a.__typename) {
|
|
92
|
+
const newOperation = this.to[delegationContext.operation][delegationContext.fieldName];
|
|
93
|
+
if (newOperation && newOperation !== delegationContext.operation) {
|
|
94
|
+
result.data.__typename = (_b = delegationContext.targetSchema.getRootType(newOperation)) === null || _b === void 0 ? void 0 : _b.name;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.MoveRootField = MoveRootField;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { GraphQLObjectType, GraphQLSchema, Kind, OperationTypeNode, visit } from 'graphql';
|
|
2
|
+
import { MapperKind, getRootTypeMap, mapSchema } from '@graphql-tools/utils';
|
|
3
|
+
const defaultRootTypeNames = {
|
|
4
|
+
query: 'Query',
|
|
5
|
+
mutation: 'Mutation',
|
|
6
|
+
subscription: 'Subscription',
|
|
7
|
+
};
|
|
8
|
+
export class MoveRootField {
|
|
9
|
+
constructor(from) {
|
|
10
|
+
this.from = from;
|
|
11
|
+
this.to = {
|
|
12
|
+
query: {},
|
|
13
|
+
mutation: {},
|
|
14
|
+
subscription: {},
|
|
15
|
+
};
|
|
16
|
+
for (const operation in this.from) {
|
|
17
|
+
const removedFields = this.from[operation];
|
|
18
|
+
for (const fieldName in removedFields) {
|
|
19
|
+
const newOperation = removedFields[fieldName];
|
|
20
|
+
this.to[newOperation][fieldName] = operation;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
transformSchema(schema, _subschemaConfig) {
|
|
25
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
26
|
+
const rootTypeMap = getRootTypeMap(schema);
|
|
27
|
+
const newRootFieldsMap = {
|
|
28
|
+
query: ((_b = (_a = rootTypeMap.get(OperationTypeNode.QUERY)) === null || _a === void 0 ? void 0 : _a.toConfig()) === null || _b === void 0 ? void 0 : _b.fields) || {},
|
|
29
|
+
mutation: ((_d = (_c = rootTypeMap.get(OperationTypeNode.MUTATION)) === null || _c === void 0 ? void 0 : _c.toConfig()) === null || _d === void 0 ? void 0 : _d.fields) || {},
|
|
30
|
+
subscription: ((_f = (_e = rootTypeMap.get(OperationTypeNode.SUBSCRIPTION)) === null || _e === void 0 ? void 0 : _e.toConfig()) === null || _f === void 0 ? void 0 : _f.fields) || {},
|
|
31
|
+
};
|
|
32
|
+
for (const operation in this.from) {
|
|
33
|
+
const removedFields = this.from[operation];
|
|
34
|
+
for (const fieldName in removedFields) {
|
|
35
|
+
const fieldConfig = newRootFieldsMap[operation][fieldName];
|
|
36
|
+
(_g = newRootFieldsMap[operation]) === null || _g === void 0 ? true : delete _g[fieldName];
|
|
37
|
+
const newOperation = removedFields[fieldName];
|
|
38
|
+
newRootFieldsMap[newOperation][fieldName] = fieldConfig;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const schemaConfig = schema.toConfig();
|
|
42
|
+
for (const rootType in newRootFieldsMap) {
|
|
43
|
+
const newRootFields = newRootFieldsMap[rootType];
|
|
44
|
+
if (!schemaConfig[rootType] && Object.keys(newRootFields).length > 0) {
|
|
45
|
+
schemaConfig[rootType] = new GraphQLObjectType({
|
|
46
|
+
name: defaultRootTypeNames[rootType],
|
|
47
|
+
fields: newRootFields,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return mapSchema(new GraphQLSchema(schemaConfig), {
|
|
52
|
+
[MapperKind.QUERY]: type => {
|
|
53
|
+
const queryConfig = type.toConfig();
|
|
54
|
+
queryConfig.fields = newRootFieldsMap.query;
|
|
55
|
+
return new GraphQLObjectType(queryConfig);
|
|
56
|
+
},
|
|
57
|
+
[MapperKind.MUTATION]: type => {
|
|
58
|
+
const mutationConfig = type.toConfig();
|
|
59
|
+
mutationConfig.fields = newRootFieldsMap.mutation;
|
|
60
|
+
return new GraphQLObjectType(mutationConfig);
|
|
61
|
+
},
|
|
62
|
+
[MapperKind.SUBSCRIPTION]: type => {
|
|
63
|
+
const subscriptionConfig = type.toConfig();
|
|
64
|
+
subscriptionConfig.fields = newRootFieldsMap.subscription;
|
|
65
|
+
return new GraphQLObjectType(subscriptionConfig);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
transformRequest(originalRequest, delegationContext) {
|
|
70
|
+
const newOperation = this.to[delegationContext.operation][delegationContext.fieldName];
|
|
71
|
+
if (newOperation && newOperation !== delegationContext.operation) {
|
|
72
|
+
return {
|
|
73
|
+
...originalRequest,
|
|
74
|
+
document: visit(originalRequest.document, {
|
|
75
|
+
[Kind.OPERATION_DEFINITION]: node => {
|
|
76
|
+
return {
|
|
77
|
+
...node,
|
|
78
|
+
operation: newOperation,
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return originalRequest;
|
|
85
|
+
}
|
|
86
|
+
transformResult(result, delegationContext) {
|
|
87
|
+
var _a, _b;
|
|
88
|
+
if ((_a = result.data) === null || _a === void 0 ? void 0 : _a.__typename) {
|
|
89
|
+
const newOperation = this.to[delegationContext.operation][delegationContext.fieldName];
|
|
90
|
+
if (newOperation && newOperation !== delegationContext.operation) {
|
|
91
|
+
result.data.__typename = (_b = delegationContext.targetSchema.getRootType(newOperation)) === null || _b === void 0 ? void 0 : _b.name;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { GraphQLSchema, OperationTypeNode } from 'graphql';
|
|
2
|
+
import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
|
|
3
|
+
import { DelegationContext, Transform } from '@graphql-tools/delegate';
|
|
4
|
+
export declare class MoveRootField implements Transform {
|
|
5
|
+
private from;
|
|
6
|
+
private to;
|
|
7
|
+
constructor(from: Record<OperationTypeNode, Record<string, OperationTypeNode>>);
|
|
8
|
+
transformSchema(schema: GraphQLSchema, _subschemaConfig: Record<string, any>): GraphQLSchema;
|
|
9
|
+
transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext): ExecutionRequest;
|
|
10
|
+
transformResult(result: ExecutionResult, delegationContext: DelegationContext): ExecutionResult<any, any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { GraphQLSchema, OperationTypeNode } from 'graphql';
|
|
2
|
+
import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
|
|
3
|
+
import { DelegationContext, Transform } from '@graphql-tools/delegate';
|
|
4
|
+
export declare class MoveRootField implements Transform {
|
|
5
|
+
private from;
|
|
6
|
+
private to;
|
|
7
|
+
constructor(from: Record<OperationTypeNode, Record<string, OperationTypeNode>>);
|
|
8
|
+
transformSchema(schema: GraphQLSchema, _subschemaConfig: Record<string, any>): GraphQLSchema;
|
|
9
|
+
transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext): ExecutionRequest;
|
|
10
|
+
transformResult(result: ExecutionResult, delegationContext: DelegationContext): ExecutionResult<any, any>;
|
|
11
|
+
}
|