UNPKG

3.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var graphql_1 = require("graphql");
4var transform_fragment_document_1 = require("./transform-fragment-document");
5var transform_operation_1 = require("./transform-operation");
6var debugging_1 = require("../debugging");
7var __1 = require("..");
8var usedNames = {};
9function nameGenerator(operationType, count) {
10 if (count === void 0) { count = 1; }
11 var idea = "Anonymous_" + operationType + "_" + count;
12 if (usedNames[idea]) {
13 return nameGenerator(operationType, count + 1);
14 }
15 usedNames[idea] = true;
16 return idea;
17}
18function generateTempName(documentNode) {
19 var operationType;
20 if (documentNode.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
21 operationType = 'fragment';
22 }
23 else if (documentNode.kind === graphql_1.Kind.OPERATION_DEFINITION) {
24 operationType = documentNode.operation;
25 }
26 return nameGenerator(operationType);
27}
28function fixAnonymousDocument(documentNode) {
29 if (!documentNode.name) {
30 var newName = generateTempName(documentNode);
31 __1.getLogger().warn("The following document does not have a name. The codegen will use an anonymous name: " + newName + ", please consider to name it.", graphql_1.print(documentNode));
32 return newName;
33 }
34 return null;
35}
36exports.fixAnonymousDocument = fixAnonymousDocument;
37function transformDocumentsFiles(schema, documentFiles) {
38 return documentFiles
39 .map(function (documentsFile) { return transformDocument(schema, documentsFile.content, documentsFile.filePath); })
40 .reduce(function (result, transformedDocument) {
41 result.fragments = result.fragments.concat(transformedDocument.fragments);
42 result.operations = result.operations.concat(transformedDocument.operations);
43 result.hasFragments = result.fragments.length > 0;
44 result.hasOperations = result.operations.length > 0;
45 return result;
46 }, {
47 fragments: [],
48 operations: [],
49 hasFragments: false,
50 hasOperations: false
51 });
52}
53exports.transformDocumentsFiles = transformDocumentsFiles;
54function transformDocument(schema, documentNode, originalFilePath) {
55 if (originalFilePath === void 0) { originalFilePath = null; }
56 var result = {
57 fragments: [],
58 operations: [],
59 hasFragments: false,
60 hasOperations: false
61 };
62 var definitions = documentNode.definitions || [];
63 debugging_1.debugLog("[transformDocument] transforming total of " + definitions.length + " definitions...");
64 definitions.forEach(function (definitionNode) {
65 if (definitionNode.kind === graphql_1.Kind.OPERATION_DEFINITION) {
66 var overrideName = fixAnonymousDocument(definitionNode);
67 var operation = transform_operation_1.transformOperation(schema, definitionNode, overrideName);
68 operation.originalFile = originalFilePath;
69 result.operations.push(operation);
70 }
71 else if (definitionNode.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
72 var overrideName = fixAnonymousDocument(definitionNode);
73 var fragment = transform_fragment_document_1.transformFragment(schema, definitionNode, overrideName);
74 fragment.originalFile = originalFilePath;
75 result.fragments.push(fragment);
76 }
77 else {
78 __1.getLogger().warn("It seems like you provided an invalid GraphQL document of kind \"" + definitionNode.kind + "\".");
79 }
80 });
81 result.hasFragments = result.fragments.length > 0;
82 result.hasOperations = result.operations.length > 0;
83 return result;
84}
85exports.transformDocument = transformDocument;
86//# sourceMappingURL=transform-document.js.map
\No newline at end of file