graphql-codegen-compiler
Advanced tools
Comparing version 0.9.0-alpha.6b9f20a7 to 0.9.0-alpha.6c0c5bdf
@@ -14,2 +14,3 @@ "use strict"; | ||
var prepare_documents_only_1 = require("./prepare-documents-only"); | ||
var path = require("path"); | ||
var handlersMap = { | ||
@@ -67,3 +68,3 @@ type: handleType, | ||
return schemaContext.types.map(function (type) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(type.name, 'type') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(type.name, 'type') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(type), | ||
@@ -76,3 +77,3 @@ }); }); | ||
return schemaContext.inputTypes.map(function (type) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(type.name, 'input-type') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(type.name, 'input-type') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(type), | ||
@@ -85,3 +86,3 @@ }); }); | ||
return schemaContext.unions.map(function (union) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(union.name, 'union') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(union.name, 'union') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(union), | ||
@@ -94,3 +95,3 @@ }); }); | ||
return schemaContext.enums.map(function (en) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(en.name, 'enum') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(en.name, 'enum') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(en), | ||
@@ -103,3 +104,3 @@ }); }); | ||
return schemaContext.scalars.map(function (scalar) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(scalar.name, 'scalar') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(scalar.name, 'scalar') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(scalar), | ||
@@ -112,3 +113,3 @@ }); }); | ||
return schemaContext.interfaces.map(function (inf) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(inf.name, 'interface') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(inf.name, 'interface') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(inf), | ||
@@ -121,3 +122,3 @@ }); }); | ||
return documents.operations.map(function (operation) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(operation.name, operation.operationType) + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(operation.name, operation.operationType) + '.' + (fileExtension || ''), | ||
content: compiledTemplate(operation), | ||
@@ -130,3 +131,3 @@ }); }); | ||
return documents.fragments.map(function (fragment) { return ({ | ||
filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizie_filename_1.sanitizeFilename(fragment.name, 'fragment') + '.' + (fileExtension || ''), | ||
filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(fragment.name, 'fragment') + '.' + (fileExtension || ''), | ||
content: compiledTemplate(fragment), | ||
@@ -136,11 +137,21 @@ }); }); | ||
function parseTemplateName(templateName) { | ||
var splitted = (templateName || '').split('.'); | ||
var templateExtension = splitted[splitted.length - 1]; | ||
var splitted = (path.basename(templateName)).split('.'); | ||
var hasPrefix = true; | ||
if (splitted.length === 3) { | ||
splitted.unshift(''); | ||
hasPrefix = false; | ||
} | ||
if (splitted.length !== 4 && templateName.includes('/')) { | ||
throw new Error("Invalid template name: " + templateName + "!"); | ||
} | ||
var templateExtension = splitted[3]; | ||
if (templateExtension && exports.ALLOWED_CUSTOM_TEMPLATE_EXT.includes(templateExtension)) { | ||
var compilationContext = splitted[splitted.length - 2]; | ||
var fileExtension = splitted[splitted.length - 3]; | ||
var compilationContext = splitted[2]; | ||
var prefix = splitted[0]; | ||
var fileExtension = splitted[1]; | ||
var handler = handlersMap[compilationContext]; | ||
if (handler) { | ||
var pref = path.resolve(path.dirname(templateName) + '/', prefix); | ||
return { | ||
prefix: splitted.slice(0, splitted.length - 3).join('.'), | ||
prefix: hasPrefix ? ['all', 'documents', 'schema'].includes(compilationContext) ? pref : pref + '.' : pref + '/', | ||
handler: handler, | ||
@@ -147,0 +158,0 @@ fileExtension: fileExtension, |
@@ -10,2 +10,6 @@ "use strict"; | ||
exports.initHelpers = function (config, schemaContext) { | ||
var customHelpers = config.customHelpers || {}; | ||
Object.keys(customHelpers).forEach(function (helperName) { | ||
handlebars_1.registerHelper(helperName, customHelpers[helperName]); | ||
}); | ||
handlebars_1.registerHelper('toPrimitive', function (type) { | ||
@@ -26,6 +30,9 @@ return config.primitives[type] || type || ''; | ||
if (directiveValue) { | ||
return options.fn ? options.fn(directiveValue) : ''; | ||
return options && options.fn ? options.fn(directiveValue) : ''; | ||
} | ||
else { | ||
return options && options.inverse ? options.inverse() : ''; | ||
} | ||
} | ||
return ''; | ||
return options && options.inverse ? options.inverse() : ''; | ||
}); | ||
@@ -37,6 +44,9 @@ handlebars_1.registerHelper('unlessDirective', function (context, directiveName, options) { | ||
if (!directiveValue) { | ||
return options.fn ? options.fn(directiveValue) : ''; | ||
return options && options.fn ? options.fn(directiveValue) : ''; | ||
} | ||
else { | ||
return options && options.inverse ? options.inverse() : ''; | ||
} | ||
} | ||
return ''; | ||
return options && options.inverse ? options.inverse() : ''; | ||
}); | ||
@@ -63,3 +73,3 @@ handlebars_1.registerHelper('toComment', function (str) { | ||
if (!imports.find(function (t) { return t.name === field.type; })) { | ||
imports.push({ name: field.type, file: file }); | ||
imports.push({ name: field.type, file: file, type: fieldType }); | ||
} | ||
@@ -74,3 +84,3 @@ } | ||
if (!imports.find(function (t) { return t.name === arg.type; })) { | ||
imports.push({ name: arg.type, file: file }); | ||
imports.push({ name: arg.type, file: file, type: fieldType }); | ||
} | ||
@@ -87,3 +97,3 @@ } | ||
if (!imports.find(function (t) { return t.name === infName; })) { | ||
imports.push({ name: infName, file: file }); | ||
imports.push({ name: infName, file: file, type: 'interface' }); | ||
} | ||
@@ -97,3 +107,3 @@ }); | ||
if (!imports.find(function (t) { return t.name === possibleType; })) { | ||
imports.push({ name: possibleType, file: file }); | ||
imports.push({ name: possibleType, file: file, type: 'type' }); | ||
} | ||
@@ -108,3 +118,3 @@ }); | ||
if (!imports.find(function (t) { return t.name === variable.type; })) { | ||
imports.push({ name: variable.type, file: file }); | ||
imports.push({ name: variable.type, file: file, type: fieldType }); | ||
} | ||
@@ -122,3 +132,3 @@ } | ||
if (!imports.find(function (t) { return t.name === fragmentSpread.fragmentName; })) { | ||
imports.push({ name: fragmentSpread.fragmentName, file: file }); | ||
imports.push({ name: fragmentSpread.fragmentName, file: file, type: 'fragment' }); | ||
} | ||
@@ -142,3 +152,3 @@ }); | ||
if (!imports.find(function (t) { return t.name === field.type; })) { | ||
imports.push({ name: field.type, file: file }); | ||
imports.push({ name: field.type, file: file, type: type }); | ||
} | ||
@@ -194,3 +204,43 @@ } | ||
}); | ||
handlebars_1.registerHelper('ifCond', function (v1, operator, v2, options) { | ||
switch (operator) { | ||
case '==': | ||
return (v1 == v2) ? options.fn(this) : options.inverse(this); | ||
case '===': | ||
return (v1 === v2) ? options.fn(this) : options.inverse(this); | ||
case '!=': | ||
return (v1 != v2) ? options.fn(this) : options.inverse(this); | ||
case '!==': | ||
return (v1 !== v2) ? options.fn(this) : options.inverse(this); | ||
case '<': | ||
return (v1 < v2) ? options.fn(this) : options.inverse(this); | ||
case '<=': | ||
return (v1 <= v2) ? options.fn(this) : options.inverse(this); | ||
case '>': | ||
return (v1 > v2) ? options.fn(this) : options.inverse(this); | ||
case '>=': | ||
return (v1 >= v2) ? options.fn(this) : options.inverse(this); | ||
case '&&': | ||
return (v1 && v2) ? options.fn(this) : options.inverse(this); | ||
case '||': | ||
return (v1 || v2) ? options.fn(this) : options.inverse(this); | ||
default: | ||
return options.inverse(this); | ||
} | ||
}); | ||
handlebars_1.registerHelper('withGql', function (type, name, options) { | ||
if (!type || !name) { | ||
throw new Error("Both type and name are required for withGql helper!"); | ||
} | ||
var sourceArr = schemaContext[type] || schemaContext[type + 's']; | ||
if (!sourceArr) { | ||
throw new Error("Type " + type + " is not a valid SchemaTemplateContext field!"); | ||
} | ||
var item = sourceArr.find(function (item) { return item.name === name; }); | ||
if (!item) { | ||
throw new Error("GraphQL object with name " + name + " and type " + type + " cannot be found!"); | ||
} | ||
return options.fn(item); | ||
}); | ||
}; | ||
//# sourceMappingURL=handlebars-extensions.js.map |
{ | ||
"name": "graphql-codegen-compiler", | ||
"version": "0.9.0-alpha.6b9f20a7", | ||
"version": "0.9.0-alpha.6c0c5bdf", | ||
"description": "GraphQL code generator compiler", | ||
@@ -28,5 +28,5 @@ "license": "MIT", | ||
"common-tags": "^1.4.0", | ||
"graphql-codegen-compiler": "0.9.0-alpha.6b9f20a7", | ||
"graphql-codegen-core": "0.9.0-alpha.6b9f20a7", | ||
"graphql-codegen-generators": "0.9.0-alpha.6b9f20a7", | ||
"graphql-codegen-compiler": "0.9.0-alpha.6c0c5bdf", | ||
"graphql-codegen-core": "0.9.0-alpha.6c0c5bdf", | ||
"graphql-codegen-generators": "0.9.0-alpha.6c0c5bdf", | ||
"handlebars": "^4.0.10" | ||
@@ -33,0 +33,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148895
837
+ Addedgraphql-codegen-core@0.9.0-alpha.6c0c5bdf(transitive)
+ Addedgraphql-codegen-generators@0.9.0-alpha.6c0c5bdf(transitive)
- Removedgraphql-codegen-core@0.9.0-alpha.6b9f20a7(transitive)
- Removedgraphql-codegen-generators@0.9.0-alpha.6b9f20a7(transitive)