@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.
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isGrpcScalar = isGrpcScalar;
4
+ exports.getGraphQLScalarForGrpc = getGraphQLScalarForGrpc;
5
+ const SCALARS = new Map([
6
+ ['bool', 'Boolean'],
7
+ ['bytes', 'Byte'],
8
+ ['double', 'Float'],
9
+ ['fixed32', 'Int'],
10
+ ['fixed64', 'BigInt'],
11
+ ['float', 'Float'],
12
+ ['int32', 'Int'],
13
+ ['int64', 'BigInt'],
14
+ ['sfixed32', 'Int'],
15
+ ['sfixed64', 'BigInt'],
16
+ ['sint32', 'Int'],
17
+ ['sint64', 'BigInt'],
18
+ ['string', 'String'],
19
+ ['uint32', 'UnsignedInt'],
20
+ ['uint64', 'BigInt'], // A new scalar might be needed
21
+ ]);
22
+ function isGrpcScalar(type) {
23
+ return SCALARS.has(type);
24
+ }
25
+ function getGraphQLScalarForGrpc(scalarType) {
26
+ const gqlScalar = SCALARS.get(scalarType);
27
+ if (!gqlScalar) {
28
+ throw new Error(`Could not find GraphQL Scalar for type ${scalarType}`);
29
+ }
30
+ return SCALARS.get(scalarType);
31
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addIncludePathResolver = addIncludePathResolver;
4
+ exports.getTypeName = getTypeName;
5
+ exports.walkToFindTypePath = walkToFindTypePath;
6
+ const tslib_1 = require("tslib");
7
+ const lodash_has_1 = tslib_1.__importDefault(require("lodash.has"));
8
+ const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
9
+ const scalars_js_1 = require("./scalars.js");
10
+ function addIncludePathResolver(root, includePaths) {
11
+ const originalResolvePath = root.resolvePath;
12
+ root.resolvePath = (origin, target) => {
13
+ if (cross_helpers_1.path.isAbsolute(target)) {
14
+ return target;
15
+ }
16
+ for (const directory of includePaths) {
17
+ const fullPath = cross_helpers_1.path.join(directory, target);
18
+ if (cross_helpers_1.fs.existsSync(fullPath)) {
19
+ return fullPath;
20
+ }
21
+ }
22
+ const path = originalResolvePath(origin, target);
23
+ if (path === null) {
24
+ console.warn(`${target} not found in any of the include paths ${includePaths}`);
25
+ }
26
+ return path;
27
+ };
28
+ }
29
+ function getTypeName(schemaComposer, pathWithName, isInput) {
30
+ if (pathWithName?.length) {
31
+ const baseTypeName = pathWithName.filter(Boolean).join('__');
32
+ if ((0, scalars_js_1.isGrpcScalar)(baseTypeName)) {
33
+ return (0, scalars_js_1.getGraphQLScalarForGrpc)(baseTypeName);
34
+ }
35
+ if (schemaComposer.isEnumType(baseTypeName)) {
36
+ return baseTypeName;
37
+ }
38
+ return isInput ? baseTypeName + '_Input' : baseTypeName;
39
+ }
40
+ return 'Void';
41
+ }
42
+ function walkToFindTypePath(rootJson, pathWithName, baseTypePath) {
43
+ // Reflection / Fully-qualified names often look like `.echo.GreetingRequest`
44
+ // (`split('.')` → `['', 'echo', …]`). Empty segments break `nested['']` walks.
45
+ const normalizedBase = baseTypePath.filter(Boolean);
46
+ const currentWalkingPath = pathWithName.filter(Boolean);
47
+ while (!(0, lodash_has_1.default)(rootJson.nested, currentWalkingPath.concat(normalizedBase).join('.nested.'))) {
48
+ if (!currentWalkingPath.length) {
49
+ break;
50
+ }
51
+ currentWalkingPath.pop();
52
+ }
53
+ return currentWalkingPath.concat(normalizedBase);
54
+ }
@@ -0,0 +1,29 @@
1
+ import { DirectiveLocation, GraphQLBoolean, GraphQLDirective, GraphQLString } from 'graphql';
2
+ export const protobufRpcDirective = new GraphQLDirective({
3
+ name: 'protobuf__rpc',
4
+ locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],
5
+ args: {
6
+ subgraph: {
7
+ type: GraphQLString,
8
+ },
9
+ service: {
10
+ type: GraphQLString,
11
+ },
12
+ method: {
13
+ type: GraphQLString,
14
+ },
15
+ args: {
16
+ type: GraphQLString,
17
+ },
18
+ selection: {
19
+ type: GraphQLString,
20
+ },
21
+ stream: {
22
+ type: GraphQLBoolean,
23
+ },
24
+ entity: {
25
+ type: GraphQLBoolean,
26
+ },
27
+ },
28
+ });
29
+ export { TransportDirective as transportDirective } from '@graphql-mesh/utils';
@@ -0,0 +1,130 @@
1
+ import { walkToFindTypePath } from './utils.js';
2
+ /**
3
+ * Emit typed protobuf mapping strings from protobufjs JSON namespace (loader-time only).
4
+ * Output is indented and may include `#` comments for humans/AI; the transport parser skips them.
5
+ */
6
+ export function emitEnumValues(rootJson, pathWithName, typeName) {
7
+ const typePath = walkToFindTypePath(rootJson, pathWithName, typeName.split('.'));
8
+ let cur = rootJson;
9
+ for (const p of typePath) {
10
+ cur = cur?.nested?.[p];
11
+ }
12
+ if (!cur?.values) {
13
+ return 'enum{}';
14
+ }
15
+ const parts = Object.entries(cur.values).map(([name, num]) => `${name}=${num}`);
16
+ return `enum{${parts.join(',')}}`;
17
+ }
18
+ export function protoTypeToken(rootJson, pathWithName, field) {
19
+ const base = field.type || 'string';
20
+ let token;
21
+ if (field.keyType) {
22
+ token = `map<${scalarOrEnum(rootJson, pathWithName, field.keyType)},${scalarOrEnum(rootJson, pathWithName, base)}>`;
23
+ }
24
+ else if (isScalar(base)) {
25
+ token = base === 'bytes' ? 'bytes' : normalizeScalar(base);
26
+ }
27
+ else {
28
+ const typePath = walkToFindTypePath(rootJson, pathWithName, base.split('.'));
29
+ let cur = rootJson;
30
+ for (const p of typePath) {
31
+ cur = cur?.nested?.[p];
32
+ }
33
+ if (cur?.values) {
34
+ token = emitEnumValues(rootJson, pathWithName, base);
35
+ }
36
+ else {
37
+ token = 'message';
38
+ }
39
+ }
40
+ if (field.rule === 'repeated') {
41
+ return `repeated ${token}`;
42
+ }
43
+ return token;
44
+ }
45
+ function isScalar(t) {
46
+ return [
47
+ 'double',
48
+ 'float',
49
+ 'int32',
50
+ 'int64',
51
+ 'uint32',
52
+ 'uint64',
53
+ 'sint32',
54
+ 'sint64',
55
+ 'fixed32',
56
+ 'fixed64',
57
+ 'sfixed32',
58
+ 'sfixed64',
59
+ 'bool',
60
+ 'string',
61
+ 'bytes',
62
+ ].includes(t);
63
+ }
64
+ function normalizeScalar(t) {
65
+ return t;
66
+ }
67
+ function scalarOrEnum(rootJson, pathWithName, t) {
68
+ if (isScalar(t))
69
+ return normalizeScalar(t);
70
+ const typePath = walkToFindTypePath(rootJson, pathWithName, t.split('.'));
71
+ let cur = rootJson;
72
+ for (const p of typePath) {
73
+ cur = cur?.nested?.[p];
74
+ }
75
+ if (cur?.values)
76
+ return emitEnumValues(rootJson, pathWithName, t);
77
+ return 'string';
78
+ }
79
+ export function emitFieldsMapping(rootJson, pathWithName, typeName, bindMode, bindPrefix = '', indent = ' ') {
80
+ const typePath = walkToFindTypePath(rootJson, pathWithName, typeName.split('.'));
81
+ let cur = rootJson;
82
+ for (const p of typePath) {
83
+ cur = cur?.nested?.[p];
84
+ }
85
+ const fields = cur?.fields;
86
+ if (!fields)
87
+ return '';
88
+ const lines = [];
89
+ for (const [fieldName, field] of Object.entries(fields)) {
90
+ const id = field.id ?? 0;
91
+ const token = protoTypeToken(rootJson, pathWithName, field);
92
+ const bind = bindMode === 'prefix' && bindPrefix ? `${bindPrefix}.${fieldName}` : fieldName;
93
+ if (token === 'message' || token === 'repeated message') {
94
+ const nestedType = field.type;
95
+ const nested = emitFieldsMapping(rootJson, pathWithName, nestedType, 'relative', '', indent + ' ');
96
+ lines.push(`${indent}${id}(${token}): ${bind} {`);
97
+ if (nested) {
98
+ lines.push(nested);
99
+ }
100
+ lines.push(`${indent}}`);
101
+ }
102
+ else {
103
+ lines.push(`${indent}${id}(${token}): ${bind}`);
104
+ }
105
+ }
106
+ return lines.join('\n');
107
+ }
108
+ /** Request: GraphQL args use `input` wrapping the request message fields. */
109
+ export function emitRequestBody(rootJson, pathWithName, requestType) {
110
+ const inner = emitFieldsMapping(rootJson, pathWithName, requestType, 'prefix', 'input', ' ');
111
+ const header = [
112
+ '# Encode GraphQL field arguments into the protobuf request message.',
113
+ '# Each line is fieldId(protoType): graphqlPath (relative to this field args).',
114
+ ].join('\n');
115
+ if (!inner.trim()) {
116
+ return `${header}\n`;
117
+ }
118
+ return `${header}\n${inner}\n`;
119
+ }
120
+ export function emitResponseSelection(rootJson, pathWithName, responseType) {
121
+ const inner = emitFieldsMapping(rootJson, pathWithName, responseType, 'relative', '', ' ');
122
+ const header = [
123
+ '# Decode the protobuf response message into GraphQL fields.',
124
+ '# Each line is fieldId(protoType): graphqlFieldName.',
125
+ ].join('\n');
126
+ if (!inner.trim()) {
127
+ return `${header}\n`;
128
+ }
129
+ return `${header}\n${inner}\n`;
130
+ }