@graphql-mesh/transport-protobuf 0.0.0-stage → 0.7.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 +1225 -0
- package/dist/cjs/index.js +394 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/patchLongJs.js +14 -0
- package/dist/cjs/protobuf/codec.js +393 -0
- package/dist/cjs/protobuf/dial.js +40 -0
- package/dist/cjs/protobuf/mapping.js +226 -0
- package/dist/cjs/utils.js +129 -0
- package/dist/esm/index.js +390 -0
- package/dist/esm/patchLongJs.js +11 -0
- package/dist/esm/protobuf/codec.js +389 -0
- package/dist/esm/protobuf/dial.js +33 -0
- package/dist/esm/protobuf/mapping.js +222 -0
- package/dist/esm/utils.js +122 -0
- package/dist/package.json +61 -0
- package/dist/typings/index.d.cts +41 -0
- package/dist/typings/index.d.ts +41 -0
- package/dist/typings/patchLongJs.d.cts +1 -0
- package/dist/typings/patchLongJs.d.ts +1 -0
- package/dist/typings/protobuf/codec.d.cts +3 -0
- package/dist/typings/protobuf/codec.d.ts +3 -0
- package/dist/typings/protobuf/dial.d.cts +16 -0
- package/dist/typings/protobuf/dial.d.ts +16 -0
- package/dist/typings/protobuf/mapping.d.cts +31 -0
- package/dist/typings/protobuf/mapping.d.ts +31 -0
- package/dist/typings/utils.d.cts +12 -0
- package/dist/typings/utils.d.ts +12 -0
- package/package.json +66 -4
- package/src/index.ts +562 -0
- package/src/patchLongJs.ts +12 -0
- package/src/protobuf/codec.ts +429 -0
- package/src/protobuf/dial.ts +56 -0
- package/src/protobuf/mapping.ts +259 -0
- package/src/utils.ts +158 -0
- package/test/mapping-codec.spec.ts +52 -0
- package/test/utils.spec.ts +182 -0
- package/README.md +0 -3
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toGrpcGraphQLError = toGrpcGraphQLError;
|
|
4
|
+
exports.buildGrpcMetadata = buildGrpcMetadata;
|
|
5
|
+
exports.addMetaDataToCall = addMetaDataToCall;
|
|
6
|
+
exports.addExecutionLogicToScalar = addExecutionLogicToScalar;
|
|
7
|
+
const tslib_1 = require("tslib");
|
|
8
|
+
const graphql_1 = require("graphql");
|
|
9
|
+
const lodash_get_1 = tslib_1.__importDefault(require("lodash.get"));
|
|
10
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
11
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
12
|
+
const grpc_js_1 = require("@grpc/grpc-js");
|
|
13
|
+
function isBlob(input) {
|
|
14
|
+
return input != null && input.stream instanceof Function;
|
|
15
|
+
}
|
|
16
|
+
function isGrpcServiceError(error) {
|
|
17
|
+
return (typeof error === 'object' &&
|
|
18
|
+
error != null &&
|
|
19
|
+
typeof error.code === 'number' &&
|
|
20
|
+
typeof error.details === 'string');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Convert a gRPC `ServiceError` into a GraphQLError so gateways do not mask it.
|
|
24
|
+
* Upstream details live under `extensions.grpc` (code, status, details, metadata).
|
|
25
|
+
* Non-ServiceError values are returned unchanged.
|
|
26
|
+
*/
|
|
27
|
+
function toGrpcGraphQLError(error) {
|
|
28
|
+
if (!isGrpcServiceError(error)) {
|
|
29
|
+
return error;
|
|
30
|
+
}
|
|
31
|
+
const status = grpc_js_1.status[error.code] != null ? String(grpc_js_1.status[error.code]) : 'UNKNOWN';
|
|
32
|
+
// Do not set `originalError` to the raw ServiceError — Envelop's maskedErrors
|
|
33
|
+
// walks originalError and would treat a non-GraphQLError root as unexpected.
|
|
34
|
+
return new graphql_1.GraphQLError(error.message || status, {
|
|
35
|
+
extensions: {
|
|
36
|
+
code: 'DOWNSTREAM_SERVICE_ERROR',
|
|
37
|
+
grpc: {
|
|
38
|
+
code: error.code,
|
|
39
|
+
status,
|
|
40
|
+
details: error.details,
|
|
41
|
+
metadata: error.metadata?.getMap?.(),
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function buildGrpcMetadata(metaData, resolverData) {
|
|
47
|
+
if (!metaData) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const meta = new grpc_js_1.Metadata();
|
|
51
|
+
const entries = Array.isArray(metaData) ? Object.fromEntries(metaData) : metaData;
|
|
52
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
53
|
+
let metaValue = value;
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
// Extract data from context
|
|
56
|
+
metaValue = (0, lodash_get_1.default)(resolverData.context, value);
|
|
57
|
+
}
|
|
58
|
+
// Ensure that the metadata is compatible with what node-grpc expects
|
|
59
|
+
if (typeof metaValue !== 'string' && !(metaValue instanceof Buffer)) {
|
|
60
|
+
metaValue = JSON.stringify(metaValue);
|
|
61
|
+
}
|
|
62
|
+
if (typeof metaValue === 'string') {
|
|
63
|
+
metaValue = string_interpolation_1.stringInterpolator.parse(metaValue, resolverData);
|
|
64
|
+
}
|
|
65
|
+
meta.add(key, metaValue);
|
|
66
|
+
}
|
|
67
|
+
return meta;
|
|
68
|
+
}
|
|
69
|
+
function addMetaDataToCall(callFn, input, resolverData, metaData, isResponseStream = false, requestTimeout) {
|
|
70
|
+
const callFnArguments = [];
|
|
71
|
+
if (!isBlob(input)) {
|
|
72
|
+
callFnArguments.push(input);
|
|
73
|
+
}
|
|
74
|
+
const meta = buildGrpcMetadata(metaData, resolverData);
|
|
75
|
+
const callOptions = {};
|
|
76
|
+
if (requestTimeout != null && requestTimeout > 0) {
|
|
77
|
+
callOptions.deadline = Date.now() + requestTimeout;
|
|
78
|
+
}
|
|
79
|
+
const hasCallOptions = Object.keys(callOptions).length > 0;
|
|
80
|
+
if (meta || hasCallOptions) {
|
|
81
|
+
// grpc-js accepts (argument, metadata, options, callback)
|
|
82
|
+
callFnArguments.push(meta ?? new grpc_js_1.Metadata());
|
|
83
|
+
if (hasCallOptions) {
|
|
84
|
+
callFnArguments.push(callOptions);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
88
|
+
const call = callFn(...callFnArguments, (error, response) => {
|
|
89
|
+
if (error) {
|
|
90
|
+
reject(toGrpcGraphQLError(error));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
resolve(response);
|
|
94
|
+
});
|
|
95
|
+
if (isResponseStream) {
|
|
96
|
+
let isCancelled = false;
|
|
97
|
+
const responseStreamWithCancel = (0, utils_1.withCancel)(call, reason => {
|
|
98
|
+
if (!isCancelled) {
|
|
99
|
+
call.call?.cancelWithStatus(0, reason?.toString() || 'aborted');
|
|
100
|
+
isCancelled = true;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
resolve(responseStreamWithCancel);
|
|
104
|
+
if (isBlob(input)) {
|
|
105
|
+
const blobStream = input.stream();
|
|
106
|
+
blobStream.pipe(call);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return promise;
|
|
110
|
+
}
|
|
111
|
+
function addExecutionLogicToScalar(nonExecutableScalar, actualScalar) {
|
|
112
|
+
Object.defineProperties(nonExecutableScalar, {
|
|
113
|
+
serialize: {
|
|
114
|
+
value: actualScalar.serialize,
|
|
115
|
+
},
|
|
116
|
+
parseValue: {
|
|
117
|
+
value: actualScalar.parseValue,
|
|
118
|
+
},
|
|
119
|
+
parseLiteral: {
|
|
120
|
+
value: actualScalar.parseLiteral,
|
|
121
|
+
},
|
|
122
|
+
extensions: {
|
|
123
|
+
value: {
|
|
124
|
+
...actualScalar.extensions,
|
|
125
|
+
...nonExecutableScalar.extensions,
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { entitiesResolver } from '@apollo/subgraph';
|
|
2
|
+
import { GraphQLError, GraphQLList, GraphQLNonNull, GraphQLScalarType, GraphQLUnionType, getNamedType, isObjectType, } from 'graphql';
|
|
3
|
+
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
|
|
4
|
+
import { createDefaultExecutor } from '@graphql-mesh/transport-common';
|
|
5
|
+
import { getDirectives } from '@graphql-tools/utils';
|
|
6
|
+
import { credentials, Metadata, } from '@grpc/grpc-js';
|
|
7
|
+
import { makeGenericClientConstructor } from '@grpc/grpc-js';
|
|
8
|
+
import { DisposableStack, DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
9
|
+
import { fs, path, process } from '@graphql-mesh/cross-helpers';
|
|
10
|
+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
|
|
11
|
+
import { fetch as defaultFetch } from '@whatwg-node/fetch';
|
|
12
|
+
import { decodeMessage, encodeMessage } from './protobuf/codec.js';
|
|
13
|
+
import { buildRpcPath, httpProtobufUnary, isHttpLocation, resolveHttpUrl, } from './protobuf/dial.js';
|
|
14
|
+
import { parseMapping } from './protobuf/mapping.js';
|
|
15
|
+
import { toGrpcGraphQLError, buildGrpcMetadata } from './utils.js';
|
|
16
|
+
function setTypeResolveReference(type, resolveReference) {
|
|
17
|
+
const extensions = (type.extensions ||= {});
|
|
18
|
+
const apollo = (extensions.apollo ||= {});
|
|
19
|
+
const subgraph = (apollo.subgraph ||= {});
|
|
20
|
+
subgraph.resolveReference = resolveReference;
|
|
21
|
+
type.resolveReference = resolveReference;
|
|
22
|
+
}
|
|
23
|
+
function getTypeResolveReference(type) {
|
|
24
|
+
const fromExtensions = type.extensions?.apollo?.subgraph?.resolveReference;
|
|
25
|
+
if (typeof fromExtensions === 'function') {
|
|
26
|
+
return fromExtensions;
|
|
27
|
+
}
|
|
28
|
+
const fromType = type.resolveReference;
|
|
29
|
+
return typeof fromType === 'function' ? fromType : undefined;
|
|
30
|
+
}
|
|
31
|
+
export class ProtobufTransportHelper extends DisposableStack {
|
|
32
|
+
constructor(subgraphName, baseDir, log, endpoint, config) {
|
|
33
|
+
super();
|
|
34
|
+
this.subgraphName = subgraphName;
|
|
35
|
+
this.baseDir = baseDir;
|
|
36
|
+
this.log = log;
|
|
37
|
+
this.endpoint = endpoint;
|
|
38
|
+
this.config = config;
|
|
39
|
+
this.fetchFn = defaultFetch;
|
|
40
|
+
}
|
|
41
|
+
setFetch(fetchFn) {
|
|
42
|
+
this.fetchFn = fetchFn || defaultFetch;
|
|
43
|
+
}
|
|
44
|
+
getCredentials() {
|
|
45
|
+
if (this.config.credentialsSsl) {
|
|
46
|
+
const absolutePrivateKeyPath = path.isAbsolute(this.config.credentialsSsl.privateKey)
|
|
47
|
+
? this.config.credentialsSsl.privateKey
|
|
48
|
+
: path.join(this.baseDir, this.config.credentialsSsl.privateKey);
|
|
49
|
+
const absoluteCertChainPath = path.isAbsolute(this.config.credentialsSsl.certChain)
|
|
50
|
+
? this.config.credentialsSsl.certChain
|
|
51
|
+
: path.join(this.baseDir, this.config.credentialsSsl.certChain);
|
|
52
|
+
const sslFiles = [
|
|
53
|
+
fs.promises.readFile(absolutePrivateKeyPath),
|
|
54
|
+
fs.promises.readFile(absoluteCertChainPath),
|
|
55
|
+
];
|
|
56
|
+
if (this.config.credentialsSsl.rootCA && this.config.credentialsSsl.rootCA !== 'rootCA') {
|
|
57
|
+
const absoluteRootCAPath = path.isAbsolute(this.config.credentialsSsl.rootCA)
|
|
58
|
+
? this.config.credentialsSsl.rootCA
|
|
59
|
+
: path.join(this.baseDir, this.config.credentialsSsl.rootCA);
|
|
60
|
+
sslFiles.unshift(fs.promises.readFile(absoluteRootCAPath));
|
|
61
|
+
}
|
|
62
|
+
return Promise.all(sslFiles).then(([rootCA, privateKey, certChain]) => credentials.createSsl(rootCA, privateKey, certChain));
|
|
63
|
+
}
|
|
64
|
+
if (this.config.useHTTPS) {
|
|
65
|
+
return credentials.createSsl();
|
|
66
|
+
}
|
|
67
|
+
return credentials.createInsecure();
|
|
68
|
+
}
|
|
69
|
+
processDirectives({ schema, creds }) {
|
|
70
|
+
this.channelCreds = creds;
|
|
71
|
+
const location = stringInterpolator.parse(this.endpoint, { env: process.env }) ?? this.endpoint;
|
|
72
|
+
const channelOptions = Array.isArray(this.config.channelOptions)
|
|
73
|
+
? Object.fromEntries(this.config.channelOptions)
|
|
74
|
+
: this.config.channelOptions;
|
|
75
|
+
const queryType = schema.getQueryType();
|
|
76
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
77
|
+
if (!isObjectType(type) || type.name.startsWith('__')) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const typeName = type.name;
|
|
81
|
+
// Type-level `@protobuf__rpc` → resolveReference (entity fill / `$this` binds).
|
|
82
|
+
const typeDirectives = getDirectives(schema, type) || [];
|
|
83
|
+
for (const directiveObj of typeDirectives) {
|
|
84
|
+
if (this.subgraphName &&
|
|
85
|
+
directiveObj.args?.subgraph &&
|
|
86
|
+
directiveObj.args.subgraph !== this.subgraphName) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (directiveObj.name !== 'protobuf__rpc' && directiveObj.name !== 'protobufRpc') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const resolver = this.createRpcResolverFromDirectiveArgs(directiveObj.args, location, channelOptions, typeName);
|
|
93
|
+
setTypeResolveReference(type, (reference, context, info) => resolver(reference, {}, context, info));
|
|
94
|
+
}
|
|
95
|
+
for (const field of Object.values(type.getFields())) {
|
|
96
|
+
const directives = getDirectives(schema, field) || [];
|
|
97
|
+
for (const directiveObj of directives) {
|
|
98
|
+
if (this.subgraphName &&
|
|
99
|
+
directiveObj.args?.subgraph &&
|
|
100
|
+
directiveObj.args.subgraph !== this.subgraphName) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (directiveObj.name !== 'protobuf__rpc' && directiveObj.name !== 'protobufRpc') {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const isEntity = !!directiveObj.args?.entity;
|
|
107
|
+
const resolver = this.createRpcResolverFromDirectiveArgs(directiveObj.args, location, channelOptions, typeName);
|
|
108
|
+
// Subscriptions use `subscribe` (async iterable). Query/Mutation server-stream
|
|
109
|
+
// fields keep `resolve` so the resolver can collect chunks into a list (and
|
|
110
|
+
// `@stream` can incremental-deliver that iterable).
|
|
111
|
+
if (typeName === 'Subscription') {
|
|
112
|
+
field.subscribe = resolver;
|
|
113
|
+
field.resolve = (x) => x;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
field.resolve = resolver;
|
|
117
|
+
}
|
|
118
|
+
// Query `@protobuf__rpc(entity: true)` → type.resolveReference for Hive `_entities`.
|
|
119
|
+
if (isEntity && type === queryType && isObjectType(getNamedType(field.type))) {
|
|
120
|
+
const returnType = getNamedType(field.type);
|
|
121
|
+
if (!getTypeResolveReference(returnType)) {
|
|
122
|
+
const entityField = field;
|
|
123
|
+
setTypeResolveReference(returnType, (reference, context, info) => {
|
|
124
|
+
const args = {};
|
|
125
|
+
for (const arg of entityField.args) {
|
|
126
|
+
args[arg.name] = reference?.[arg.name];
|
|
127
|
+
}
|
|
128
|
+
return entityField.resolve?.(null, args, context, info);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
ensureFederationEntitiesField(schema);
|
|
136
|
+
}
|
|
137
|
+
createRpcResolverFromDirectiveArgs(directiveArgs, location, channelOptions, typeName) {
|
|
138
|
+
const { service, method, args: argsMapping, selection, stream: isStream, } = directiveArgs;
|
|
139
|
+
return this.createRpcResolver({
|
|
140
|
+
location,
|
|
141
|
+
service,
|
|
142
|
+
method,
|
|
143
|
+
argsFields: parseMapping(argsMapping),
|
|
144
|
+
selectionFields: parseMapping(selection),
|
|
145
|
+
isStream: !!isStream,
|
|
146
|
+
channelOptions,
|
|
147
|
+
typeName,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
createRpcResolver({ location, service, method, argsFields, selectionFields, isStream, channelOptions, typeName, }) {
|
|
151
|
+
const metaData = this.config.metaData;
|
|
152
|
+
const headersConfig = this.config.headers;
|
|
153
|
+
const timeout = this.config.requestTimeout;
|
|
154
|
+
const creds = this.channelCreds ?? credentials.createInsecure();
|
|
155
|
+
const http = isHttpLocation(location);
|
|
156
|
+
const rpcPath = buildRpcPath(service, method);
|
|
157
|
+
const transportFetch = this.fetchFn;
|
|
158
|
+
return async function protobufRpcResolver(_root, args, context, info) {
|
|
159
|
+
if (isStream && http) {
|
|
160
|
+
throw new GraphQLError('Server streaming protobuf RPCs require a gRPC location (not HTTP Connect/Twirp)');
|
|
161
|
+
}
|
|
162
|
+
// Type-level entity resolvers pass the representation as `_root` and empty args;
|
|
163
|
+
// `$this.*` binds read from that root.
|
|
164
|
+
const thisRoot = _root != null && typeof _root === 'object' ? _root : undefined;
|
|
165
|
+
const requestBytes = encodeMessage(argsFields, args ?? {}, args ?? {}, thisRoot);
|
|
166
|
+
try {
|
|
167
|
+
if (http) {
|
|
168
|
+
const url = resolveHttpUrl(location, service, method);
|
|
169
|
+
const headers = {};
|
|
170
|
+
const rawHeaders = Array.isArray(headersConfig)
|
|
171
|
+
? Object.fromEntries(headersConfig)
|
|
172
|
+
: headersConfig;
|
|
173
|
+
if (rawHeaders) {
|
|
174
|
+
for (const [k, v] of Object.entries(rawHeaders)) {
|
|
175
|
+
headers[k] = stringInterpolator.parse(String(v), {
|
|
176
|
+
args,
|
|
177
|
+
context,
|
|
178
|
+
env: process.env,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const ctrl = timeout ? AbortSignal.timeout(timeout) : undefined;
|
|
183
|
+
const fetchFn = context?.fetch || transportFetch || defaultFetch;
|
|
184
|
+
const responseBytes = await httpProtobufUnary({
|
|
185
|
+
url,
|
|
186
|
+
body: requestBytes,
|
|
187
|
+
headers,
|
|
188
|
+
signal: ctrl,
|
|
189
|
+
fetchFn,
|
|
190
|
+
context,
|
|
191
|
+
info,
|
|
192
|
+
});
|
|
193
|
+
return decodeMessage(selectionFields, responseBytes);
|
|
194
|
+
}
|
|
195
|
+
// gRPC unary (streaming: use generic client stream — unary first for Subscription via repeated unary not supported; use client streaming API)
|
|
196
|
+
const metadata = buildGrpcMetadata(metaData, {
|
|
197
|
+
root: thisRoot ?? {},
|
|
198
|
+
args,
|
|
199
|
+
context,
|
|
200
|
+
env: process.env,
|
|
201
|
+
}) ?? new Metadata();
|
|
202
|
+
if (isStream || typeName === 'Subscription') {
|
|
203
|
+
const gen = (async function* () {
|
|
204
|
+
for await (const chunk of grpcServerStream({
|
|
205
|
+
location,
|
|
206
|
+
path: rpcPath,
|
|
207
|
+
request: requestBytes,
|
|
208
|
+
creds,
|
|
209
|
+
metadata,
|
|
210
|
+
channelOptions,
|
|
211
|
+
})) {
|
|
212
|
+
yield decodeMessage(selectionFields, chunk);
|
|
213
|
+
}
|
|
214
|
+
})();
|
|
215
|
+
if (typeName === 'Subscription') {
|
|
216
|
+
return gen;
|
|
217
|
+
}
|
|
218
|
+
// Query/Mutation with server-stream: collect messages into a list
|
|
219
|
+
const items = [];
|
|
220
|
+
for await (const item of gen) {
|
|
221
|
+
items.push(item);
|
|
222
|
+
}
|
|
223
|
+
return items;
|
|
224
|
+
}
|
|
225
|
+
const responseBytes = await grpcUnary({
|
|
226
|
+
location,
|
|
227
|
+
path: rpcPath,
|
|
228
|
+
request: requestBytes,
|
|
229
|
+
creds,
|
|
230
|
+
metadata,
|
|
231
|
+
channelOptions,
|
|
232
|
+
timeout,
|
|
233
|
+
});
|
|
234
|
+
return decodeMessage(selectionFields, responseBytes);
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
throw toGrpcGraphQLError(err);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function ensureFederationEntitiesField(schema) {
|
|
243
|
+
const queryType = schema.getQueryType();
|
|
244
|
+
if (!queryType) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const queryFields = queryType.getFields();
|
|
248
|
+
const entityTypes = Object.values(schema.getTypeMap()).filter((t) => isObjectType(t) && !t.name.startsWith('__') && !!getTypeResolveReference(t));
|
|
249
|
+
if (!entityTypes.length) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const typeMap = schema.getTypeMap();
|
|
253
|
+
let anyType = typeMap._Any;
|
|
254
|
+
if (!anyType) {
|
|
255
|
+
anyType = new GraphQLScalarType({
|
|
256
|
+
name: '_Any',
|
|
257
|
+
serialize: value => value,
|
|
258
|
+
parseValue: value => value,
|
|
259
|
+
});
|
|
260
|
+
typeMap._Any = anyType;
|
|
261
|
+
}
|
|
262
|
+
let entityUnion = typeMap._Entity;
|
|
263
|
+
if (!entityUnion) {
|
|
264
|
+
entityUnion = new GraphQLUnionType({
|
|
265
|
+
name: '_Entity',
|
|
266
|
+
types: entityTypes,
|
|
267
|
+
resolveType(obj) {
|
|
268
|
+
return obj?.__typename || undefined;
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
typeMap._Entity = entityUnion;
|
|
272
|
+
}
|
|
273
|
+
const entitiesResolve = (_root, args, context, info) => {
|
|
274
|
+
const representations = args?.representations;
|
|
275
|
+
if (!Array.isArray(representations)) {
|
|
276
|
+
return [];
|
|
277
|
+
}
|
|
278
|
+
return entitiesResolver({ representations, context, info });
|
|
279
|
+
};
|
|
280
|
+
if (queryFields._entities) {
|
|
281
|
+
queryFields._entities.resolve = entitiesResolve;
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
queryFields._entities = {
|
|
285
|
+
name: '_entities',
|
|
286
|
+
description: 'Federation entity resolver for protobuf RPC transport (Hive `_entities` path)',
|
|
287
|
+
type: new GraphQLNonNull(new GraphQLList(entityUnion)),
|
|
288
|
+
args: [
|
|
289
|
+
{
|
|
290
|
+
name: 'representations',
|
|
291
|
+
description: undefined,
|
|
292
|
+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(anyType))),
|
|
293
|
+
defaultValue: undefined,
|
|
294
|
+
deprecationReason: undefined,
|
|
295
|
+
extensions: {},
|
|
296
|
+
astNode: undefined,
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
resolve: entitiesResolve,
|
|
300
|
+
subscribe: undefined,
|
|
301
|
+
deprecationReason: undefined,
|
|
302
|
+
extensions: {},
|
|
303
|
+
astNode: undefined,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function grpcUnary(opts) {
|
|
307
|
+
const Client = makeGenericClientConstructor({
|
|
308
|
+
Invoke: {
|
|
309
|
+
path: opts.path,
|
|
310
|
+
requestStream: false,
|
|
311
|
+
responseStream: false,
|
|
312
|
+
requestSerialize: (v) => Buffer.from(v),
|
|
313
|
+
requestDeserialize: (v) => new Uint8Array(v),
|
|
314
|
+
responseSerialize: (v) => Buffer.from(v),
|
|
315
|
+
responseDeserialize: (v) => new Uint8Array(v),
|
|
316
|
+
},
|
|
317
|
+
}, 'MeshProtobuf');
|
|
318
|
+
const client = new Client(opts.location, opts.creds, opts.channelOptions);
|
|
319
|
+
return new Promise((resolve, reject) => {
|
|
320
|
+
const deadline = opts.timeout != null && opts.timeout > 0 ? new Date(Date.now() + opts.timeout) : undefined;
|
|
321
|
+
client.Invoke(opts.request, opts.metadata, deadline ? { deadline } : {}, (err, value) => {
|
|
322
|
+
try {
|
|
323
|
+
client.close?.();
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
// ignore
|
|
327
|
+
}
|
|
328
|
+
if (err)
|
|
329
|
+
reject(err);
|
|
330
|
+
else
|
|
331
|
+
resolve(value ?? new Uint8Array());
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
async function* grpcServerStream(opts) {
|
|
336
|
+
const Client = makeGenericClientConstructor({
|
|
337
|
+
Invoke: {
|
|
338
|
+
path: opts.path,
|
|
339
|
+
requestStream: false,
|
|
340
|
+
responseStream: true,
|
|
341
|
+
requestSerialize: (v) => Buffer.from(v),
|
|
342
|
+
requestDeserialize: (v) => new Uint8Array(v),
|
|
343
|
+
responseSerialize: (v) => Buffer.from(v),
|
|
344
|
+
responseDeserialize: (v) => new Uint8Array(v),
|
|
345
|
+
},
|
|
346
|
+
}, 'MeshProtobuf');
|
|
347
|
+
const client = new Client(opts.location, opts.creds, opts.channelOptions);
|
|
348
|
+
const call = client.Invoke(opts.request, opts.metadata);
|
|
349
|
+
try {
|
|
350
|
+
for await (const chunk of call) {
|
|
351
|
+
yield chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
finally {
|
|
355
|
+
try {
|
|
356
|
+
client.close?.();
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// ignore
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function mergeTransportHeaders(entryHeaders, optionHeaders) {
|
|
364
|
+
const fromEntry = entryHeaders?.length ? Object.fromEntries(entryHeaders) : undefined;
|
|
365
|
+
const fromOptions = Array.isArray(optionHeaders)
|
|
366
|
+
? Object.fromEntries(optionHeaders)
|
|
367
|
+
: optionHeaders;
|
|
368
|
+
if (!fromEntry && !fromOptions)
|
|
369
|
+
return undefined;
|
|
370
|
+
return { ...fromEntry, ...fromOptions };
|
|
371
|
+
}
|
|
372
|
+
const transport = {
|
|
373
|
+
getSubgraphExecutor({ transportEntry, subgraph, cwd, log, fetch }) {
|
|
374
|
+
const options = transportEntry.options || {};
|
|
375
|
+
const helper = new ProtobufTransportHelper(transportEntry.subgraph, cwd, log, transportEntry.location, {
|
|
376
|
+
...options,
|
|
377
|
+
headers: mergeTransportHeaders(transportEntry.headers, options.headers),
|
|
378
|
+
});
|
|
379
|
+
helper.setFetch(fetch);
|
|
380
|
+
return handleMaybePromise(() => isHttpLocation(transportEntry.location)
|
|
381
|
+
? credentials.createInsecure()
|
|
382
|
+
: helper.getCredentials(), creds => {
|
|
383
|
+
helper.processDirectives({ schema: subgraph, creds });
|
|
384
|
+
const executor = createDefaultExecutor(subgraph);
|
|
385
|
+
executor[DisposableSymbols.dispose] = () => helper.dispose();
|
|
386
|
+
return executor;
|
|
387
|
+
});
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
export default transport;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Long from 'long';
|
|
2
|
+
function patchLongJs() {
|
|
3
|
+
const originalLongFromValue = Long.fromValue.bind(Long);
|
|
4
|
+
Long.fromValue = (value) => {
|
|
5
|
+
if (typeof value === 'bigint') {
|
|
6
|
+
return Long.fromValue(value.toString());
|
|
7
|
+
}
|
|
8
|
+
return originalLongFromValue(value);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
patchLongJs();
|