@omnigraph/protobuf 0.0.0-stage → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +716 -0
- package/dist/cjs/directives.js +33 -0
- package/dist/cjs/emitMapping.js +137 -0
- package/dist/cjs/grpcLoaderHelper.js +461 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/scalars.js +31 -0
- package/dist/cjs/utils.js +54 -0
- package/dist/esm/directives.js +29 -0
- package/dist/esm/emitMapping.js +130 -0
- package/dist/esm/grpcLoaderHelper.js +456 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/scalars.js +27 -0
- package/dist/esm/utils.js +48 -0
- package/dist/package.json +58 -0
- package/dist/typings/directives.d.cts +3 -0
- package/dist/typings/directives.d.ts +3 -0
- package/dist/typings/emitMapping.d.cts +15 -0
- package/dist/typings/emitMapping.d.ts +15 -0
- package/dist/typings/grpcLoaderHelper.d.cts +28 -0
- package/dist/typings/grpcLoaderHelper.d.ts +28 -0
- package/dist/typings/index.d.cts +22 -0
- package/dist/typings/index.d.ts +22 -0
- package/dist/typings/scalars.d.cts +2 -0
- package/dist/typings/scalars.d.ts +2 -0
- package/dist/typings/utils.d.cts +5 -0
- package/dist/typings/utils.d.ts +5 -0
- package/examples/movies-connect.graphql +53 -0
- package/examples/movies-twirp.graphql +59 -0
- package/package.json +63 -4
- package/src/directives.ts +31 -0
- package/src/emitMapping.ts +166 -0
- package/src/grpcLoaderHelper.ts +556 -0
- package/src/index.ts +35 -0
- package/src/scalars.ts +31 -0
- package/src/utils.ts +61 -0
- package/test/scalars.spec.ts +37 -0
- package/README.md +0 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transportDirective = exports.protobufRpcDirective = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
exports.protobufRpcDirective = new graphql_1.GraphQLDirective({
|
|
6
|
+
name: 'protobuf__rpc',
|
|
7
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION, graphql_1.DirectiveLocation.OBJECT],
|
|
8
|
+
args: {
|
|
9
|
+
subgraph: {
|
|
10
|
+
type: graphql_1.GraphQLString,
|
|
11
|
+
},
|
|
12
|
+
service: {
|
|
13
|
+
type: graphql_1.GraphQLString,
|
|
14
|
+
},
|
|
15
|
+
method: {
|
|
16
|
+
type: graphql_1.GraphQLString,
|
|
17
|
+
},
|
|
18
|
+
args: {
|
|
19
|
+
type: graphql_1.GraphQLString,
|
|
20
|
+
},
|
|
21
|
+
selection: {
|
|
22
|
+
type: graphql_1.GraphQLString,
|
|
23
|
+
},
|
|
24
|
+
stream: {
|
|
25
|
+
type: graphql_1.GraphQLBoolean,
|
|
26
|
+
},
|
|
27
|
+
entity: {
|
|
28
|
+
type: graphql_1.GraphQLBoolean,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
var utils_1 = require("@graphql-mesh/utils");
|
|
33
|
+
Object.defineProperty(exports, "transportDirective", { enumerable: true, get: function () { return utils_1.TransportDirective; } });
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emitEnumValues = emitEnumValues;
|
|
4
|
+
exports.protoTypeToken = protoTypeToken;
|
|
5
|
+
exports.emitFieldsMapping = emitFieldsMapping;
|
|
6
|
+
exports.emitRequestBody = emitRequestBody;
|
|
7
|
+
exports.emitResponseSelection = emitResponseSelection;
|
|
8
|
+
const utils_js_1 = require("./utils.js");
|
|
9
|
+
/**
|
|
10
|
+
* Emit typed protobuf mapping strings from protobufjs JSON namespace (loader-time only).
|
|
11
|
+
* Output is indented and may include `#` comments for humans/AI; the transport parser skips them.
|
|
12
|
+
*/
|
|
13
|
+
function emitEnumValues(rootJson, pathWithName, typeName) {
|
|
14
|
+
const typePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, typeName.split('.'));
|
|
15
|
+
let cur = rootJson;
|
|
16
|
+
for (const p of typePath) {
|
|
17
|
+
cur = cur?.nested?.[p];
|
|
18
|
+
}
|
|
19
|
+
if (!cur?.values) {
|
|
20
|
+
return 'enum{}';
|
|
21
|
+
}
|
|
22
|
+
const parts = Object.entries(cur.values).map(([name, num]) => `${name}=${num}`);
|
|
23
|
+
return `enum{${parts.join(',')}}`;
|
|
24
|
+
}
|
|
25
|
+
function protoTypeToken(rootJson, pathWithName, field) {
|
|
26
|
+
const base = field.type || 'string';
|
|
27
|
+
let token;
|
|
28
|
+
if (field.keyType) {
|
|
29
|
+
token = `map<${scalarOrEnum(rootJson, pathWithName, field.keyType)},${scalarOrEnum(rootJson, pathWithName, base)}>`;
|
|
30
|
+
}
|
|
31
|
+
else if (isScalar(base)) {
|
|
32
|
+
token = base === 'bytes' ? 'bytes' : normalizeScalar(base);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const typePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, base.split('.'));
|
|
36
|
+
let cur = rootJson;
|
|
37
|
+
for (const p of typePath) {
|
|
38
|
+
cur = cur?.nested?.[p];
|
|
39
|
+
}
|
|
40
|
+
if (cur?.values) {
|
|
41
|
+
token = emitEnumValues(rootJson, pathWithName, base);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
token = 'message';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (field.rule === 'repeated') {
|
|
48
|
+
return `repeated ${token}`;
|
|
49
|
+
}
|
|
50
|
+
return token;
|
|
51
|
+
}
|
|
52
|
+
function isScalar(t) {
|
|
53
|
+
return [
|
|
54
|
+
'double',
|
|
55
|
+
'float',
|
|
56
|
+
'int32',
|
|
57
|
+
'int64',
|
|
58
|
+
'uint32',
|
|
59
|
+
'uint64',
|
|
60
|
+
'sint32',
|
|
61
|
+
'sint64',
|
|
62
|
+
'fixed32',
|
|
63
|
+
'fixed64',
|
|
64
|
+
'sfixed32',
|
|
65
|
+
'sfixed64',
|
|
66
|
+
'bool',
|
|
67
|
+
'string',
|
|
68
|
+
'bytes',
|
|
69
|
+
].includes(t);
|
|
70
|
+
}
|
|
71
|
+
function normalizeScalar(t) {
|
|
72
|
+
return t;
|
|
73
|
+
}
|
|
74
|
+
function scalarOrEnum(rootJson, pathWithName, t) {
|
|
75
|
+
if (isScalar(t))
|
|
76
|
+
return normalizeScalar(t);
|
|
77
|
+
const typePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, t.split('.'));
|
|
78
|
+
let cur = rootJson;
|
|
79
|
+
for (const p of typePath) {
|
|
80
|
+
cur = cur?.nested?.[p];
|
|
81
|
+
}
|
|
82
|
+
if (cur?.values)
|
|
83
|
+
return emitEnumValues(rootJson, pathWithName, t);
|
|
84
|
+
return 'string';
|
|
85
|
+
}
|
|
86
|
+
function emitFieldsMapping(rootJson, pathWithName, typeName, bindMode, bindPrefix = '', indent = ' ') {
|
|
87
|
+
const typePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, typeName.split('.'));
|
|
88
|
+
let cur = rootJson;
|
|
89
|
+
for (const p of typePath) {
|
|
90
|
+
cur = cur?.nested?.[p];
|
|
91
|
+
}
|
|
92
|
+
const fields = cur?.fields;
|
|
93
|
+
if (!fields)
|
|
94
|
+
return '';
|
|
95
|
+
const lines = [];
|
|
96
|
+
for (const [fieldName, field] of Object.entries(fields)) {
|
|
97
|
+
const id = field.id ?? 0;
|
|
98
|
+
const token = protoTypeToken(rootJson, pathWithName, field);
|
|
99
|
+
const bind = bindMode === 'prefix' && bindPrefix ? `${bindPrefix}.${fieldName}` : fieldName;
|
|
100
|
+
if (token === 'message' || token === 'repeated message') {
|
|
101
|
+
const nestedType = field.type;
|
|
102
|
+
const nested = emitFieldsMapping(rootJson, pathWithName, nestedType, 'relative', '', indent + ' ');
|
|
103
|
+
lines.push(`${indent}${id}(${token}): ${bind} {`);
|
|
104
|
+
if (nested) {
|
|
105
|
+
lines.push(nested);
|
|
106
|
+
}
|
|
107
|
+
lines.push(`${indent}}`);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
lines.push(`${indent}${id}(${token}): ${bind}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return lines.join('\n');
|
|
114
|
+
}
|
|
115
|
+
/** Request: GraphQL args use `input` wrapping the request message fields. */
|
|
116
|
+
function emitRequestBody(rootJson, pathWithName, requestType) {
|
|
117
|
+
const inner = emitFieldsMapping(rootJson, pathWithName, requestType, 'prefix', 'input', ' ');
|
|
118
|
+
const header = [
|
|
119
|
+
'# Encode GraphQL field arguments into the protobuf request message.',
|
|
120
|
+
'# Each line is fieldId(protoType): graphqlPath (relative to this field args).',
|
|
121
|
+
].join('\n');
|
|
122
|
+
if (!inner.trim()) {
|
|
123
|
+
return `${header}\n`;
|
|
124
|
+
}
|
|
125
|
+
return `${header}\n${inner}\n`;
|
|
126
|
+
}
|
|
127
|
+
function emitResponseSelection(rootJson, pathWithName, responseType) {
|
|
128
|
+
const inner = emitFieldsMapping(rootJson, pathWithName, responseType, 'relative', '', ' ');
|
|
129
|
+
const header = [
|
|
130
|
+
'# Decode the protobuf response message into GraphQL fields.',
|
|
131
|
+
'# Each line is fieldId(protoType): graphqlFieldName.',
|
|
132
|
+
].join('\n');
|
|
133
|
+
if (!inner.trim()) {
|
|
134
|
+
return `${header}\n`;
|
|
135
|
+
}
|
|
136
|
+
return `${header}\n${inner}\n`;
|
|
137
|
+
}
|
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GrpcLoaderHelper = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const graphql_compose_1 = require("graphql-compose");
|
|
7
|
+
const graphql_scalars_1 = require("graphql-scalars");
|
|
8
|
+
const micromatch_1 = tslib_1.__importDefault(require("micromatch"));
|
|
9
|
+
const protobufjs_1 = tslib_1.__importDefault(require("protobufjs"));
|
|
10
|
+
const descriptor_js_1 = tslib_1.__importDefault(require("protobufjs/ext/descriptor.js"));
|
|
11
|
+
const grpc_reflection_js_1 = require("@ardatan/grpc-reflection-js");
|
|
12
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
13
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
14
|
+
const load_files_1 = require("@graphql-tools/load-files");
|
|
15
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
16
|
+
const grpc_js_1 = require("@grpc/grpc-js");
|
|
17
|
+
const disposablestack_1 = require("@whatwg-node/disposablestack");
|
|
18
|
+
const directives_js_1 = require("./directives.js");
|
|
19
|
+
const emitMapping_js_1 = require("./emitMapping.js");
|
|
20
|
+
const utils_js_1 = require("./utils.js");
|
|
21
|
+
const { Root } = protobufjs_1.default;
|
|
22
|
+
const QUERY_METHOD_PREFIXES = ['get', 'list', 'search'];
|
|
23
|
+
class GrpcLoaderHelper extends disposablestack_1.DisposableStack {
|
|
24
|
+
constructor(subgraphName, baseDir, log, config) {
|
|
25
|
+
super();
|
|
26
|
+
this.subgraphName = subgraphName;
|
|
27
|
+
this.baseDir = baseDir;
|
|
28
|
+
this.log = log;
|
|
29
|
+
this.config = config;
|
|
30
|
+
this.schemaComposer = new graphql_compose_1.SchemaComposer();
|
|
31
|
+
}
|
|
32
|
+
async buildSchema() {
|
|
33
|
+
this.schemaComposer.add(graphql_scalars_1.GraphQLBigInt);
|
|
34
|
+
this.schemaComposer.add(graphql_scalars_1.GraphQLByte);
|
|
35
|
+
this.schemaComposer.add(graphql_scalars_1.GraphQLUnsignedInt);
|
|
36
|
+
this.schemaComposer.add(graphql_scalars_1.GraphQLVoid);
|
|
37
|
+
this.schemaComposer.add(graphql_scalars_1.GraphQLJSON);
|
|
38
|
+
this.schemaComposer.createScalarTC({
|
|
39
|
+
name: 'File',
|
|
40
|
+
});
|
|
41
|
+
this.config.requestTimeout = this.config.requestTimeout || 200000;
|
|
42
|
+
this.log.debug(`Getting channel credentials`);
|
|
43
|
+
const creds = await this.getCredentials();
|
|
44
|
+
if ('_unref' in creds && typeof creds._unref === 'function') {
|
|
45
|
+
this.defer(() => creds._unref());
|
|
46
|
+
}
|
|
47
|
+
this.log.debug(`Getting stored root and decoded descriptor set objects`);
|
|
48
|
+
const descriptorSets = await this.getDescriptorSets(creds);
|
|
49
|
+
for (const { name: rootJsonName, rootJson } of descriptorSets) {
|
|
50
|
+
const rootLogger = this.log.child({ root: rootJsonName });
|
|
51
|
+
this.log.debug(`Building the schema structure based on the root object`);
|
|
52
|
+
this.visit({
|
|
53
|
+
nested: rootJson,
|
|
54
|
+
name: '',
|
|
55
|
+
currentPath: [],
|
|
56
|
+
rootJsonName,
|
|
57
|
+
rootJson,
|
|
58
|
+
rootLogger,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// graphql-compose doesn't add @defer and @stream to the schema
|
|
62
|
+
graphql_1.specifiedDirectives.forEach(directive => this.schemaComposer.addDirective(directive));
|
|
63
|
+
if (!this.schemaComposer.hasDirective('stream')) {
|
|
64
|
+
this.schemaComposer.addDirective(utils_1.GraphQLStreamDirective);
|
|
65
|
+
}
|
|
66
|
+
this.log.debug(`Building the final GraphQL Schema`);
|
|
67
|
+
this.schemaComposer.addDirective(directives_js_1.transportDirective);
|
|
68
|
+
const schema = this.schemaComposer.buildSchema();
|
|
69
|
+
const schemaExtensions = (schema.extensions = schema.extensions || {});
|
|
70
|
+
const directiveExtensions = (schemaExtensions.directives = schemaExtensions.directives || {});
|
|
71
|
+
directiveExtensions.transport = {
|
|
72
|
+
subgraph: this.subgraphName,
|
|
73
|
+
kind: 'protobuf',
|
|
74
|
+
location: this.config.endpoint,
|
|
75
|
+
options: {
|
|
76
|
+
requestTimeout: this.config.requestTimeout,
|
|
77
|
+
credentialsSsl: this.config.credentialsSsl,
|
|
78
|
+
useHTTPS: this.config.useHTTPS,
|
|
79
|
+
metaData: this.config.metaData ? Object.entries(this.config.metaData) : undefined,
|
|
80
|
+
// Serialize as entries so keys like `grpc.max_receive_message_length` are GraphQL-safe
|
|
81
|
+
channelOptions: this.config.channelOptions
|
|
82
|
+
? Object.entries(this.config.channelOptions)
|
|
83
|
+
: undefined,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
return schema;
|
|
87
|
+
}
|
|
88
|
+
processReflection(creds) {
|
|
89
|
+
this.log.debug(`Using the reflection`);
|
|
90
|
+
const reflectionEndpoint = string_interpolation_1.stringInterpolator.parse(this.config.endpoint, {
|
|
91
|
+
env: globalThis.process?.env,
|
|
92
|
+
});
|
|
93
|
+
this.log.debug(`Creating gRPC Reflection Client`);
|
|
94
|
+
const reflectionClient = new grpc_reflection_js_1.Client(reflectionEndpoint, creds, this.config.channelOptions);
|
|
95
|
+
// Attach reflectionMetadata to server-reflection calls (routing / auth metadata)
|
|
96
|
+
if (this.config.reflectionMetadata) {
|
|
97
|
+
const meta = new grpc_js_1.Metadata();
|
|
98
|
+
const resolverData = { env: globalThis.process?.env };
|
|
99
|
+
for (const [key, value] of Object.entries(this.config.reflectionMetadata)) {
|
|
100
|
+
const interpolated = typeof value === 'string' ? string_interpolation_1.stringInterpolator.parse(value, resolverData) : value;
|
|
101
|
+
meta.set(key, String(interpolated));
|
|
102
|
+
}
|
|
103
|
+
const grpcClient = reflectionClient.grpcClient;
|
|
104
|
+
const originalServerReflectionInfo = grpcClient.serverReflectionInfo.bind(grpcClient);
|
|
105
|
+
grpcClient.serverReflectionInfo = (...args) => {
|
|
106
|
+
args[0] = meta;
|
|
107
|
+
return originalServerReflectionInfo(...args);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
// Avoid Close RPC name collisions if present on generated clients
|
|
111
|
+
this.defer(() => grpc_js_1.Client.prototype.close.call(reflectionClient.grpcClient));
|
|
112
|
+
return reflectionClient.listServices().then(services => services.filter(service => service && !service?.startsWith('grpc.')).map(service => {
|
|
113
|
+
this.log.debug(`Resolving root of Service: ${service} from the reflection response`);
|
|
114
|
+
return reflectionClient.fileContainingSymbol(service);
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
async processDescriptorFile() {
|
|
118
|
+
let fileName;
|
|
119
|
+
let options;
|
|
120
|
+
if (typeof this.config.source === 'object') {
|
|
121
|
+
fileName = this.config.source.file;
|
|
122
|
+
options = {
|
|
123
|
+
...this.config.source.load,
|
|
124
|
+
includeDirs: this.config.source.load.includeDirs?.map(includeDir => cross_helpers_1.path.isAbsolute(includeDir) ? includeDir : cross_helpers_1.path.join(this.baseDir, includeDir)),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
fileName = this.config.source;
|
|
129
|
+
}
|
|
130
|
+
fileName = string_interpolation_1.stringInterpolator.parse(fileName, { env: globalThis.process?.env });
|
|
131
|
+
const absoluteFilePath = cross_helpers_1.path.isAbsolute(fileName)
|
|
132
|
+
? fileName
|
|
133
|
+
: cross_helpers_1.path.join(this.baseDir, fileName);
|
|
134
|
+
this.log.debug(`Using the descriptor set from ${absoluteFilePath} `);
|
|
135
|
+
const descriptorSetBuffer = await cross_helpers_1.fs.promises.readFile(absoluteFilePath);
|
|
136
|
+
this.log.debug(`Reading ${absoluteFilePath} `);
|
|
137
|
+
let decodedDescriptorSet;
|
|
138
|
+
if (absoluteFilePath.endsWith('json')) {
|
|
139
|
+
this.log.debug(`Parsing ${absoluteFilePath} as json`);
|
|
140
|
+
const descriptorSetJSON = JSON.parse(descriptorSetBuffer.toString());
|
|
141
|
+
decodedDescriptorSet = descriptor_js_1.default.FileDescriptorSet.fromObject(descriptorSetJSON);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
decodedDescriptorSet = descriptor_js_1.default.FileDescriptorSet.decode(descriptorSetBuffer);
|
|
145
|
+
}
|
|
146
|
+
this.log.debug(`Creating root from descriptor set`);
|
|
147
|
+
const rootFromDescriptor = Root.fromDescriptor(decodedDescriptorSet);
|
|
148
|
+
if (options.includeDirs) {
|
|
149
|
+
if (!Array.isArray(options.includeDirs)) {
|
|
150
|
+
throw new Error('The includeDirs option must be an array');
|
|
151
|
+
}
|
|
152
|
+
(0, utils_js_1.addIncludePathResolver)(rootFromDescriptor, options.includeDirs);
|
|
153
|
+
}
|
|
154
|
+
return rootFromDescriptor;
|
|
155
|
+
}
|
|
156
|
+
async processProtoFile() {
|
|
157
|
+
this.log.debug(`Using proto file(s)`);
|
|
158
|
+
let protoRoot = new Root();
|
|
159
|
+
let fileGlob;
|
|
160
|
+
let options = {
|
|
161
|
+
keepCase: true,
|
|
162
|
+
alternateCommentMode: true,
|
|
163
|
+
};
|
|
164
|
+
if (typeof this.config.source === 'object') {
|
|
165
|
+
fileGlob = this.config.source.file;
|
|
166
|
+
options = {
|
|
167
|
+
...options,
|
|
168
|
+
...this.config.source.load,
|
|
169
|
+
includeDirs: this.config.source.load?.includeDirs?.map(includeDir => cross_helpers_1.path.isAbsolute(includeDir) ? includeDir : cross_helpers_1.path.join(this.baseDir, includeDir)),
|
|
170
|
+
};
|
|
171
|
+
if (options.includeDirs) {
|
|
172
|
+
if (!Array.isArray(options.includeDirs)) {
|
|
173
|
+
throw new Error('The includeDirs option must be an array');
|
|
174
|
+
}
|
|
175
|
+
(0, utils_js_1.addIncludePathResolver)(protoRoot, options.includeDirs);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
fileGlob = this.config.source;
|
|
180
|
+
}
|
|
181
|
+
fileGlob = string_interpolation_1.stringInterpolator.parse(fileGlob, { env: globalThis.process?.env });
|
|
182
|
+
const fileNames = await (0, load_files_1.matchFiles)(fileGlob, {
|
|
183
|
+
cwd: this.baseDir,
|
|
184
|
+
absolute: false,
|
|
185
|
+
});
|
|
186
|
+
this.log.debug(`Loading proto files(${fileGlob}); \n ${fileNames.join('\n')} `);
|
|
187
|
+
protoRoot = await protoRoot.load(fileNames.map(filePath => cross_helpers_1.path.isAbsolute(filePath) ? filePath : cross_helpers_1.path.join(this.baseDir, filePath)), options);
|
|
188
|
+
this.log.debug(`Adding proto content to the root`);
|
|
189
|
+
return protoRoot;
|
|
190
|
+
}
|
|
191
|
+
async getDescriptorSets(creds) {
|
|
192
|
+
const rootPromises = [];
|
|
193
|
+
this.log.debug(`Building Roots`);
|
|
194
|
+
const filePath = this.config.source
|
|
195
|
+
? typeof this.config.source === 'string'
|
|
196
|
+
? this.config.source
|
|
197
|
+
: this.config.source.file
|
|
198
|
+
: undefined;
|
|
199
|
+
// Missing or empty source means gRPC reflection (also accepts source: { file: '' } workaround)
|
|
200
|
+
if (filePath?.endsWith('json')) {
|
|
201
|
+
rootPromises.push(this.processDescriptorFile());
|
|
202
|
+
}
|
|
203
|
+
else if (filePath?.endsWith('proto')) {
|
|
204
|
+
rootPromises.push(this.processProtoFile());
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const reflectionPromises = await this.processReflection(creds);
|
|
208
|
+
rootPromises.push(...reflectionPromises);
|
|
209
|
+
}
|
|
210
|
+
return Promise.all(rootPromises.map(async (root$, i) => {
|
|
211
|
+
const root = await root$;
|
|
212
|
+
const rootName = root.name || `Root${i}`;
|
|
213
|
+
const rootLogger = this.log.child({ root: rootName });
|
|
214
|
+
rootLogger.debug(`Resolving entire the root tree`);
|
|
215
|
+
root.resolveAll();
|
|
216
|
+
rootLogger.debug(`Creating artifacts from descriptor set and root`);
|
|
217
|
+
return {
|
|
218
|
+
name: rootName,
|
|
219
|
+
rootJson: root.toJSON({
|
|
220
|
+
keepComments: true,
|
|
221
|
+
}),
|
|
222
|
+
};
|
|
223
|
+
}));
|
|
224
|
+
}
|
|
225
|
+
getCredentials() {
|
|
226
|
+
this.log.debug(`Getting channel credentials`);
|
|
227
|
+
if (this.config.credentialsSsl) {
|
|
228
|
+
this.log.debug(() => `Using SSL Connection with credentials at ${this.config.credentialsSsl.privateKey} & ${this.config.credentialsSsl.certChain}`);
|
|
229
|
+
const absolutePrivateKeyPath = cross_helpers_1.path.isAbsolute(this.config.credentialsSsl.privateKey)
|
|
230
|
+
? this.config.credentialsSsl.privateKey
|
|
231
|
+
: cross_helpers_1.path.join(this.baseDir, this.config.credentialsSsl.privateKey);
|
|
232
|
+
const absoluteCertChainPath = cross_helpers_1.path.isAbsolute(this.config.credentialsSsl.certChain)
|
|
233
|
+
? this.config.credentialsSsl.certChain
|
|
234
|
+
: cross_helpers_1.path.join(this.baseDir, this.config.credentialsSsl.certChain);
|
|
235
|
+
const sslFiles = [
|
|
236
|
+
cross_helpers_1.fs.promises.readFile(absolutePrivateKeyPath),
|
|
237
|
+
cross_helpers_1.fs.promises.readFile(absoluteCertChainPath),
|
|
238
|
+
];
|
|
239
|
+
if (this.config.credentialsSsl.rootCA !== 'rootCA') {
|
|
240
|
+
const absoluteRootCAPath = cross_helpers_1.path.isAbsolute(this.config.credentialsSsl.rootCA)
|
|
241
|
+
? this.config.credentialsSsl.rootCA
|
|
242
|
+
: cross_helpers_1.path.join(this.baseDir, this.config.credentialsSsl.rootCA);
|
|
243
|
+
sslFiles.unshift(cross_helpers_1.fs.promises.readFile(absoluteRootCAPath));
|
|
244
|
+
}
|
|
245
|
+
return Promise.all(sslFiles).then(([rootCA, privateKey, certChain]) => grpc_js_1.credentials.createSsl(rootCA, privateKey, certChain));
|
|
246
|
+
}
|
|
247
|
+
else if (this.config.useHTTPS) {
|
|
248
|
+
this.log.debug(`Using SSL Connection`);
|
|
249
|
+
return grpc_js_1.credentials.createSsl();
|
|
250
|
+
}
|
|
251
|
+
this.log.debug(`Using insecure connection`);
|
|
252
|
+
return grpc_js_1.credentials.createInsecure();
|
|
253
|
+
}
|
|
254
|
+
visit({ nested, name, currentPath, rootJsonName, rootJson, rootLogger: log, }) {
|
|
255
|
+
const pathWithName = [...currentPath, ...name.split('.')].filter(Boolean);
|
|
256
|
+
if ('nested' in nested) {
|
|
257
|
+
for (const key in nested.nested) {
|
|
258
|
+
log.debug(`Visiting ${currentPath}.nested[${key}]`);
|
|
259
|
+
const currentNested = nested.nested[key];
|
|
260
|
+
this.visit({
|
|
261
|
+
nested: currentNested,
|
|
262
|
+
name: key,
|
|
263
|
+
currentPath: pathWithName,
|
|
264
|
+
rootJsonName,
|
|
265
|
+
rootJson,
|
|
266
|
+
rootLogger: log,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const typeName = pathWithName.join('__');
|
|
271
|
+
if ('values' in nested) {
|
|
272
|
+
const enumValues = {};
|
|
273
|
+
const commentMap = nested.comments;
|
|
274
|
+
for (const [key] of Object.entries(nested.values)) {
|
|
275
|
+
log.debug(`Visiting ${currentPath}.nested.values[${key}]`);
|
|
276
|
+
enumValues[key] = {
|
|
277
|
+
description: commentMap?.[key],
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
this.schemaComposer.createEnumTC({
|
|
281
|
+
name: typeName,
|
|
282
|
+
values: enumValues,
|
|
283
|
+
description: nested.comment,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
else if ('fields' in nested) {
|
|
287
|
+
const inputTypeName = typeName + '_Input';
|
|
288
|
+
const outputTypeName = typeName;
|
|
289
|
+
const description = nested.comment;
|
|
290
|
+
const fieldEntries = Object.entries(nested.fields);
|
|
291
|
+
if (fieldEntries.length) {
|
|
292
|
+
const inputTC = this.schemaComposer.createInputTC({
|
|
293
|
+
name: inputTypeName,
|
|
294
|
+
description,
|
|
295
|
+
fields: {},
|
|
296
|
+
});
|
|
297
|
+
const outputTC = this.schemaComposer.createObjectTC({
|
|
298
|
+
name: outputTypeName,
|
|
299
|
+
description,
|
|
300
|
+
fields: {},
|
|
301
|
+
});
|
|
302
|
+
for (const [fieldName, { type, rule, comment, keyType }] of fieldEntries) {
|
|
303
|
+
log.debug(`Visiting ${currentPath}.nested.fields[${fieldName}]`);
|
|
304
|
+
const baseFieldTypePath = type.split('.');
|
|
305
|
+
inputTC.addFields({
|
|
306
|
+
[fieldName]: {
|
|
307
|
+
type: () => {
|
|
308
|
+
let fieldInputTypeName;
|
|
309
|
+
if (keyType) {
|
|
310
|
+
fieldInputTypeName = 'JSON';
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
const fieldTypePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, baseFieldTypePath);
|
|
314
|
+
fieldInputTypeName = (0, utils_js_1.getTypeName)(this.schemaComposer, fieldTypePath, true);
|
|
315
|
+
}
|
|
316
|
+
return rule === 'repeated' ? `[${fieldInputTypeName}]` : fieldInputTypeName;
|
|
317
|
+
},
|
|
318
|
+
description: comment,
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
outputTC.addFields({
|
|
322
|
+
[fieldName]: {
|
|
323
|
+
type: () => {
|
|
324
|
+
let fieldTypeName;
|
|
325
|
+
if (keyType) {
|
|
326
|
+
fieldTypeName = 'JSON';
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
const fieldTypePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, baseFieldTypePath);
|
|
330
|
+
fieldTypeName = (0, utils_js_1.getTypeName)(this.schemaComposer, fieldTypePath, false);
|
|
331
|
+
}
|
|
332
|
+
return rule === 'repeated' ? `[${fieldTypeName}]` : fieldTypeName;
|
|
333
|
+
},
|
|
334
|
+
description: comment,
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
this.schemaComposer.createScalarTC({
|
|
341
|
+
...graphql_scalars_1.GraphQLJSON.toConfig(),
|
|
342
|
+
name: inputTypeName,
|
|
343
|
+
description,
|
|
344
|
+
});
|
|
345
|
+
this.schemaComposer.createScalarTC({
|
|
346
|
+
...graphql_scalars_1.GraphQLJSON.toConfig(),
|
|
347
|
+
name: outputTypeName,
|
|
348
|
+
description,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
else if ('methods' in nested) {
|
|
353
|
+
const objPath = pathWithName.join('.');
|
|
354
|
+
for (const methodName in nested.methods) {
|
|
355
|
+
const method = nested.methods[methodName];
|
|
356
|
+
const rootFieldName = [...pathWithName, methodName].join('_');
|
|
357
|
+
const fieldConfigTypeFactory = () => {
|
|
358
|
+
const baseResponseTypePath = method.responseType?.split('.');
|
|
359
|
+
if (baseResponseTypePath) {
|
|
360
|
+
const responseTypePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, baseResponseTypePath);
|
|
361
|
+
return (0, utils_js_1.getTypeName)(this.schemaComposer, responseTypePath, false);
|
|
362
|
+
}
|
|
363
|
+
return 'Void';
|
|
364
|
+
};
|
|
365
|
+
const fieldConfig = {
|
|
366
|
+
type: () => {
|
|
367
|
+
const typeName = fieldConfigTypeFactory();
|
|
368
|
+
if (method.responseStream) {
|
|
369
|
+
return `[${typeName}]`;
|
|
370
|
+
}
|
|
371
|
+
return typeName;
|
|
372
|
+
},
|
|
373
|
+
description: method.comment,
|
|
374
|
+
};
|
|
375
|
+
const fieldConfigArgs = {
|
|
376
|
+
input: () => {
|
|
377
|
+
if (method.requestStream) {
|
|
378
|
+
return 'File';
|
|
379
|
+
}
|
|
380
|
+
const baseRequestTypePath = method.requestType?.split('.');
|
|
381
|
+
if (baseRequestTypePath) {
|
|
382
|
+
const requestTypePath = (0, utils_js_1.walkToFindTypePath)(rootJson, pathWithName, baseRequestTypePath);
|
|
383
|
+
const requestTypeName = (0, utils_js_1.getTypeName)(this.schemaComposer, requestTypePath, true);
|
|
384
|
+
return requestTypeName;
|
|
385
|
+
}
|
|
386
|
+
return undefined;
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
fieldConfig.args = fieldConfigArgs;
|
|
390
|
+
const methodNameLowerCased = methodName.toLowerCase();
|
|
391
|
+
const prefixQueryMethod = this.config.prefixQueryMethod || QUERY_METHOD_PREFIXES;
|
|
392
|
+
let rootTypeComposer;
|
|
393
|
+
if (this.config.selectQueryOrMutationField) {
|
|
394
|
+
const selection = this.config.selectQueryOrMutationField.find(selection => (0, micromatch_1.default)([rootFieldName], selection.fieldName).length > 0);
|
|
395
|
+
const rootTypeName = selection?.type?.toLowerCase();
|
|
396
|
+
if (rootTypeName) {
|
|
397
|
+
if (rootTypeName === 'query') {
|
|
398
|
+
rootTypeComposer = this.schemaComposer.Query;
|
|
399
|
+
}
|
|
400
|
+
else if (rootTypeName === 'mutation') {
|
|
401
|
+
rootTypeComposer = this.schemaComposer.Mutation;
|
|
402
|
+
}
|
|
403
|
+
else if (rootTypeName === 'subscription') {
|
|
404
|
+
rootTypeComposer = this.schemaComposer.Subscription;
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
throw new Error(`Unknown type provided ${selection.type} for ${rootFieldName}; available options are Query, Mutation and Subscription`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (rootTypeComposer == null) {
|
|
412
|
+
rootTypeComposer = prefixQueryMethod.some(prefix => methodNameLowerCased.startsWith(prefix))
|
|
413
|
+
? this.schemaComposer.Query
|
|
414
|
+
: this.schemaComposer.Mutation;
|
|
415
|
+
}
|
|
416
|
+
const serviceName = pathWithName.join('.') || objPath;
|
|
417
|
+
const argsMapping = method.requestType
|
|
418
|
+
? (0, emitMapping_js_1.emitRequestBody)(rootJson, pathWithName, method.requestType)
|
|
419
|
+
: '';
|
|
420
|
+
const selection = method.responseType
|
|
421
|
+
? (0, emitMapping_js_1.emitResponseSelection)(rootJson, pathWithName, method.responseType)
|
|
422
|
+
: '';
|
|
423
|
+
const rpcDirective = {
|
|
424
|
+
name: 'protobuf__rpc',
|
|
425
|
+
args: {
|
|
426
|
+
subgraph: this.subgraphName,
|
|
427
|
+
service: serviceName,
|
|
428
|
+
method: methodName,
|
|
429
|
+
args: argsMapping,
|
|
430
|
+
selection,
|
|
431
|
+
stream: !!method.responseStream,
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
this.schemaComposer.addDirective(directives_js_1.protobufRpcDirective);
|
|
435
|
+
rootTypeComposer.addFields({
|
|
436
|
+
[rootFieldName]: {
|
|
437
|
+
...fieldConfig,
|
|
438
|
+
directives: [rpcDirective],
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
if (method.responseStream) {
|
|
442
|
+
this.schemaComposer.Subscription.addFields({
|
|
443
|
+
[rootFieldName]: {
|
|
444
|
+
args: fieldConfigArgs,
|
|
445
|
+
description: method.comment,
|
|
446
|
+
type: fieldConfigTypeFactory,
|
|
447
|
+
directives: [
|
|
448
|
+
{
|
|
449
|
+
...rpcDirective,
|
|
450
|
+
args: { ...rpcDirective.args, stream: true },
|
|
451
|
+
},
|
|
452
|
+
],
|
|
453
|
+
},
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// connectivity state removed from protobuf author surface
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
exports.GrpcLoaderHelper = GrpcLoaderHelper;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadProtobufSubgraph = loadProtobufSubgraph;
|
|
4
|
+
const grpcLoaderHelper_js_1 = require("./grpcLoaderHelper.js");
|
|
5
|
+
/**
|
|
6
|
+
* Load a protobuf-RPC subgraph from `.proto` / reflection.
|
|
7
|
+
*/
|
|
8
|
+
function loadProtobufSubgraph(name, options) {
|
|
9
|
+
return (ctx) => {
|
|
10
|
+
const helper = new grpcLoaderHelper_js_1.GrpcLoaderHelper(name, ctx.cwd, ctx.log, {
|
|
11
|
+
...options,
|
|
12
|
+
// type reserved for future connect/twirp emit path conventions
|
|
13
|
+
});
|
|
14
|
+
const schema$ = helper.buildSchema().finally(() => helper.dispose());
|
|
15
|
+
return {
|
|
16
|
+
name,
|
|
17
|
+
schema$,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|