graphql-static-binding
Version:
Generate static binding files for a GraphQL schema
36 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
var graphcool_js_1 = require("./graphcool-js");
exports.generator = {
Main: renderMainMethod,
Header: renderHeader,
};
function renderHeader(schema) {
return "const { Prisma } = require('prisma-binding')\nconst { GraphQLResolveInfo } = require('graphql')\n\nconst typeDefs = `\n" + schema + "`";
}
function renderMainMethod(queryType, mutationType, subscriptionType) {
return "module.exports.Prisma = class Binding extends Prisma {\n \n constructor({ endpoint, secret, fragmentReplacements, debug }) {\n super({ typeDefs, endpoint, secret, fragmentReplacements, debug });\n\n var self = this\n this.exists = {\n" + renderExistsFields(queryType.getFields()) + "\n }\n\n this.query = {\n" + graphcool_js_1.renderMainMethodFields('query', queryType.getFields()) + "\n }" + (mutationType ? "\n \n this.mutation = {\n" + graphcool_js_1.renderMainMethodFields('mutation', mutationType.getFields()) + "\n }" : '') + (subscriptionType ? "\n \n this.subscription = {\n" + graphcool_js_1.renderMainSubscriptionMethodFields('mutation', subscriptionType.getFields()) + "\n }" : '') + "\n }\n \n delegate(operation, field, args, context, info) {\n return super.delegate(operation, field, args, context, info)\n }\n}";
}
function renderExistsFields(fields) {
return Object.keys(fields)
.map(function (f) {
var field = fields[f];
var type = field.type;
var foundList = false;
// Traverse the wrapping types (if any)
while (graphql_1.isWrappingType(type)) {
type = type.ofType;
// One of those wrappings need to be a GraphQLList for this field to qualify
foundList = foundList || graphql_1.isListType(type);
}
if (foundList) {
var whereType = field.args.find(function (a) { return a.name === 'where'; }).type.name;
return " " + type.name + "(where) {\n return super.existsDelegate('query', '" + field.name + "', { where }, {}, '{ id }')\n }";
}
})
.filter(function (f) { return f; })
.join(',\n');
}
exports.renderExistsFields = renderExistsFields;
//# sourceMappingURL=prisma-js.js.map