proto2graphql
Version:
Converts schema definitions in Protocol Buffer to GraphQL
49 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertScalar = exports.isScalar = exports.fullTypeName = void 0;
var protobuf = require("protobufjs");
var graphql_1 = require("graphql");
var ScalarTypeMap = {
double: graphql_1.GraphQLFloat,
float: graphql_1.GraphQLFloat,
int32: graphql_1.GraphQLInt,
int64: graphql_1.GraphQLInt,
uint32: graphql_1.GraphQLInt,
uint64: graphql_1.GraphQLInt,
sint32: graphql_1.GraphQLInt,
sint64: graphql_1.GraphQLInt,
fixed32: graphql_1.GraphQLInt,
fixed64: graphql_1.GraphQLInt,
sfixed32: graphql_1.GraphQLInt,
sfixed64: graphql_1.GraphQLInt,
bool: graphql_1.GraphQLBoolean,
string: graphql_1.GraphQLString,
bytes: graphql_1.GraphQLString
};
function fullTypeName(type) {
if (type instanceof protobuf.MapField) {
var keyType = convertScalar(type.keyType);
var valueType = isScalar(type.type)
? convertScalar(type.type)
: fullTypeName(type.resolvedType);
return keyType + "_" + valueType + "_map";
}
return type.parent && type.parent.name
? fullTypeName(type.parent) + "_" + type.name
: type.name;
}
exports.fullTypeName = fullTypeName;
function isScalar(type) {
return type in ScalarTypeMap;
}
exports.isScalar = isScalar;
function convertScalar(type) {
return ScalarTypeMap[type];
}
exports.convertScalar = convertScalar;
Array.prototype.flat = function () {
return this.reduce(function (arr, flatting) {
return arr.concat(Array.isArray(flatting) ? flatting.flat() : flatting);
}, []);
};
//# sourceMappingURL=utils.js.map