gatsby-source-wordpress
Version:
Source data from WordPress in an efficient and scalable way.
61 lines (60 loc) • 2.05 kB
JavaScript
;
exports.__esModule = true;
exports.transformUnion = exports.transformListOfUnions = void 0;
var _helpers = require("../helpers");
const transformUnion = ({
field,
fieldName,
pluginOptions
}) => {
const prefix = pluginOptions.schema.typePrefix;
return {
type: (0, _helpers.buildTypeName)(field.type.name, prefix),
resolve: (source, _, context) => {
const resolvedField = source[fieldName] || source[`${field.name}__typename_${field.type.name}`];
if (resolvedField && resolvedField.id) {
const gatsbyNode = context.nodeModel.getNodeById({
id: resolvedField.id,
type: resolvedField.type
});
if (gatsbyNode) {
return gatsbyNode;
}
}
return resolvedField !== null && resolvedField !== void 0 ? resolvedField : null;
}
};
};
exports.transformUnion = transformUnion;
const transformListOfUnions = ({
field,
fieldName,
pluginOptions
}) => {
const prefix = pluginOptions.schema.typePrefix;
const typeSDLString = (0, _helpers.introspectionFieldTypeToSDL)(field.type);
return {
type: typeSDLString,
resolve: (source, _, context) => {
var _source$fieldName;
const resolvedField = (_source$fieldName = source[fieldName]) !== null && _source$fieldName !== void 0 ? _source$fieldName : source[`${field.name}__typename_${field.type.name}`];
if (!resolvedField && resolvedField !== false || !resolvedField.length) {
return null;
}
return resolvedField.reduce((accumulator, item) => {
const node = item !== null && item !== void 0 && item.id ? context.nodeModel.getNodeById({
id: item.id,
type: (0, _helpers.buildTypeName)(item.__typename, prefix)
}) : null;
if (node) {
accumulator.push(node);
} else if (item && !item.id) {
accumulator.push(item);
}
return accumulator;
}, []);
}
};
};
exports.transformListOfUnions = transformListOfUnions;
//# sourceMappingURL=transform-union.js.map