graphql-codegen-core
Version:
GraphQL types and code generator based on schema
86 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
var transform_fragment_document_1 = require("./transform-fragment-document");
var transform_operation_1 = require("./transform-operation");
var debugging_1 = require("../debugging");
var __1 = require("..");
var usedNames = {};
function nameGenerator(operationType, count) {
if (count === void 0) { count = 1; }
var idea = "Anonymous_" + operationType + "_" + count;
if (usedNames[idea]) {
return nameGenerator(operationType, count + 1);
}
usedNames[idea] = true;
return idea;
}
function generateTempName(documentNode) {
var operationType;
if (documentNode.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
operationType = 'fragment';
}
else if (documentNode.kind === graphql_1.Kind.OPERATION_DEFINITION) {
operationType = documentNode.operation;
}
return nameGenerator(operationType);
}
function fixAnonymousDocument(documentNode) {
if (!documentNode.name) {
var newName = generateTempName(documentNode);
__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));
return newName;
}
return null;
}
exports.fixAnonymousDocument = fixAnonymousDocument;
function transformDocumentsFiles(schema, documentFiles) {
return documentFiles
.map(function (documentsFile) { return transformDocument(schema, documentsFile.content, documentsFile.filePath); })
.reduce(function (result, transformedDocument) {
result.fragments = result.fragments.concat(transformedDocument.fragments);
result.operations = result.operations.concat(transformedDocument.operations);
result.hasFragments = result.fragments.length > 0;
result.hasOperations = result.operations.length > 0;
return result;
}, {
fragments: [],
operations: [],
hasFragments: false,
hasOperations: false
});
}
exports.transformDocumentsFiles = transformDocumentsFiles;
function transformDocument(schema, documentNode, originalFilePath) {
if (originalFilePath === void 0) { originalFilePath = null; }
var result = {
fragments: [],
operations: [],
hasFragments: false,
hasOperations: false
};
var definitions = documentNode.definitions || [];
debugging_1.debugLog("[transformDocument] transforming total of " + definitions.length + " definitions...");
definitions.forEach(function (definitionNode) {
if (definitionNode.kind === graphql_1.Kind.OPERATION_DEFINITION) {
var overrideName = fixAnonymousDocument(definitionNode);
var operation = transform_operation_1.transformOperation(schema, definitionNode, overrideName);
operation.originalFile = originalFilePath;
result.operations.push(operation);
}
else if (definitionNode.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
var overrideName = fixAnonymousDocument(definitionNode);
var fragment = transform_fragment_document_1.transformFragment(schema, definitionNode, overrideName);
fragment.originalFile = originalFilePath;
result.fragments.push(fragment);
}
else {
__1.getLogger().warn("It seems like you provided an invalid GraphQL document of kind \"" + definitionNode.kind + "\".");
}
});
result.hasFragments = result.fragments.length > 0;
result.hasOperations = result.operations.length > 0;
return result;
}
exports.transformDocument = transformDocument;
//# sourceMappingURL=transform-document.js.map