@graphql-mesh/transport-grpc
Version:
74 lines (73 loc) • 2.58 kB
JavaScript
import lodashGet from 'lodash.get';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
import { withCancel } from '@graphql-mesh/utils';
import { Metadata } from '@grpc/grpc-js';
function isBlob(input) {
return input != null && input.stream instanceof Function;
}
export function addMetaDataToCall(callFn, input, resolverData, metaData, isResponseStream = false) {
const callFnArguments = [];
if (!isBlob(input)) {
callFnArguments.push(input);
}
if (metaData) {
const meta = new Metadata();
for (const [key, value] of Object.entries(metaData)) {
let metaValue = value;
if (Array.isArray(value)) {
// Extract data from context
metaValue = lodashGet(resolverData.context, value);
}
// Ensure that the metadata is compatible with what node-grpc expects
if (typeof metaValue !== 'string' && !(metaValue instanceof Buffer)) {
metaValue = JSON.stringify(metaValue);
}
if (typeof metaValue === 'string') {
metaValue = stringInterpolator.parse(metaValue, resolverData);
}
meta.add(key, metaValue);
}
callFnArguments.push(meta);
}
return new Promise((resolve, reject) => {
const call = callFn(...callFnArguments, (error, response) => {
if (error) {
reject(error);
}
resolve(response);
});
if (isResponseStream) {
let isCancelled = false;
const responseStreamWithCancel = withCancel(call, () => {
if (!isCancelled) {
call.call?.cancelWithStatus(0, 'Cancelled by GraphQL Mesh');
isCancelled = true;
}
});
resolve(responseStreamWithCancel);
if (isBlob(input)) {
const blobStream = input.stream();
blobStream.pipe(call);
}
}
});
}
export function addExecutionLogicToScalar(nonExecutableScalar, actualScalar) {
Object.defineProperties(nonExecutableScalar, {
serialize: {
value: actualScalar.serialize,
},
parseValue: {
value: actualScalar.parseValue,
},
parseLiteral: {
value: actualScalar.parseLiteral,
},
extensions: {
value: {
...actualScalar.extensions,
...nonExecutableScalar.extensions,
},
},
});
}