1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var graphql_1 = require("graphql");
|
3 | var isEmptyObject_1 = require("../isEmptyObject");
|
4 | var visitSchema_1 = require("./visitSchema");
|
5 | var schemaRecreation_1 = require("../stitching/schemaRecreation");
|
6 | var TransformRootFields = (function () {
|
7 | function TransformRootFields(transform) {
|
8 | this.transform = transform;
|
9 | }
|
10 | TransformRootFields.prototype.transformSchema = function (originalSchema) {
|
11 | var _this = this;
|
12 | var _a;
|
13 | return visitSchema_1.visitSchema(originalSchema, (_a = {},
|
14 | _a[visitSchema_1.VisitSchemaKind.QUERY] = function (type) {
|
15 | return transformFields(type, function (fieldName, field) {
|
16 | return _this.transform('Query', fieldName, field);
|
17 | });
|
18 | },
|
19 | _a[visitSchema_1.VisitSchemaKind.MUTATION] = function (type) {
|
20 | return transformFields(type, function (fieldName, field) {
|
21 | return _this.transform('Mutation', fieldName, field);
|
22 | });
|
23 | },
|
24 | _a[visitSchema_1.VisitSchemaKind.SUBSCRIPTION] = function (type) {
|
25 | return transformFields(type, function (fieldName, field) {
|
26 | return _this.transform('Subscription', fieldName, field);
|
27 | });
|
28 | },
|
29 | _a));
|
30 | };
|
31 | return TransformRootFields;
|
32 | }());
|
33 | exports.default = TransformRootFields;
|
34 | function transformFields(type, transformer) {
|
35 | var resolveType = schemaRecreation_1.createResolveType(function (name, originalType) {
|
36 | return originalType;
|
37 | });
|
38 | var fields = type.getFields();
|
39 | var newFields = {};
|
40 | Object.keys(fields).forEach(function (fieldName) {
|
41 | var field = fields[fieldName];
|
42 | var newField = transformer(fieldName, field);
|
43 | if (typeof newField === 'undefined') {
|
44 | newFields[fieldName] = schemaRecreation_1.fieldToFieldConfig(field, resolveType, true);
|
45 | }
|
46 | else if (newField !== null) {
|
47 | if (newField.name) {
|
48 | newFields[newField.name] = newField.field;
|
49 | }
|
50 | else {
|
51 | newFields[fieldName] = newField;
|
52 | }
|
53 | }
|
54 | });
|
55 | if (isEmptyObject_1.default(newFields)) {
|
56 | return null;
|
57 | }
|
58 | else {
|
59 | return new graphql_1.GraphQLObjectType({
|
60 | name: type.name,
|
61 | description: type.description,
|
62 | astNode: type.astNode,
|
63 | fields: newFields,
|
64 | });
|
65 | }
|
66 | }
|
67 |
|
\ | No newline at end of file |