UNPKG

@graphql-hive/envelop

Version:
56 lines (55 loc) 2.29 kB
import { autoDisposeSymbol, createHive as createHiveClient, isAsyncIterable, isHiveClient, } from '@graphql-hive/core'; export { atLeastOnceSampler, createSchemaFetcher, createServicesFetcher } from '@graphql-hive/core'; export function createHive(clientOrOptions) { return createHiveClient(Object.assign(Object.assign({}, clientOrOptions), { agent: Object.assign({ name: 'hive-client-envelop' }, clientOrOptions.agent) })); } export function useHive(clientOrOptions) { const hive = isHiveClient(clientOrOptions) ? clientOrOptions : createHive(Object.assign(Object.assign({}, clientOrOptions), { agent: Object.assign({ name: 'hive-client-envelop' }, clientOrOptions.agent) })); void hive.info(); if (hive[autoDisposeSymbol]) { if (global.process) { const signals = Array.isArray(hive[autoDisposeSymbol]) ? hive[autoDisposeSymbol] : ['SIGINT', 'SIGTERM']; for (const signal of signals) { process.once(signal, () => hive.dispose()); } } else { console.error('It seems that GraphQL Hive is not being executed in Node.js. ' + 'Please attempt manual client disposal and use autoDispose: false option.'); } } return { onSchemaChange({ schema }) { hive.reportSchema({ schema }); }, onExecute({ args }) { const complete = hive.collectUsage(); return { onExecuteDone({ result }) { if (!isAsyncIterable(result)) { void complete(args, result); return; } const errors = []; return { onNext(ctx) { if (ctx.result.errors) { errors.push(...ctx.result.errors); } }, onEnd() { void complete(args, errors.length ? { errors } : {}); }, }; }, }; }, onSubscribe({ args }) { hive.collectSubscriptionUsage({ args }); }, }; }