UNPKG

4.12 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __importDefault = (this && this.__importDefault) || function (mod) {
14 return (mod && mod.__esModule) ? mod : { "default": mod };
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.resolveReferences = void 0;
18var debug_1 = __importDefault(require("../debug"));
19var documentIds_1 = require("./documentIds");
20var unprefixDraftId_1 = require("./unprefixDraftId");
21var defaultResolveOptions = {
22 maxDepth: 5,
23 overlayDrafts: false,
24};
25// NOTE: This is now a public API and should be treated as such
26function resolveReferences(obj, context, options, currentDepth) {
27 if (options === void 0) { options = {}; }
28 if (currentDepth === void 0) { currentDepth = 0; }
29 var createNodeId = context.createNodeId, getNode = context.getNode;
30 var resolveOptions = __assign(__assign({}, defaultResolveOptions), options);
31 var overlayDrafts = resolveOptions.overlayDrafts, maxDepth = resolveOptions.maxDepth;
32 if (Array.isArray(obj)) {
33 return currentDepth <= maxDepth
34 ? obj.map(function (item) { return resolveReferences(item, context, resolveOptions, currentDepth + 1); })
35 : obj;
36 }
37 if (obj === null || typeof obj !== 'object') {
38 return obj;
39 }
40 if (typeof obj._ref === 'string') {
41 var targetId =
42 // If the reference starts with a '-', it means it's a Gatsby node ID,
43 // not a Sanity document ID. Thus, it does not need to be rewritten
44 obj._ref.startsWith('-')
45 ? obj._ref
46 : documentIds_1.safeId(overlayDrafts ? unprefixDraftId_1.unprefixDraftId(obj._ref) : obj._ref, createNodeId);
47 debug_1.default('Resolve %s (Sanity ID %s)', targetId, obj._ref);
48 var node = getNode(targetId);
49 if (!node && obj._weak) {
50 return null;
51 }
52 else if (!node) {
53 debug_1.default("Could not resolve reference to ID \"" + targetId + "\"");
54 return null;
55 }
56 return node && currentDepth <= maxDepth
57 ? resolveReferences(remapRawFields(node), context, resolveOptions, currentDepth + 1)
58 : obj;
59 }
60 var initial = {};
61 return Object.keys(obj).reduce(function (acc, key) {
62 var isRawDataField = key.startsWith('_rawData');
63 var value = resolveReferences(obj[key], context, resolveOptions, currentDepth + 1);
64 var targetKey = isRawDataField ? "_raw" + key.slice(8) : key;
65 acc[targetKey] = value;
66 return acc;
67 }, initial);
68}
69exports.resolveReferences = resolveReferences;
70/**
71 * When resolving a Gatsby node through resolveReferences, it's always (through the GraphQL API)
72 * operation on a "raw" field. The expectation is to have the return value be as close to the
73 * Sanity document as possible. Thus, when we've resolved the node, we want to remap the raw
74 * fields to be named as the original field name. `_rawSections` becomes `sections`. Since the
75 * value is fetched from the "raw" variant, the references inside it do not have their IDs
76 * rewired to their Gatsby equivalents.
77 */
78function remapRawFields(obj) {
79 var initial = {};
80 return Object.keys(obj).reduce(function (acc, key) {
81 if (key.startsWith('_rawData')) {
82 var targetKey = key.slice(8);
83 // Look for UpperCase variant first, if not found, try camelCase
84 targetKey =
85 typeof obj[targetKey] === 'undefined'
86 ? targetKey[0].toLowerCase() + targetKey.slice(1)
87 : targetKey;
88 acc[targetKey] = obj[key];
89 }
90 else if (!acc[key]) {
91 acc[key] = obj[key];
92 }
93 return acc;
94 }, initial);
95}
96//# sourceMappingURL=resolveReferences.js.map
\No newline at end of file