Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dictionary Service] [Sitemap Service] Provide ability to customize jssAppTemplateId #763

Merged
merged 12 commits into from
Aug 10, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,41 @@ describe('GraphQLSitemapService', () => {
expect(sitemap).to.deep.equal(sitemapServiceResult);
});

it('should use a jssTemplateId, if provided', async () => {
const jssAppTemplateId = '{de397294-cfcc-4795-847e-442416d0617b}';
const randomId = '{5a4e6edc-4518-4afb-afdc-9fa22ec4eb91}';

nock(endpoint)
.post('/', (body) => body.variables.jssAppTemplateId === jssAppTemplateId)
.reply(200, {
data: {
layout: {
homePage: {
rootItem: [
{
id: randomId,
},
],
},
},
},
});

nock(endpoint)
.post('/', (body) => body.variables.rootItemId === randomId)
.reply(200, sitemapQueryResult);

const service = new GraphQLSitemapService({
endpoint,
apiKey,
siteName,
jssAppTemplateId,
});

const sitemap = await service.fetchSSGSitemap(['ua']);
expect(sitemap).to.deep.equal(sitemapServiceResult);
});

it('should throw error if SitemapQuery fails', async () => {
mockRootItemIdRequest();
nock(endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export interface GraphQLSitemapServiceConfig extends SearchServiceConfig {
* The API key to use for authentication.
*/
apiKey: string;

/**
* Optional. The template ID of a JSS App to use when searching for the appRootId.
* @default '061cba1554744b918a0617903b102b82' (/sitecore/templates/Foundation/JavaScript Services/App)
*/
jssAppTemplateId?: string;
}

/**
Expand Down Expand Up @@ -156,7 +162,12 @@ export class GraphQLSitemapService {
// If the caller does not specify a root item ID, then we try to figure it out
const rootItemId =
this.options.rootItemId ||
(await getAppRootId(this.graphQLClient, this.options.siteName, languages[0]));
(await getAppRootId(
this.graphQLClient,
this.options.siteName,
languages[0],
this.options.jssAppTemplateId
));

if (!rootItemId) {
throw new Error(queryError);
Expand Down
36 changes: 36 additions & 0 deletions packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ describe('GraphQLDictionaryService', () => {
expect(result).to.have.all.keys('foo', 'bar');
});

it('should use a jssTemplateId, if provided', async () => {
const jssAppTemplateId = '{71d608ca-ac9c-4f1c-8e0a-85a6946e30f8}';
const randomId = '{412286b7-6d4f-4deb-80e9-108ee986c6e9}';

nock(endpoint)
.post('/', (body) => body.variables.jssAppTemplateId === jssAppTemplateId)
.reply(200, {
data: {
layout: {
homePage: {
rootItem: [
{
id: randomId,
},
],
},
},
},
});

nock(endpoint)
.post('/', (body) => body.variables.rootItemId === randomId)
.reply(200, dictionaryQueryResponse);

const service = new GraphQLDictionaryService({
endpoint,
apiKey,
siteName,
cacheEnabled: false,
jssAppTemplateId,
});

const result = await service.fetchDictionaryData('en');
expect(result).to.have.all.keys('foo', 'bar');
});

it('should throw error if could not resolve rootItemId', async () => {
nock(endpoint)
.post('/', /AppRootQuery/)
Expand Down
13 changes: 12 additions & 1 deletion packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export interface GraphQLDictionaryServiceConfig extends SearchServiceConfig, Cac
* @default '6d1cd89719364a3aa511289a94c2a7b1' (/sitecore/templates/System/Dictionary/Dictionary entry)
*/
dictionaryEntryTemplateId?: string;

/**
* Optional. The template ID of a JSS App to use when searching for the appRootId.
* @default '061cba1554744b918a0617903b102b82' (/sitecore/templates/Foundation/JavaScript Services/App)
*/
jssAppTemplateId?: string;
}

/**
Expand Down Expand Up @@ -111,7 +117,12 @@ export class GraphQLDictionaryService extends DictionaryServiceBase {
// If the caller does not specify a root item ID, then we try to figure it out
const rootItemId =
this.options.rootItemId ||
(await getAppRootId(this.graphQLClient, this.options.siteName, language));
(await getAppRootId(
this.graphQLClient,
this.options.siteName,
language,
this.options.jssAppTemplateId
));

if (!rootItemId) {
throw new Error(queryError);
Expand Down