import { isThenable, type PothosOutputFieldConfig, type PothosOutputFieldType, type SchemaTypes, } from '@pothos/core'; import { defaultFieldResolver, type GraphQLFieldResolver, type GraphQLResolveInfo } from 'graphql'; export function isRootField(config: PothosOutputFieldConfig) { return ( config.parentType === 'Query' || config.parentType === 'Mutation' || config.parentType === 'Subscription' ); } export function isScalarField(config: PothosOutputFieldConfig) { return resolveFieldType(config.type) === 'Scalar'; } export function isEnumField(config: PothosOutputFieldConfig) { return resolveFieldType(config.type) === 'Enum'; } export function isExposedField(config: PothosOutputFieldConfig) { return ( !!config.extensions?.pothosExposedField || !config.resolve || config.resolve === defaultFieldResolver ); } export function resolveFieldType( type: PothosOutputFieldType, ): 'Enum' | 'Interface' | 'Object' | 'Scalar' | 'Union' { if (type.kind === 'List') { return resolveFieldType(type.type); } return type.kind; } const spanCacheSymbol = Symbol.for('Pothos.tracing.spanCache'); interface InternalContext { [spanCacheSymbol]?: Map; } export function pathToString(info: GraphQLResolveInfo) { let current = info.path; let path = String(current.key); while (current.prev) { current = current.prev; path = `${current.key}.${path}`; } return path; } function getParentPaths(path: GraphQLResolveInfo['path']): [string, ...string[]] { if (!path.prev) { return [String(path.key)]; } const parentPaths = getParentPaths(path.prev); return [`${parentPaths[0]}.${path.key}`, ...parentPaths]; } export function getParentSpan(context: InternalContext, info: GraphQLResolveInfo) { if (!info.path.prev) { return null; } const paths = getParentPaths(info.path.prev); const spanCache = context[spanCacheSymbol]; if (!spanCache) { return null; } for (const path of paths) { const span = spanCache.get(path); if (span) { return span; } } return null; } export function createSpanWithParent( context: object, info: GraphQLResolveInfo, createSpan: (path: string, parent: T | null) => T, ) { const parentSpan = getParentSpan(context, info); const stringPath = pathToString(info); const span = createSpan(stringPath, parentSpan); if (!(context as InternalContext)[spanCacheSymbol]) { (context as InternalContext)[spanCacheSymbol] = new Map(); } (context as InternalContext)[spanCacheSymbol]!.set(stringPath, span); return span; } const { performance } = globalThis as unknown as { performance: { now: () => number } }; export function wrapResolver( resolver: GraphQLFieldResolver, end: (error: unknown, duration: number) => void, ): GraphQLFieldResolver { return (source, args, ctx, info) => { const start = performance.now(); let result: unknown; try { result = resolver(source, args, ctx, info); } catch (error: unknown) { end(error, performance.now() - start); throw error; } if (isThenable(result)) { return result.then( (value) => { end(null, performance.now() - start); return value; }, (error: Error) => { end(error, performance.now() - start); throw error; }, ); } end(null, performance.now() - start); return result; }; } export function runFunction(next: () => T, end: (error: unknown, duration: number) => void) { const start = performance.now(); let result: unknown; try { result = next(); } catch (error: unknown) { end(error, performance.now() - start); throw error; } if (isThenable(result)) { return result.then( (value) => { end(null, performance.now() - start); return value; }, (error: Error) => { end(error, performance.now() - start); throw error; }, ); } end(null, performance.now() - start); return result; }