@graphql-hive/core
Version:
35 lines (34 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathToCoordinate = pathToCoordinate;
const graphql_1 = require("graphql");
function resolveRootType(schema, operationType) {
if (operationType === 'mutation') {
return schema.getMutationType();
}
if (operationType === 'subscription') {
return schema.getSubscriptionType();
}
return schema.getQueryType();
}
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 = (0, graphql_1.getNamedType)(currentType);
if ((0, graphql_1.isObjectType)(currentType) || (0, graphql_1.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;
}