1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.stripCommonPrimitives = exports.replaceExtendedDefinitionsWithExtensions = exports.defaultRootOperationTypes = exports.normalizeTypeDefs = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const utils_1 = require("./utils");
|
6 | const directives_1 = require("@apollo/subgraph/dist/directives");
|
7 | function normalizeTypeDefs(typeDefs) {
|
8 | return stripCommonPrimitives(defaultRootOperationTypes(replaceExtendedDefinitionsWithExtensions(typeDefs)));
|
9 | }
|
10 | exports.normalizeTypeDefs = normalizeTypeDefs;
|
11 | function defaultRootOperationTypes(typeDefs) {
|
12 | const defaultRootOperationNames = Object.values(utils_1.defaultRootOperationNameLookup);
|
13 | let rootOperationTypeMap = Object.create(null);
|
14 | let hasSchemaDefinitionOrExtension = false;
|
15 | (0, graphql_1.visit)(typeDefs, {
|
16 | OperationTypeDefinition(node) {
|
17 | hasSchemaDefinitionOrExtension = true;
|
18 | rootOperationTypeMap[node.type.name.value] =
|
19 | utils_1.defaultRootOperationNameLookup[node.operation];
|
20 | },
|
21 | });
|
22 | if (!hasSchemaDefinitionOrExtension) {
|
23 | rootOperationTypeMap = {
|
24 | Query: 'Query',
|
25 | Mutation: 'Mutation',
|
26 | Subscription: 'Subscription',
|
27 | };
|
28 | }
|
29 | let schemaWithoutConflictingDefaultDefinitions;
|
30 | if (!hasSchemaDefinitionOrExtension) {
|
31 | schemaWithoutConflictingDefaultDefinitions = typeDefs;
|
32 | }
|
33 | else {
|
34 | schemaWithoutConflictingDefaultDefinitions = (0, graphql_1.visit)(typeDefs, {
|
35 | ObjectTypeDefinition(node) {
|
36 | if (defaultRootOperationNames.includes(node.name.value) &&
|
37 | !rootOperationTypeMap[node.name.value]) {
|
38 | return null;
|
39 | }
|
40 | return;
|
41 | },
|
42 | ObjectTypeExtension(node) {
|
43 | if (defaultRootOperationNames.includes(node.name.value) &&
|
44 | !rootOperationTypeMap[node.name.value]) {
|
45 | return null;
|
46 | }
|
47 | return;
|
48 | },
|
49 | FieldDefinition(node) {
|
50 | if (node.type.kind === graphql_1.Kind.NAMED_TYPE &&
|
51 | defaultRootOperationNames.includes(node.type.name.value)) {
|
52 | return null;
|
53 | }
|
54 | if (node.type.kind === graphql_1.Kind.NON_NULL_TYPE &&
|
55 | node.type.type.kind === graphql_1.Kind.NAMED_TYPE &&
|
56 | defaultRootOperationNames.includes(node.type.type.name.value)) {
|
57 | return null;
|
58 | }
|
59 | return;
|
60 | },
|
61 | });
|
62 | }
|
63 | const schemaWithDefaultRootTypes = (0, graphql_1.visit)(schemaWithoutConflictingDefaultDefinitions, {
|
64 | SchemaDefinition() {
|
65 | return null;
|
66 | },
|
67 | SchemaExtension() {
|
68 | return null;
|
69 | },
|
70 | ObjectTypeDefinition(node) {
|
71 | if (node.name.value in rootOperationTypeMap ||
|
72 | defaultRootOperationNames.includes(node.name.value)) {
|
73 | return {
|
74 | ...node,
|
75 | name: {
|
76 | ...node.name,
|
77 | value: rootOperationTypeMap[node.name.value] || node.name.value,
|
78 | },
|
79 | kind: graphql_1.Kind.OBJECT_TYPE_EXTENSION,
|
80 | };
|
81 | }
|
82 | return;
|
83 | },
|
84 | ObjectTypeExtension(node) {
|
85 | if (node.name.value in rootOperationTypeMap ||
|
86 | defaultRootOperationNames.includes(node.name.value)) {
|
87 | return {
|
88 | ...node,
|
89 | name: {
|
90 | ...node.name,
|
91 | value: rootOperationTypeMap[node.name.value] || node.name.value,
|
92 | },
|
93 | };
|
94 | }
|
95 | return;
|
96 | },
|
97 | NamedType(node) {
|
98 | if (node.name.value in rootOperationTypeMap) {
|
99 | return {
|
100 | ...node,
|
101 | name: {
|
102 | ...node.name,
|
103 | value: rootOperationTypeMap[node.name.value],
|
104 | },
|
105 | };
|
106 | }
|
107 | return;
|
108 | },
|
109 | });
|
110 | return schemaWithDefaultRootTypes;
|
111 | }
|
112 | exports.defaultRootOperationTypes = defaultRootOperationTypes;
|
113 | function replaceExtendedDefinitionsWithExtensions(typeDefs) {
|
114 | const typeDefsWithExtendedTypesReplaced = (0, graphql_1.visit)(typeDefs, {
|
115 | ObjectTypeDefinition: visitor,
|
116 | InterfaceTypeDefinition: visitor,
|
117 | });
|
118 | function visitor(node) {
|
119 | const isExtensionDefinition = (0, utils_1.findDirectivesOnNode)(node, 'extends').length > 0;
|
120 | if (!isExtensionDefinition) {
|
121 | return node;
|
122 | }
|
123 | const filteredDirectives = node.directives &&
|
124 | node.directives.filter(directive => directive.name.value !== 'extends');
|
125 | return {
|
126 | ...node,
|
127 | ...(filteredDirectives && { directives: filteredDirectives }),
|
128 | kind: utils_1.defKindToExtKind[node.kind],
|
129 | };
|
130 | }
|
131 | return typeDefsWithExtendedTypesReplaced;
|
132 | }
|
133 | exports.replaceExtendedDefinitionsWithExtensions = replaceExtendedDefinitionsWithExtensions;
|
134 | function stripCommonPrimitives(document) {
|
135 | const typeDefinitionVisitor = (node) => {
|
136 | var _a;
|
137 | if (node.name.value === utils_1.defaultRootOperationNameLookup.query) {
|
138 | const filteredFieldDefinitions = (_a = node.fields) === null || _a === void 0 ? void 0 : _a.filter((fieldDefinition) => !utils_1.reservedRootFields.includes(fieldDefinition.name.value));
|
139 | if (!filteredFieldDefinitions || filteredFieldDefinitions.length === 0) {
|
140 | return null;
|
141 | }
|
142 | return {
|
143 | ...node,
|
144 | fields: filteredFieldDefinitions,
|
145 | };
|
146 | }
|
147 | const isFederationType = node.name.value === '_Service';
|
148 | return isFederationType ? null : node;
|
149 | };
|
150 | return (0, graphql_1.visit)(document, {
|
151 | DirectiveDefinition(node) {
|
152 | const isCommonDirective = [
|
153 | ...directives_1.knownSubgraphDirectives,
|
154 | ...graphql_1.specifiedDirectives,
|
155 | ].some((directive) => directive.name === node.name.value);
|
156 | return isCommonDirective ? null : node;
|
157 | },
|
158 | ScalarTypeDefinition(node) {
|
159 | const isFederationScalar = ['_Any', '_FieldSet'].includes(node.name.value);
|
160 | return isFederationScalar ? null : node;
|
161 | },
|
162 | UnionTypeDefinition(node) {
|
163 | const isFederationUnion = node.name.value === "_Entity";
|
164 | return isFederationUnion ? null : node;
|
165 | },
|
166 | ObjectTypeDefinition: typeDefinitionVisitor,
|
167 | ObjectTypeExtension: typeDefinitionVisitor,
|
168 | });
|
169 | }
|
170 | exports.stripCommonPrimitives = stripCommonPrimitives;
|
171 |
|
\ | No newline at end of file |