@graphql-mesh/transform-hive
Version:
92 lines (91 loc) • 3.72 kB
JavaScript
import { createHive } from '@graphql-hive/client';
import { process } from '@graphql-mesh/cross-helpers';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
export default class HiveTransform {
constructor({ config, pubsub, logger }) {
const enabled =
// eslint-disable-next-line no-new-func
config != null && 'enabled' in config ? new Function(`return ${config.enabled}`)() : true;
const token = stringInterpolator.parse(config.token, {
env: process.env,
});
let usage;
if (config.usage) {
usage = {
max: config.usage.max,
ttl: config.usage.ttl,
exclude: config.usage.exclude,
sampleRate: config.usage.sampleRate,
processVariables: config.usage.processVariables,
};
if (config.usage?.clientInfo) {
usage.clientInfo = function (context) {
return {
name: stringInterpolator.parse(config.usage.clientInfo.name, {
context,
env: process.env,
}),
version: stringInterpolator.parse(config.usage.clientInfo.version, {
context,
env: process.env,
}),
};
};
}
}
let reporting;
if (config.reporting) {
reporting = {
author: stringInterpolator.parse(config.reporting.author, { env: process.env }),
commit: stringInterpolator.parse(config.reporting.commit, { env: process.env }),
serviceName: stringInterpolator.parse(config.reporting.serviceName, { env: process.env }),
serviceUrl: stringInterpolator.parse(config.reporting.serviceUrl, { env: process.env }),
};
}
let agent;
if (config.agent) {
agent = {
timeout: config.agent.timeout,
maxRetries: config.agent.maxRetries,
minTimeout: config.agent.minTimeout,
sendInterval: config.agent.sendInterval,
maxSize: config.agent.maxSize,
logger,
};
}
this.hiveClient = createHive({
enabled,
debug: !!process.env.DEBUG,
token,
agent,
usage,
reporting,
});
const id = pubsub.subscribe('destroy', () => {
this.hiveClient
.dispose()
.catch(e => logger.error(`Hive client failed to dispose`, e))
.finally(() => pubsub.unsubscribe(id));
});
}
transformSchema(schema) {
this.hiveClient.reportSchema({ schema });
return schema;
}
transformRequest(request, delegationContext, transformationContext) {
transformationContext.collectUsageCallback = this.hiveClient.collectUsage();
transformationContext.request = request;
return request;
}
transformResult(result, delegationContext, transformationContext) {
transformationContext.collectUsageCallback({
schema: delegationContext.transformedSchema,
document: transformationContext.request.document,
rootValue: transformationContext.request.rootValue,
contextValue: transformationContext.request.context,
variableValues: transformationContext.request.variables,
operationName: transformationContext.request.operationName,
}, result);
return result;
}
}