1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var graphql_1 = require("graphql");
|
4 | var transform_fragment_document_1 = require("./transform-fragment-document");
|
5 | var transform_operation_1 = require("./transform-operation");
|
6 | var debugging_1 = require("../debugging");
|
7 | var __1 = require("..");
|
8 | var usedNames = {};
|
9 | function 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 | }
|
18 | function 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 | }
|
28 | function 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 | }
|
36 | exports.fixAnonymousDocument = fixAnonymousDocument;
|
37 | function 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 | }
|
53 | exports.transformDocumentsFiles = transformDocumentsFiles;
|
54 | function 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 | }
|
85 | exports.transformDocument = transformDocument;
|
86 |
|
\ | No newline at end of file |