UNPKG

@graphql-mesh/plugin-hive

Version:
128 lines (127 loc) 5.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useMeshHive; const yoga_1 = require("@graphql-hive/yoga"); const cross_helpers_1 = require("@graphql-mesh/cross-helpers"); const string_interpolation_1 = require("@graphql-mesh/string-interpolation"); const utils_1 = require("@graphql-mesh/utils"); function useMeshHive(pluginOptions) { const enabled = pluginOptions != null && 'enabled' in pluginOptions ? typeof pluginOptions.enabled === 'string' ? // eslint-disable-next-line no-new-func new Function(`return ${pluginOptions.enabled}`)() : pluginOptions.enabled : true; if (!enabled) { pluginOptions.logger?.warn('Plugin is disabled'); return {}; } const token = string_interpolation_1.stringInterpolator.parse(pluginOptions.token, { env: cross_helpers_1.process.env, }); if (token) { pluginOptions.logger?.info('Reporting enabled'); } const persistedDocuments = pluginOptions.experimental__persistedDocuments; if (persistedDocuments) { pluginOptions.logger?.info('Persisted documents enabled'); } let usage = true; if (pluginOptions.usage) { usage = { max: pluginOptions.usage.max, ttl: pluginOptions.usage.ttl, exclude: pluginOptions.usage.exclude, sampleRate: pluginOptions.usage.sampleRate, processVariables: pluginOptions.usage.processVariables, }; if (pluginOptions.usage?.clientInfo) { if (typeof pluginOptions.usage.clientInfo === 'function') { usage.clientInfo = pluginOptions.usage.clientInfo; } else { usage.clientInfo = function (context) { return { name: string_interpolation_1.stringInterpolator.parse(pluginOptions.usage.clientInfo.name, { context, env: cross_helpers_1.process.env, }), version: string_interpolation_1.stringInterpolator.parse(pluginOptions.usage.clientInfo.version, { context, env: cross_helpers_1.process.env, }), }; }; } } } let reporting; if (pluginOptions.reporting) { reporting = { author: string_interpolation_1.stringInterpolator.parse(pluginOptions.reporting.author, { env: cross_helpers_1.process.env }), commit: string_interpolation_1.stringInterpolator.parse(pluginOptions.reporting.commit, { env: cross_helpers_1.process.env }), serviceName: string_interpolation_1.stringInterpolator.parse(pluginOptions.reporting.serviceName, { env: cross_helpers_1.process.env, }), serviceUrl: string_interpolation_1.stringInterpolator.parse(pluginOptions.reporting.serviceUrl, { env: cross_helpers_1.process.env, }), }; } let agent = { name: 'graphql-mesh', logger: pluginOptions.logger, }; if (pluginOptions.agent) { agent = { name: pluginOptions.agent.name || 'graphql-mesh', timeout: pluginOptions.agent.timeout, maxRetries: pluginOptions.agent.maxRetries, minTimeout: pluginOptions.agent.minTimeout, sendInterval: pluginOptions.agent.sendInterval, maxSize: pluginOptions.agent.maxSize, logger: pluginOptions.agent?.logger || pluginOptions.logger, }; } let selfHosting; if (pluginOptions.selfHosting) { selfHosting = { graphqlEndpoint: string_interpolation_1.stringInterpolator.parse(pluginOptions.selfHosting.graphqlEndpoint, { env: cross_helpers_1.process.env, }), usageEndpoint: string_interpolation_1.stringInterpolator.parse(pluginOptions.selfHosting.usageEndpoint, { env: cross_helpers_1.process.env, }), applicationUrl: string_interpolation_1.stringInterpolator.parse(pluginOptions.selfHosting.applicationUrl, { env: cross_helpers_1.process.env, }), }; } const hiveClient = (0, yoga_1.createHive)({ enabled: // eslint-disable-next-line no-unneeded-ternary -- for brevity persistedDocuments && !token ? false // disable usage reporting if just persisted documents are configured : true, debug: ['1', 't', 'true', 'y', 'yes'].includes(cross_helpers_1.process.env.DEBUG), token, agent, usage, reporting, selfHosting, // Mesh already disposes the client below on Mesh's `destroy` event autoDispose: false, experimental__persistedDocuments: persistedDocuments, }); // TODO: Remove later after v0 // Pubsub.destroy will no longer function onTerminate() { return hiveClient .dispose() .catch(e => pluginOptions.logger?.error(`Hive client failed to dispose`, e)); } const id = pluginOptions.pubsub?.subscribe('destroy', () => onTerminate().finally(() => pluginOptions.pubsub.unsubscribe(id))); return (0, utils_1.makeAsyncDisposable)( // @ts-expect-error - Typings are wrong (0, yoga_1.useHive)(hiveClient), onTerminate); }