@graphql-hive/core
Version:
29 lines (28 loc) • 1.25 kB
JavaScript
import { getNamedType, isInterfaceType, isObjectType, } from 'graphql';
export function pathToCoordinate(schema, errorPath, operationType = 'query') {
let currentType;
if (operationType === 'mutation')
currentType = schema.getMutationType();
else if (operationType === 'subscription')
currentType = schema.getSubscriptionType();
else
currentType = schema.getQueryType();
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;
}