Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit d993186

Browse files
authored
Merge pull request #897 from solocommand/site-context
Allow overriding siteContext from field input
2 parents c934c53 + 6f046e6 commit d993186

File tree

2 files changed

+37
-5
lines changed
  • services/graphql-server/src/graphql

2 files changed

+37
-5
lines changed

services/graphql-server/src/graphql/definitions/platform/content/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ input ContentHashQueryInput {
362362
input ContentSiteContextInput {
363363
"Determines whether to use the \`content.linkUrl\` field for generating paths and URLs. If \`false\`, the \`linkUrl\` will be ignored."
364364
enableLinkUrl: Boolean = true
365+
"If specified, override the site context"
366+
siteId: ObjectID
365367
}
366368
367369
input ContentSitemapUrlsQueryInput {

services/graphql-server/src/graphql/resolvers/platform/content.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const momentTZ = require('moment-timezone');
1212
const cheerio = require('cheerio');
1313
const fetch = require('node-fetch');
1414
const readingTime = require('reading-time');
15-
15+
const loadSiteContext = require('../../../site-context/load');
1616
const newrelic = require('../../../newrelic');
1717
const defaults = require('../../defaults');
1818
const validateRest = require('../../utils/validate-rest');
@@ -41,6 +41,27 @@ const { validateYoutubePlaylistId, validateYoutubeChannelId, validateYoutubeUser
4141
const { MOST_POPULAR_CONTENT_API_URL } = require('../../../env');
4242
const optimizeForRss = require('../../utils/optimize-for-rss');
4343

44+
/**
45+
* @typedef {import('../../../routes/graphql').GraphQLServerContext} GraphQLServerContext
46+
* @typedef {import("../../../site-context")} SiteContext
47+
*/
48+
49+
/**
50+
*
51+
* @param {string} siteId
52+
* @param {GraphQLServerContext} context
53+
* @returns {SiteContext}
54+
*/
55+
const loadSiteContextFrom = async (siteId, context) => {
56+
const site = await loadSiteContext({
57+
basedb: context.basedb,
58+
siteId,
59+
tenant: context.tenant,
60+
enableCache: true,
61+
});
62+
return site;
63+
};
64+
4465
const retrieveYoutubePlaylistId = async ({ youtube }) => {
4566
const playlistId = get(youtube, 'playlistId');
4667
if (playlistId) return playlistId;
@@ -320,15 +341,24 @@ module.exports = {
320341
return links;
321342
},
322343

344+
/**
345+
*
346+
* @param {object} content
347+
* @param {object} variables
348+
* @param {GraphQLServerContext} ctx
349+
* @returns
350+
*/
323351
siteContext: async (content, { input }, ctx) => {
324-
const { enableLinkUrl } = input;
325-
const { site, load, basedb } = ctx;
352+
const { enableLinkUrl, siteId } = input;
353+
const { load, basedb } = ctx;
354+
const site = siteId ? await loadSiteContextFrom(siteId, ctx) : ctx.site;
326355
if (!site.exists()) throw new UserInputError('A website context must be set to generate `Content.siteContext` fields.');
356+
const context = { ...ctx, site };
327357

328358
return {
329-
path: () => canonicalPathFor(content, ctx, { enableLinkUrl }),
359+
path: () => canonicalPathFor(content, context, { enableLinkUrl }),
330360
url: async () => {
331-
const path = await canonicalPathFor(content, ctx, { enableLinkUrl });
361+
const path = await canonicalPathFor(content, context, { enableLinkUrl });
332362
if (/^http/i.test(path)) return path;
333363
return `${site.get('origin')}${path}`;
334364
},

0 commit comments

Comments
 (0)