UNPKG

gatsby-source-wordpress

Version:

Source data from WordPress in an efficient and scalable way.

226 lines (219 loc) • 7.73 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.runFnForEachNodeQuery = exports.getGatsbyNodeTypeNames = exports.getContentTypeQueryInfos = exports.fetchWPGQLContentNodesByContentType = exports.fetchWPGQLContentNodes = exports.fetchAndCreateAllNodes = void 0; var _createNodes = require("../create-nodes/create-nodes"); var _fetchNodesPaginated = require("./fetch-nodes-paginated"); var _formatLogMessage = require("../../../utils/format-log-message"); var _constants = require("../../../constants"); var _gatsbyVersion = require("../../../utils/gatsby-version"); var _store = require("../../../store"); var _getGatsbyApi = require("../../../utils/get-gatsby-api"); var _chunk = _interopRequireDefault(require("lodash/chunk")); var _cache = require("../../../utils/cache"); var _helpers = require("../../create-schema-customization/helpers"); var _gatsbyFeatures = require("../../../utils/gatsby-features"); /** * fetchWPGQLContentNodes * * fetches and paginates remote nodes by post type while reporting progress */ const fetchWPGQLContentNodes = async ({ queryInfo }) => { const { pluginOptions, helpers } = (0, _store.getStore)().getState().gatsbyApi; const { reporter } = helpers; const { url, schema: { perPage } } = pluginOptions; const { nodeListQueries, typeInfo, settings } = queryInfo; const typeName = typeInfo.nodesTypeName; (0, _store.getStore)().dispatch.logger.createActivityTimer({ typeName, pluginOptions, reporter }); let allNodesOfContentType = []; // there's normally just one query here, but more can be added using the settings.nodeListQueries api for (const nodeListQuery of nodeListQueries) { const contentNodes = await (0, _fetchNodesPaginated.paginatedWpNodeFetch)({ first: perPage, after: null, contentTypePlural: typeInfo.pluralName, nodeTypeName: typeInfo.nodesTypeName, query: nodeListQuery, url, settings, helpers }); allNodesOfContentType = [...allNodesOfContentType, ...contentNodes]; } (0, _store.getStore)().dispatch.logger.stopActivityTimer({ typeName }); if (allNodesOfContentType && allNodesOfContentType.length) { return { singular: queryInfo.typeInfo.singularName, plural: queryInfo.typeInfo.pluralName, allNodesOfContentType }; } return false; }; /** * getContentTypeQueryInfos * * returns query infos (Type info & GQL query strings) filtered to * remove types that are excluded in the plugin options * * @returns {Array} Type info & GQL query strings */ exports.fetchWPGQLContentNodes = fetchWPGQLContentNodes; const getContentTypeQueryInfos = () => { const { nodeQueries } = (0, _store.getStore)().getState().remoteSchema; const queryInfos = Object.values(nodeQueries).filter(({ settings }) => !settings.exclude); return queryInfos; }; exports.getContentTypeQueryInfos = getContentTypeQueryInfos; let cachedGatsbyNodeTypeNames = null; const getGatsbyNodeTypeNames = () => { if (cachedGatsbyNodeTypeNames) { return cachedGatsbyNodeTypeNames; } const { typeMap } = (0, _store.getStore)().getState().remoteSchema; const queryableTypenames = getContentTypeQueryInfos().map(query => query.typeInfo.nodesTypeName); const implementingNodeTypes = queryableTypenames.reduce((accumulator, typename) => { var _type$possibleTypes; const type = typeMap.get(typename); if ((_type$possibleTypes = type.possibleTypes) !== null && _type$possibleTypes !== void 0 && _type$possibleTypes.length) { accumulator = [...accumulator, ...type.possibleTypes.map(({ name }) => name)]; } return accumulator; }, []); const allTypeNames = [...new Set([...queryableTypenames, ...implementingNodeTypes])]; const allBuiltTypeNames = allTypeNames.map(typename => (0, _helpers.buildTypeName)(typename)); const typeNameList = [...allTypeNames, ...allBuiltTypeNames]; if (typeNameList.length) { cachedGatsbyNodeTypeNames = typeNameList; } return typeNameList; }; /** * fetchWPGQLContentNodesByContentType * * fetches nodes from the remote WPGQL server and groups them by post type * * @returns {Array} */ exports.getGatsbyNodeTypeNames = getGatsbyNodeTypeNames; const runFnForEachNodeQuery = async fn => { var _getPluginOptions, _getPluginOptions$sch; const nodeQueries = getContentTypeQueryInfos(); const chunkSize = ((_getPluginOptions = (0, _getGatsbyApi.getPluginOptions)()) === null || _getPluginOptions === void 0 ? void 0 : (_getPluginOptions$sch = _getPluginOptions.schema) === null || _getPluginOptions$sch === void 0 ? void 0 : _getPluginOptions$sch.requestConcurrency) || 15; const chunkedQueries = (0, _chunk.default)(nodeQueries, chunkSize); for (const queries of chunkedQueries) { await Promise.all(queries.map(async queryInfo => { if ( // if the type settings call for lazyNodes, don't fetch them upfront here queryInfo.settings.lazyNodes && // but not in Gatsby v4+ since lazyNodes isn't supported in 4+ !_gatsbyVersion.usingGatsbyV4OrGreater || // for media items we only want to fetch referenced nodes so don't fetch them here. queryInfo.typeInfo.nodesTypeName === `MediaItem`) { return; } await fn({ queryInfo }); })); } }; exports.runFnForEachNodeQuery = runFnForEachNodeQuery; const fetchWPGQLContentNodesByContentType = async () => { const contentNodeGroups = []; await runFnForEachNodeQuery(async ({ queryInfo }) => { const contentNodeGroup = await fetchWPGQLContentNodes({ queryInfo }); if (contentNodeGroup) { contentNodeGroups.push(contentNodeGroup); } }); return contentNodeGroups; }; /** * fetchAndCreateAllNodes * * uses query info (generated from introspection in onPreBootstrap) to * fetch and create Gatsby nodes from any lists of nodes in the remote schema */ exports.fetchWPGQLContentNodesByContentType = fetchWPGQLContentNodesByContentType; const fetchAndCreateAllNodes = async () => { const { helpers } = (0, _getGatsbyApi.getGatsbyApi)(); const { reporter } = helpers; // // fetch nodes from WPGQL const activity = reporter.activityTimer((0, _formatLogMessage.formatLogMessage)(`fetching nodes`)); activity.start(); (0, _store.getStore)().subscribe(() => { activity.setStatus(`${(0, _store.getStore)().getState().logger.entityCount} total`); }); let createdNodeIds; const hardCachedNodes = await (0, _cache.getHardCachedNodes)(); if (!hardCachedNodes) { const wpgqlNodesByContentType = await fetchWPGQLContentNodesByContentType(); const createNodesActivity = reporter.activityTimer((0, _formatLogMessage.formatLogMessage)(`creating nodes`)); createNodesActivity.start(); // // Create Gatsby nodes from WPGQL response createdNodeIds = await (0, _createNodes.createGatsbyNodesFromWPGQLContentNodes)({ wpgqlNodesByContentType, createNodesActivity }); await (0, _cache.setHardCachedNodes)({ helpers }); createNodesActivity.end(); activity.end(); } else if (hardCachedNodes) { createdNodeIds = await (0, _cache.restoreHardCachedNodes)({ hardCachedNodes }); } if (_gatsbyFeatures.needToTouchNodes) { // save the node id's so we can touch them on the next build // so that we don't have to refetch all nodes await (0, _cache.setPersistentCache)({ key: _constants.CREATED_NODE_IDS, value: createdNodeIds }); } }; exports.fetchAndCreateAllNodes = fetchAndCreateAllNodes; //# sourceMappingURL=fetch-nodes.js.map