@graphql-hive/core
Version:
32 lines (31 loc) • 1.31 kB
JavaScript
import { getNamedType, isInterfaceType, isObjectType, } from 'graphql';
function resolveRootType(schema, operationType) {
if (operationType === 'mutation') {
return schema.getMutationType();
}
if (operationType === 'subscription') {
return schema.getSubscriptionType();
}
return schema.getQueryType();
}
export function pathToCoordinate(schema, errorPath, operationType = 'query') {
let currentType = resolveRootType(schema, operationType);
let coordinate = null;
for (const segment of errorPath) {
// Skip array indices entirely (they don't change the underlying type)
if (typeof segment === 'number')
continue;
currentType = getNamedType(currentType);
if (isObjectType(currentType) || isInterfaceType(currentType)) {
const fields = currentType.getFields();
const field = fields[segment];
if (!field) {
console.warn(`Hive Usage Client: Field '${segment}' not found on type '${currentType.name}'. Was this aliased? Error ignored.`);
return;
}
coordinate = `${currentType.name}.${field.name}`;
currentType = field.type;
}
}
return coordinate !== null && coordinate !== void 0 ? coordinate : undefined;
}