@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
package/src/index.ts
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getNamedType,
|
|
3
|
+
GraphQLError,
|
|
4
|
+
GraphQLList,
|
|
5
|
+
GraphQLNonNull,
|
|
6
|
+
GraphQLScalarType,
|
|
7
|
+
GraphQLUnionType,
|
|
8
|
+
isObjectType,
|
|
9
|
+
type GraphQLField,
|
|
10
|
+
type GraphQLFieldResolver,
|
|
11
|
+
type GraphQLObjectType,
|
|
12
|
+
type GraphQLResolveInfo,
|
|
13
|
+
type GraphQLSchema,
|
|
14
|
+
} from 'graphql';
|
|
15
|
+
import { entitiesResolver } from '@apollo/subgraph';
|
|
16
|
+
import type { Logger } from '@graphql-hive/logger';
|
|
17
|
+
import { fs, path, process } from '@graphql-mesh/cross-helpers';
|
|
18
|
+
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
|
|
19
|
+
import { createDefaultExecutor, type Transport } from '@graphql-mesh/transport-common';
|
|
20
|
+
import type { MeshFetch } from '@graphql-mesh/types';
|
|
21
|
+
import { getDirectives } from '@graphql-tools/utils';
|
|
22
|
+
import {
|
|
23
|
+
credentials,
|
|
24
|
+
makeGenericClientConstructor,
|
|
25
|
+
Metadata,
|
|
26
|
+
type ChannelCredentials,
|
|
27
|
+
type ChannelOptions,
|
|
28
|
+
} from '@grpc/grpc-js';
|
|
29
|
+
import { DisposableStack, DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
30
|
+
import { fetch as defaultFetch } from '@whatwg-node/fetch';
|
|
31
|
+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
|
|
32
|
+
import type { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
33
|
+
import { decodeMessage, encodeMessage } from './protobuf/codec.js';
|
|
34
|
+
import {
|
|
35
|
+
buildRpcPath,
|
|
36
|
+
httpProtobufUnary,
|
|
37
|
+
isHttpLocation,
|
|
38
|
+
resolveHttpUrl,
|
|
39
|
+
} from './protobuf/dial.js';
|
|
40
|
+
import { parseMapping } from './protobuf/mapping.js';
|
|
41
|
+
import { buildGrpcMetadata, toGrpcGraphQLError } from './utils.js';
|
|
42
|
+
|
|
43
|
+
type ResolveReferenceFn = (
|
|
44
|
+
reference: Record<string, any>,
|
|
45
|
+
context: any,
|
|
46
|
+
info: GraphQLResolveInfo,
|
|
47
|
+
) => any;
|
|
48
|
+
|
|
49
|
+
function setTypeResolveReference(
|
|
50
|
+
type: { extensions?: any; name: string },
|
|
51
|
+
resolveReference: ResolveReferenceFn,
|
|
52
|
+
) {
|
|
53
|
+
const extensions: Record<string, any> = (type.extensions ||= {});
|
|
54
|
+
const apollo: Record<string, any> = (extensions.apollo ||= {});
|
|
55
|
+
const subgraph: Record<string, any> = (apollo.subgraph ||= {});
|
|
56
|
+
subgraph.resolveReference = resolveReference;
|
|
57
|
+
(type as any).resolveReference = resolveReference;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getTypeResolveReference(type: { extensions?: any }): ResolveReferenceFn | undefined {
|
|
61
|
+
const fromExtensions = (type.extensions as any)?.apollo?.subgraph?.resolveReference;
|
|
62
|
+
if (typeof fromExtensions === 'function') {
|
|
63
|
+
return fromExtensions;
|
|
64
|
+
}
|
|
65
|
+
const fromType = (type as any).resolveReference;
|
|
66
|
+
return typeof fromType === 'function' ? fromType : undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface ProtobufTransportOptions {
|
|
70
|
+
requestTimeout?: number;
|
|
71
|
+
credentialsSsl?: {
|
|
72
|
+
rootCA?: string;
|
|
73
|
+
certChain?: string;
|
|
74
|
+
privateKey?: string;
|
|
75
|
+
};
|
|
76
|
+
useHTTPS?: boolean;
|
|
77
|
+
metaData?:
|
|
78
|
+
| {
|
|
79
|
+
[k: string]: any;
|
|
80
|
+
}
|
|
81
|
+
| [string, string][];
|
|
82
|
+
channelOptions?: ChannelOptions | [string, string | number][];
|
|
83
|
+
headers?: Record<string, string> | [string, string][];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class ProtobufTransportHelper extends DisposableStack {
|
|
87
|
+
private channelCreds: ChannelCredentials | undefined;
|
|
88
|
+
private fetchFn: MeshFetch = defaultFetch;
|
|
89
|
+
|
|
90
|
+
constructor(
|
|
91
|
+
private subgraphName: string,
|
|
92
|
+
private baseDir: string,
|
|
93
|
+
private log: Logger,
|
|
94
|
+
private endpoint: string,
|
|
95
|
+
private config: ProtobufTransportOptions,
|
|
96
|
+
) {
|
|
97
|
+
super();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
setFetch(fetchFn: MeshFetch | undefined) {
|
|
101
|
+
this.fetchFn = fetchFn || defaultFetch;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
getCredentials(): MaybePromise<ChannelCredentials> {
|
|
105
|
+
if (this.config.credentialsSsl) {
|
|
106
|
+
const absolutePrivateKeyPath = path.isAbsolute(this.config.credentialsSsl.privateKey)
|
|
107
|
+
? this.config.credentialsSsl.privateKey
|
|
108
|
+
: path.join(this.baseDir, this.config.credentialsSsl.privateKey);
|
|
109
|
+
const absoluteCertChainPath = path.isAbsolute(this.config.credentialsSsl.certChain)
|
|
110
|
+
? this.config.credentialsSsl.certChain
|
|
111
|
+
: path.join(this.baseDir, this.config.credentialsSsl.certChain);
|
|
112
|
+
|
|
113
|
+
const sslFiles = [
|
|
114
|
+
fs.promises.readFile(absolutePrivateKeyPath),
|
|
115
|
+
fs.promises.readFile(absoluteCertChainPath),
|
|
116
|
+
];
|
|
117
|
+
if (this.config.credentialsSsl.rootCA && this.config.credentialsSsl.rootCA !== 'rootCA') {
|
|
118
|
+
const absoluteRootCAPath = path.isAbsolute(this.config.credentialsSsl.rootCA)
|
|
119
|
+
? this.config.credentialsSsl.rootCA
|
|
120
|
+
: path.join(this.baseDir, this.config.credentialsSsl.rootCA);
|
|
121
|
+
sslFiles.unshift(fs.promises.readFile(absoluteRootCAPath));
|
|
122
|
+
}
|
|
123
|
+
return Promise.all(sslFiles).then(([rootCA, privateKey, certChain]) =>
|
|
124
|
+
credentials.createSsl(rootCA, privateKey, certChain),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
if (this.config.useHTTPS) {
|
|
128
|
+
return credentials.createSsl();
|
|
129
|
+
}
|
|
130
|
+
return credentials.createInsecure();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
processDirectives({ schema, creds }: { schema: GraphQLSchema; creds: ChannelCredentials }) {
|
|
134
|
+
this.channelCreds = creds;
|
|
135
|
+
const location = stringInterpolator.parse(this.endpoint, { env: process.env }) ?? this.endpoint;
|
|
136
|
+
const channelOptions = Array.isArray(this.config.channelOptions)
|
|
137
|
+
? Object.fromEntries(this.config.channelOptions)
|
|
138
|
+
: this.config.channelOptions;
|
|
139
|
+
|
|
140
|
+
const queryType = schema.getQueryType();
|
|
141
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
142
|
+
if (!isObjectType(type) || type.name.startsWith('__')) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const typeName = type.name;
|
|
146
|
+
// Type-level `@protobuf__rpc` → resolveReference (entity fill / `$this` binds).
|
|
147
|
+
const typeDirectives = getDirectives(schema, type) || [];
|
|
148
|
+
for (const directiveObj of typeDirectives) {
|
|
149
|
+
if (
|
|
150
|
+
this.subgraphName &&
|
|
151
|
+
directiveObj.args?.subgraph &&
|
|
152
|
+
directiveObj.args.subgraph !== this.subgraphName
|
|
153
|
+
) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (directiveObj.name !== 'protobuf__rpc' && directiveObj.name !== 'protobufRpc') {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const resolver = this.createRpcResolverFromDirectiveArgs(
|
|
160
|
+
directiveObj.args,
|
|
161
|
+
location,
|
|
162
|
+
channelOptions,
|
|
163
|
+
typeName,
|
|
164
|
+
);
|
|
165
|
+
setTypeResolveReference(type, (reference, context, info) =>
|
|
166
|
+
resolver(reference, {}, context, info),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
for (const field of Object.values(type.getFields())) {
|
|
171
|
+
const directives = getDirectives(schema, field) || [];
|
|
172
|
+
for (const directiveObj of directives) {
|
|
173
|
+
if (
|
|
174
|
+
this.subgraphName &&
|
|
175
|
+
directiveObj.args?.subgraph &&
|
|
176
|
+
directiveObj.args.subgraph !== this.subgraphName
|
|
177
|
+
) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (directiveObj.name !== 'protobuf__rpc' && directiveObj.name !== 'protobufRpc') {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const isEntity = !!(directiveObj.args as { entity?: boolean })?.entity;
|
|
184
|
+
const resolver = this.createRpcResolverFromDirectiveArgs(
|
|
185
|
+
directiveObj.args,
|
|
186
|
+
location,
|
|
187
|
+
channelOptions,
|
|
188
|
+
typeName,
|
|
189
|
+
);
|
|
190
|
+
// Subscriptions use `subscribe` (async iterable). Query/Mutation server-stream
|
|
191
|
+
// fields keep `resolve` and return an AsyncIterable so `@stream` can
|
|
192
|
+
// incremental-deliver as gRPC chunks arrive (same pattern as legacy gRPC).
|
|
193
|
+
if (typeName === 'Subscription') {
|
|
194
|
+
field.subscribe = resolver;
|
|
195
|
+
field.resolve = (x: any) => x;
|
|
196
|
+
} else {
|
|
197
|
+
field.resolve = resolver;
|
|
198
|
+
}
|
|
199
|
+
// Query `@protobuf__rpc(entity: true)` → type.resolveReference for Hive `_entities`.
|
|
200
|
+
if (isEntity && type === queryType && isObjectType(getNamedType(field.type))) {
|
|
201
|
+
const returnType = getNamedType(field.type) as GraphQLObjectType;
|
|
202
|
+
if (!getTypeResolveReference(returnType)) {
|
|
203
|
+
const entityField = field as GraphQLField<any, any>;
|
|
204
|
+
setTypeResolveReference(returnType, (reference, context, info) => {
|
|
205
|
+
const args: Record<string, any> = {};
|
|
206
|
+
for (const arg of entityField.args) {
|
|
207
|
+
args[arg.name] = reference?.[arg.name];
|
|
208
|
+
}
|
|
209
|
+
return entityField.resolve?.(null, args, context, info);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
ensureFederationEntitiesField(schema);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private createRpcResolverFromDirectiveArgs(
|
|
220
|
+
directiveArgs: any,
|
|
221
|
+
location: string,
|
|
222
|
+
channelOptions: Record<string, string | number> | undefined,
|
|
223
|
+
typeName: string,
|
|
224
|
+
): GraphQLFieldResolver<any, any> {
|
|
225
|
+
const {
|
|
226
|
+
service,
|
|
227
|
+
method,
|
|
228
|
+
args: argsMapping,
|
|
229
|
+
selection,
|
|
230
|
+
stream: isStream,
|
|
231
|
+
} = directiveArgs as {
|
|
232
|
+
service?: string;
|
|
233
|
+
method: string;
|
|
234
|
+
args: string;
|
|
235
|
+
selection: string;
|
|
236
|
+
stream?: boolean;
|
|
237
|
+
};
|
|
238
|
+
return this.createRpcResolver({
|
|
239
|
+
location,
|
|
240
|
+
service,
|
|
241
|
+
method,
|
|
242
|
+
argsFields: parseMapping(argsMapping),
|
|
243
|
+
selectionFields: parseMapping(selection),
|
|
244
|
+
isStream: !!isStream,
|
|
245
|
+
channelOptions,
|
|
246
|
+
typeName,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private createRpcResolver({
|
|
251
|
+
location,
|
|
252
|
+
service,
|
|
253
|
+
method,
|
|
254
|
+
argsFields,
|
|
255
|
+
selectionFields,
|
|
256
|
+
isStream,
|
|
257
|
+
channelOptions,
|
|
258
|
+
typeName,
|
|
259
|
+
}: {
|
|
260
|
+
location: string;
|
|
261
|
+
service?: string;
|
|
262
|
+
method: string;
|
|
263
|
+
argsFields: ReturnType<typeof parseMapping>;
|
|
264
|
+
selectionFields: ReturnType<typeof parseMapping>;
|
|
265
|
+
isStream: boolean;
|
|
266
|
+
channelOptions?: Record<string, string | number>;
|
|
267
|
+
typeName: string;
|
|
268
|
+
}): GraphQLFieldResolver<any, any> {
|
|
269
|
+
const metaData = this.config.metaData;
|
|
270
|
+
const headersConfig = this.config.headers;
|
|
271
|
+
const timeout = this.config.requestTimeout;
|
|
272
|
+
const creds = this.channelCreds ?? credentials.createInsecure();
|
|
273
|
+
const http = isHttpLocation(location);
|
|
274
|
+
const rpcPath = buildRpcPath(service, method);
|
|
275
|
+
const transportFetch = this.fetchFn;
|
|
276
|
+
|
|
277
|
+
return async function protobufRpcResolver(_root, args, context, info) {
|
|
278
|
+
if (isStream && http) {
|
|
279
|
+
throw new GraphQLError(
|
|
280
|
+
'Server streaming protobuf RPCs require a gRPC location (not HTTP Connect/Twirp)',
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
// Type-level entity resolvers pass the representation as `_root` and empty args;
|
|
284
|
+
// `$this.*` binds read from that root.
|
|
285
|
+
const thisRoot =
|
|
286
|
+
_root != null && typeof _root === 'object' ? (_root as Record<string, any>) : undefined;
|
|
287
|
+
const requestBytes = encodeMessage(argsFields, args ?? {}, args ?? {}, thisRoot);
|
|
288
|
+
try {
|
|
289
|
+
if (http) {
|
|
290
|
+
const url = resolveHttpUrl(location, service, method);
|
|
291
|
+
const headers: Record<string, string> = {};
|
|
292
|
+
const rawHeaders = Array.isArray(headersConfig)
|
|
293
|
+
? Object.fromEntries(headersConfig)
|
|
294
|
+
: headersConfig;
|
|
295
|
+
if (rawHeaders) {
|
|
296
|
+
for (const [k, v] of Object.entries(rawHeaders)) {
|
|
297
|
+
headers[k] = stringInterpolator.parse(String(v), {
|
|
298
|
+
args,
|
|
299
|
+
context,
|
|
300
|
+
env: process.env,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const ctrl = timeout ? AbortSignal.timeout(timeout) : undefined;
|
|
305
|
+
const fetchFn: MeshFetch = context?.fetch || transportFetch || defaultFetch;
|
|
306
|
+
const responseBytes = await httpProtobufUnary({
|
|
307
|
+
url,
|
|
308
|
+
body: requestBytes,
|
|
309
|
+
headers,
|
|
310
|
+
signal: ctrl,
|
|
311
|
+
fetchFn,
|
|
312
|
+
context,
|
|
313
|
+
info,
|
|
314
|
+
});
|
|
315
|
+
return decodeMessage(selectionFields, responseBytes);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// gRPC unary (streaming: use generic client stream — unary first for Subscription via repeated unary not supported; use client streaming API)
|
|
319
|
+
const metadata =
|
|
320
|
+
buildGrpcMetadata(metaData, {
|
|
321
|
+
root: thisRoot ?? {},
|
|
322
|
+
args,
|
|
323
|
+
context,
|
|
324
|
+
env: process.env,
|
|
325
|
+
}) ?? new Metadata();
|
|
326
|
+
|
|
327
|
+
if (isStream || typeName === 'Subscription') {
|
|
328
|
+
// Return AsyncIterable for both Subscription and Query `@stream` list fields.
|
|
329
|
+
// Materializing into an array first corrupted repeated fields under `@stream`
|
|
330
|
+
// incremental delivery (e.g. duplicated `cast` across chunks).
|
|
331
|
+
return (async function* () {
|
|
332
|
+
for await (const chunk of grpcServerStream({
|
|
333
|
+
location,
|
|
334
|
+
path: rpcPath,
|
|
335
|
+
request: requestBytes,
|
|
336
|
+
creds,
|
|
337
|
+
metadata,
|
|
338
|
+
channelOptions,
|
|
339
|
+
})) {
|
|
340
|
+
yield decodeMessage(selectionFields, chunk);
|
|
341
|
+
}
|
|
342
|
+
})();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const responseBytes = await grpcUnary({
|
|
346
|
+
location,
|
|
347
|
+
path: rpcPath,
|
|
348
|
+
request: requestBytes,
|
|
349
|
+
creds,
|
|
350
|
+
metadata,
|
|
351
|
+
channelOptions,
|
|
352
|
+
timeout,
|
|
353
|
+
});
|
|
354
|
+
return decodeMessage(selectionFields, responseBytes);
|
|
355
|
+
} catch (err) {
|
|
356
|
+
throw toGrpcGraphQLError(err);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function ensureFederationEntitiesField(schema: GraphQLSchema) {
|
|
363
|
+
const queryType = schema.getQueryType();
|
|
364
|
+
if (!queryType) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
const queryFields = queryType.getFields();
|
|
368
|
+
const entityTypes = Object.values(schema.getTypeMap()).filter(
|
|
369
|
+
(t): t is GraphQLObjectType =>
|
|
370
|
+
isObjectType(t) && !t.name.startsWith('__') && !!getTypeResolveReference(t),
|
|
371
|
+
);
|
|
372
|
+
if (!entityTypes.length) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const typeMap = schema.getTypeMap() as Record<string, any>;
|
|
377
|
+
let anyType = typeMap._Any as GraphQLScalarType | undefined;
|
|
378
|
+
if (!anyType) {
|
|
379
|
+
anyType = new GraphQLScalarType({
|
|
380
|
+
name: '_Any',
|
|
381
|
+
serialize: value => value,
|
|
382
|
+
parseValue: value => value,
|
|
383
|
+
});
|
|
384
|
+
typeMap._Any = anyType;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
let entityUnion = typeMap._Entity as GraphQLUnionType | undefined;
|
|
388
|
+
if (!entityUnion) {
|
|
389
|
+
entityUnion = new GraphQLUnionType({
|
|
390
|
+
name: '_Entity',
|
|
391
|
+
types: entityTypes,
|
|
392
|
+
resolveType(obj) {
|
|
393
|
+
return obj?.__typename || undefined;
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
typeMap._Entity = entityUnion;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const entitiesResolve = (
|
|
400
|
+
_root: unknown,
|
|
401
|
+
args: { representations?: unknown },
|
|
402
|
+
context: any,
|
|
403
|
+
info: GraphQLResolveInfo,
|
|
404
|
+
) => {
|
|
405
|
+
const representations = args?.representations;
|
|
406
|
+
if (!Array.isArray(representations)) {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
return entitiesResolver({ representations, context, info });
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
if (queryFields._entities) {
|
|
413
|
+
queryFields._entities.resolve = entitiesResolve;
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
queryFields._entities = {
|
|
418
|
+
name: '_entities',
|
|
419
|
+
description: 'Federation entity resolver for protobuf RPC transport (Hive `_entities` path)',
|
|
420
|
+
type: new GraphQLNonNull(new GraphQLList(entityUnion)),
|
|
421
|
+
args: [
|
|
422
|
+
{
|
|
423
|
+
name: 'representations',
|
|
424
|
+
description: undefined,
|
|
425
|
+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(anyType))),
|
|
426
|
+
defaultValue: undefined,
|
|
427
|
+
deprecationReason: undefined,
|
|
428
|
+
extensions: {},
|
|
429
|
+
astNode: undefined,
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
resolve: entitiesResolve,
|
|
433
|
+
subscribe: undefined,
|
|
434
|
+
deprecationReason: undefined,
|
|
435
|
+
extensions: {},
|
|
436
|
+
astNode: undefined,
|
|
437
|
+
} as GraphQLField<any, any>;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function grpcUnary(opts: {
|
|
441
|
+
location: string;
|
|
442
|
+
path: string;
|
|
443
|
+
request: Uint8Array;
|
|
444
|
+
creds: ChannelCredentials;
|
|
445
|
+
metadata: Metadata;
|
|
446
|
+
channelOptions?: Record<string, string | number>;
|
|
447
|
+
timeout?: number;
|
|
448
|
+
}): Promise<Uint8Array> {
|
|
449
|
+
const Client = makeGenericClientConstructor(
|
|
450
|
+
{
|
|
451
|
+
Invoke: {
|
|
452
|
+
path: opts.path,
|
|
453
|
+
requestStream: false,
|
|
454
|
+
responseStream: false,
|
|
455
|
+
requestSerialize: (v: Uint8Array) => Buffer.from(v),
|
|
456
|
+
requestDeserialize: (v: Buffer) => new Uint8Array(v),
|
|
457
|
+
responseSerialize: (v: Uint8Array) => Buffer.from(v),
|
|
458
|
+
responseDeserialize: (v: Buffer) => new Uint8Array(v),
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
'MeshProtobuf',
|
|
462
|
+
);
|
|
463
|
+
const client = new Client(opts.location, opts.creds, opts.channelOptions);
|
|
464
|
+
return new Promise((resolve, reject) => {
|
|
465
|
+
const deadline =
|
|
466
|
+
opts.timeout != null && opts.timeout > 0 ? new Date(Date.now() + opts.timeout) : undefined;
|
|
467
|
+
(client as any).Invoke(
|
|
468
|
+
opts.request,
|
|
469
|
+
opts.metadata,
|
|
470
|
+
deadline ? { deadline } : {},
|
|
471
|
+
(err: Error | null, value?: Uint8Array) => {
|
|
472
|
+
try {
|
|
473
|
+
(client as any).close?.();
|
|
474
|
+
} catch {
|
|
475
|
+
// ignore
|
|
476
|
+
}
|
|
477
|
+
if (err) reject(err);
|
|
478
|
+
else resolve(value ?? new Uint8Array());
|
|
479
|
+
},
|
|
480
|
+
);
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async function* grpcServerStream(opts: {
|
|
485
|
+
location: string;
|
|
486
|
+
path: string;
|
|
487
|
+
request: Uint8Array;
|
|
488
|
+
creds: ChannelCredentials;
|
|
489
|
+
metadata: Metadata;
|
|
490
|
+
channelOptions?: Record<string, string | number>;
|
|
491
|
+
}): AsyncGenerator<Uint8Array> {
|
|
492
|
+
const Client = makeGenericClientConstructor(
|
|
493
|
+
{
|
|
494
|
+
Invoke: {
|
|
495
|
+
path: opts.path,
|
|
496
|
+
requestStream: false,
|
|
497
|
+
responseStream: true,
|
|
498
|
+
requestSerialize: (v: Uint8Array) => Buffer.from(v),
|
|
499
|
+
requestDeserialize: (v: Buffer) => new Uint8Array(v),
|
|
500
|
+
responseSerialize: (v: Uint8Array) => Buffer.from(v),
|
|
501
|
+
responseDeserialize: (v: Buffer) => new Uint8Array(v),
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
'MeshProtobuf',
|
|
505
|
+
);
|
|
506
|
+
const client = new Client(opts.location, opts.creds, opts.channelOptions);
|
|
507
|
+
const call = (client as any).Invoke(opts.request, opts.metadata);
|
|
508
|
+
try {
|
|
509
|
+
for await (const chunk of call) {
|
|
510
|
+
yield chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
511
|
+
}
|
|
512
|
+
} finally {
|
|
513
|
+
try {
|
|
514
|
+
(client as any).close?.();
|
|
515
|
+
} catch {
|
|
516
|
+
// ignore
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function mergeTransportHeaders(
|
|
522
|
+
entryHeaders: [string, string][] | undefined,
|
|
523
|
+
optionHeaders: ProtobufTransportOptions['headers'],
|
|
524
|
+
): Record<string, string> | [string, string][] | undefined {
|
|
525
|
+
const fromEntry = entryHeaders?.length ? Object.fromEntries(entryHeaders) : undefined;
|
|
526
|
+
const fromOptions = Array.isArray(optionHeaders)
|
|
527
|
+
? Object.fromEntries(optionHeaders)
|
|
528
|
+
: optionHeaders;
|
|
529
|
+
if (!fromEntry && !fromOptions) return undefined;
|
|
530
|
+
return { ...fromEntry, ...fromOptions };
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const transport: Transport<ProtobufTransportOptions> = {
|
|
534
|
+
getSubgraphExecutor({ transportEntry, subgraph, cwd, log, fetch }) {
|
|
535
|
+
const options = transportEntry.options || {};
|
|
536
|
+
const helper = new ProtobufTransportHelper(
|
|
537
|
+
transportEntry.subgraph,
|
|
538
|
+
cwd,
|
|
539
|
+
log,
|
|
540
|
+
transportEntry.location,
|
|
541
|
+
{
|
|
542
|
+
...options,
|
|
543
|
+
headers: mergeTransportHeaders(transportEntry.headers, options.headers),
|
|
544
|
+
},
|
|
545
|
+
);
|
|
546
|
+
helper.setFetch(fetch);
|
|
547
|
+
return handleMaybePromise(
|
|
548
|
+
() =>
|
|
549
|
+
isHttpLocation(transportEntry.location)
|
|
550
|
+
? credentials.createInsecure()
|
|
551
|
+
: helper.getCredentials(),
|
|
552
|
+
creds => {
|
|
553
|
+
helper.processDirectives({ schema: subgraph, creds });
|
|
554
|
+
const executor = createDefaultExecutor(subgraph);
|
|
555
|
+
executor[DisposableSymbols.dispose] = () => helper.dispose();
|
|
556
|
+
return executor;
|
|
557
|
+
},
|
|
558
|
+
);
|
|
559
|
+
},
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
export default transport;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Long from 'long';
|
|
2
|
+
|
|
3
|
+
function patchLongJs() {
|
|
4
|
+
const originalLongFromValue = Long.fromValue.bind(Long);
|
|
5
|
+
Long.fromValue = (value: any) => {
|
|
6
|
+
if (typeof value === 'bigint') {
|
|
7
|
+
return Long.fromValue(value.toString());
|
|
8
|
+
}
|
|
9
|
+
return originalLongFromValue(value);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
patchLongJs();
|