UNPKG

@graphql-mesh/transform-hive

Version:
116 lines (115 loc) 4.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const graphql_1 = require("graphql"); const core_1 = require("@graphql-hive/core"); const cross_helpers_1 = require("@graphql-mesh/cross-helpers"); const string_interpolation_1 = require("@graphql-mesh/string-interpolation"); const delegate_1 = require("@graphql-tools/delegate"); class HiveTransform { constructor({ config, logger }) { this.logger = logger; const enabled = // eslint-disable-next-line no-new-func config != null && 'enabled' in config ? new Function(`return ${config.enabled}`)() : true; const token = string_interpolation_1.stringInterpolator.parse(config.token, { env: cross_helpers_1.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: string_interpolation_1.stringInterpolator.parse(config.usage.clientInfo.name, { context, env: cross_helpers_1.process.env, }), version: string_interpolation_1.stringInterpolator.parse(config.usage.clientInfo.version, { context, env: cross_helpers_1.process.env, }), }; }; } } let reporting; if (config.reporting) { reporting = { author: string_interpolation_1.stringInterpolator.parse(config.reporting.author, { env: cross_helpers_1.process.env }), commit: string_interpolation_1.stringInterpolator.parse(config.reporting.commit, { env: cross_helpers_1.process.env }), serviceName: string_interpolation_1.stringInterpolator.parse(config.reporting.serviceName, { env: cross_helpers_1.process.env }), serviceUrl: string_interpolation_1.stringInterpolator.parse(config.reporting.serviceUrl, { env: cross_helpers_1.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 = (0, core_1.createHive)({ enabled, debug: !!cross_helpers_1.process.env.DEBUG, token, agent, usage, reporting, selfHosting: config.selfHosting, }); } transformSchema(schema) { this.hiveClient.reportSchema({ schema }); this.schema = schema; return schema; } transformRequest(request, _delegationContext, transformationContext) { try { transformationContext.collectUsageCallback = this.hiveClient.collectUsage(); transformationContext.request = request; } catch (e) { this.logger.error(`Failed to collect usage`, e); } return request; } transformResult(result, _delegationContext, transformationContext) { // eslint-disable-next-line @typescript-eslint/no-floating-promises -- we dont really care about usage reporting result try { const typeInfo = (0, delegate_1.getTypeInfo)(this.schema); transformationContext .collectUsageCallback?.({ schema: this.schema, document: (0, graphql_1.visit)(transformationContext.request.document, (0, graphql_1.visitWithTypeInfo)(typeInfo, { Argument: () => { if (!typeInfo.getArgument()) { return null; } }, })), rootValue: transformationContext.request.rootValue, contextValue: transformationContext.request.context, variableValues: transformationContext.request.variables, operationName: transformationContext.request.operationName, }, result) ?.catch(e => { this.logger.error(`Failed to report usage`, e); }); } catch (e) { this.logger.error(`Failed to report usage`, e); } return result; } } exports.default = HiveTransform;