UNPKG

proto2graphql

Version:

Converts schema definitions in Protocol Buffer to GraphQL

135 lines 4.87 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.visit = void 0; var protobuf = require("protobufjs"); var graphql_1 = require("graphql"); var context_1 = require("./context"); var utils_1 = require("./utils"); function visit(objects) { return visitNested(objects, { types: {} }); } exports.visit = visit; function visitNested(objects, context) { return objects .map(function (object) { if (object instanceof protobuf.Type) { return visitMessage(object, context); } if (object instanceof protobuf.Enum) { return visitEnum(object, context); } if (object instanceof protobuf.Namespace) { return visitNested(object.nestedArray, context); } }) .flat() .filter(Boolean); } function visitMessage(message, context) { var objectType = new graphql_1.GraphQLObjectType({ name: utils_1.fullTypeName(message), fields: function () { return visitFields(message.fieldsArray, context); } }); context_1.setType(objectType, context); return [ objectType, visitOneOfs(message, context), visitMaps(message, context), visitNested(message.nestedArray, context) ]; } function visitEnum(enm, context) { var enumType = new graphql_1.GraphQLEnumType({ name: utils_1.fullTypeName(enm), values: Object.assign.apply(Object, __spread([{}], Object.keys(enm.values).map(function (key) { var _a; return (_a = {}, _a[key] = { value: enm.values[key].valueOf() }, _a); }))) }); context_1.setType(enumType, context); return enumType; } function visitOneOfs(message, context) { return __spread(new Set(message.fieldsArray.map(function (field) { return field.partOf; }).filter(Boolean))).map(function (oneOf) { return visitOneOf(oneOf, context); }); } function visitOneOf(oneOf, context) { var unionType = new graphql_1.GraphQLUnionType({ name: utils_1.fullTypeName(oneOf), types: function () { return oneOf.fieldsArray.map(function (field) { return visitFieldType(field, context); }); } }); context_1.setType(unionType, context); return unionType; } function visitMaps(message, context) { return message.fieldsArray.map(function (field) { if (field instanceof protobuf.MapField) { field.resolve(); var objectType = new graphql_1.GraphQLObjectType({ name: utils_1.fullTypeName(field), fields: function () { return ({ key: { type: visitDataType(field.keyType, false, null, context) }, value: { type: visitDataType(field.type, field.repeated, function () { return field.resolvedType; }, context) } }); } }); context_1.setType(objectType, context); return objectType; } return null; }); } function visitFields(fields, context) { return Object.assign.apply(Object, __spread([{}], fields.map(function (field) { var _a, _b; return field.partOf ? (_a = {}, _a[field.partOf.name] = { type: visitOneOf(field.partOf, context) }, _a) : (_b = {}, _b[field.name] = { type: visitFieldType(field, context) }, _b); }))); } function visitFieldType(field, context) { if (field instanceof protobuf.MapField) { return new graphql_1.GraphQLList(context_1.getType(utils_1.fullTypeName(field), context)); } return visitDataType(field.type, field.repeated, function () { return field.resolve().resolvedType; }, context); } function visitDataType(type, repeated, resolver, context) { var baseType = utils_1.isScalar(type) ? utils_1.convertScalar(type) : context_1.getType(utils_1.fullTypeName(resolver()), context); return repeated ? new graphql_1.GraphQLList(baseType) : baseType; } //# sourceMappingURL=visitor.js.map