graphlit-client
Version:
Graphlit API Client for TypeScript
59 lines (58 loc) • 2.01 kB
JavaScript
/**
* Partial GraphQL error surfacing.
*
* When a GraphQL response contains both `data` and `errors`, the SDK returns
* the partial data so callers don't break. This module lets callers opt-in to
* inspecting the errors that came alongside that partial data.
*
* Errors are attached as a non-enumerable, Symbol-keyed property so they are
* invisible to JSON.stringify, Object.keys, for…in, and spread — no breaking
* changes for any existing consumer.
*/
// Symbol.for() is globally shared, so even if two copies of the SDK are loaded
// (e.g. CJS + ESM, or different versions) they will share the same key.
export const PARTIAL_ERRORS_KEY = Symbol.for("graphlit.partialErrors");
/**
* Retrieve partial errors attached to an SDK result, if any.
*
* @returns The array of errors, or `undefined` if the result had no errors.
*
* @example
* ```ts
* const result = await client.queryGitHubRepositories(input);
* const errors = getPartialErrors(result);
* if (errors) {
* console.warn("Partial errors:", errors);
* }
* ```
*/
export function getPartialErrors(result) {
if (result != null &&
typeof result === "object" &&
PARTIAL_ERRORS_KEY in result) {
return result[PARTIAL_ERRORS_KEY];
}
return undefined;
}
/**
* Attach partial errors to an SDK result object. The property is
* non-enumerable so it won't appear in serialisation or iteration.
*
* @internal — only called from client.ts
*/
export function attachPartialErrors(data, errors) {
if (data != null && typeof data === "object" && errors.length > 0) {
const simplified = errors.map((e) => ({
message: e.message,
...(e.path ? { path: e.path } : {}),
...(e.extensions ? { extensions: e.extensions } : {}),
}));
Object.defineProperty(data, PARTIAL_ERRORS_KEY, {
value: simplified,
enumerable: false,
writable: false,
configurable: true,
});
}
return data;
}