grafast
Version:
Cutting edge GraphQL planning and execution engine
66 lines • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.specFromNodeId = specFromNodeId;
exports.nodeIdFromNode = nodeIdFromNode;
exports.makeDecodeNodeIdRuntime = makeDecodeNodeIdRuntime;
exports.makeDecodeNodeId = makeDecodeNodeId;
const utils_ts_1 = require("../utils.js");
const constant_ts_1 = require("./constant.js");
const lambda_ts_1 = require("./lambda.js");
/**
* Decodes a global identifier that is expected to correspond to a specific
* {@link NodeIdHandler}. The returned specifier can be passed directly to
* whatever data-source helper understands that handler's spec (for example a
* `get`/`update` helper on your own resource abstraction) without invoking the
* polymorphic machinery.
*
* Prefer `specFromNodeId()` whenever the expected object type is already
* known (for example in `updateUser(id: ID!, ...)` mutations). It avoids the
* extra work performed by {@link NodeStep} and keeps plan resolvers
* straightforward.
*/
function specFromNodeId(handler, $id) {
const $decoded = (0, lambda_ts_1.lambda)({ handler: (0, constant_ts_1.constant)(handler), raw: $id }, decodeNodeIdWithHandler);
return handler.getSpec($decoded);
}
function decodeNodeIdWithHandler(details) {
const { handler, raw } = details;
if (raw == null)
return raw;
try {
const decoded = handler.codec.decode(raw);
if (handler.match(decoded)) {
return decoded;
}
return null;
}
catch (e) {
return null;
}
}
(0, utils_ts_1.markSyncAndSafe)(decodeNodeIdWithHandler);
function nodeIdFromNode(handler, $node) {
const specifier = handler.plan($node);
return (0, lambda_ts_1.lambda)(specifier, handler.codec.encode);
}
function makeDecodeNodeIdRuntime(handlers) {
const codecs = [...new Set(handlers.map((h) => h.codec))];
return (0, utils_ts_1.markSyncAndSafe)(function decodeNodeIdWithCodecs(raw) {
if (raw == null)
return null;
return codecs.reduce((memo, codec) => {
try {
memo[codec.name] = codec.decode(raw);
}
catch (e) {
memo[codec.name] = null;
}
return memo;
}, { raw });
});
}
function makeDecodeNodeId(handlers) {
const decodeNodeIdWithCodecs = makeDecodeNodeIdRuntime(handlers);
return ($id) => (0, lambda_ts_1.lambda)($id, decodeNodeIdWithCodecs);
}
//# sourceMappingURL=node.js.map