From 1776a21d7bcf04bc13a7077986890c7a8806dc4e Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Mon, 2 Aug 2021 14:46:53 -0500 Subject: [PATCH 01/10] Extend config and getAppRootId to accept jssAppTemplateId --- .../src/i18n/graphql-dictionary-service.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts index 545d52efe0..ded87ddf21 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts @@ -64,6 +64,11 @@ export interface GraphQLDictionaryServiceConfig extends SearchServiceConfig, Cac * @default '6d1cd89719364a3aa511289a94c2a7b1' (/sitecore/templates/System/Dictionary/Dictionary entry) */ dictionaryEntryTemplateId?: string; + + /** + * Optional. The template ID to use when searching for the appRootId. + */ + jssAppTemplateId?: string; } /** @@ -111,7 +116,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); From a0a4bfdafe1ab3a20e883a1282b785e0cb7c59f7 Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Mon, 2 Aug 2021 15:06:41 -0500 Subject: [PATCH 02/10] Update tsdoc comment for jssAppTemplateId --- packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts index ded87ddf21..c86726388b 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts @@ -67,6 +67,7 @@ export interface GraphQLDictionaryServiceConfig extends SearchServiceConfig, Cac /** * Optional. The template ID to use when searching for the appRootId. + * @default '061cba1554744b918a0617903b102b82' (/sitecore/templates/Foundation/JavaScript Services/App) */ jssAppTemplateId?: string; } From 241df069c84395eb0d7502c1a9008c1fdbf1c84f Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Mon, 2 Aug 2021 15:40:22 -0500 Subject: [PATCH 03/10] Extend config and getAppRootId to accept jssAppTemplateId --- .../src/services/graphql-sitemap-service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts index 0e9e8e5e02..e71cf5665b 100644 --- a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts +++ b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts @@ -76,6 +76,12 @@ export interface GraphQLSitemapServiceConfig extends SearchServiceConfig { * The API key to use for authentication. */ apiKey: string; + + /** + * Optional. The template ID to use when searching for the appRootId. + * @default '061cba1554744b918a0617903b102b82' (/sitecore/templates/Foundation/JavaScript Services/App) + */ + jssAppTemplateId?: string; } /** @@ -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); From 156cc3bd44641f853e24c320ecfcc627c18dfb76 Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Tue, 3 Aug 2021 08:44:56 -0500 Subject: [PATCH 04/10] Update tsdoc comments --- .../sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts | 2 +- packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts index e71cf5665b..a8e365a788 100644 --- a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts +++ b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts @@ -78,7 +78,7 @@ export interface GraphQLSitemapServiceConfig extends SearchServiceConfig { apiKey: string; /** - * Optional. The template ID to use when searching for the appRootId. + * 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; diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts index c86726388b..fd3dab86f4 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts @@ -66,7 +66,7 @@ export interface GraphQLDictionaryServiceConfig extends SearchServiceConfig, Cac dictionaryEntryTemplateId?: string; /** - * Optional. The template ID to use when searching for the appRootId. + * 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; From ccef1dad64e511c5a73ba7ce64ac2f3b259e426c Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Tue, 3 Aug 2021 11:59:59 -0500 Subject: [PATCH 05/10] Add unit test for dictionary-service --- .../i18n/graphql-dictionary-service.test.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts index 5a8ee13a42..e2490bf747 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts @@ -7,6 +7,21 @@ import { GraphQLDictionaryService } from '.'; import dictionaryQueryResponse from '../testData/mockDictionaryQueryResponse.json'; import appRootQueryResponse from '../testData/mockAppRootQueryResponse.json'; +/** + * @description helper function to generate a random GUID + * @returns a randomly generated GUID for testing purposes + * @see https://www.tutorialspoint.com/how-to-create-guid-uuid-in-javascript + */ + function generateGUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + /* eslint no-bitwise: off */ + const r = (Math.random() * 16) | 0, + v = c === 'x' ? r : (r & 0x3) | 0x8; + const GUID = v.toString(16); + return `{${GUID}}`; + }); +} + class TestService extends GraphQLDictionaryService { public client: GraphQLClient; constructor(options: GraphQLDictionaryServiceConfig) { @@ -81,6 +96,42 @@ describe('GraphQLDictionaryService', () => { expect(result).to.have.all.keys('foo', 'bar'); }); + it('should use a jssTemplateId, if provided', async () => { + const jssAppTemplateId = generateGUID(); + // mock a random template id + const randomId = generateGUID(); + 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/) From 67ca4a26220ff692db5e3e96f17ef55c9c5eb472 Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Tue, 3 Aug 2021 12:22:07 -0500 Subject: [PATCH 06/10] Add test for graphql-sitemap-service --- .../services/graphql-sitemap-service.test.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts index 2380a49d57..2c4dd53f15 100644 --- a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts +++ b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts @@ -10,6 +10,21 @@ import sitemapQueryResult from '../testData/sitemapQueryResult.json'; import sitemapServiceResult from '../testData/sitemapServiceResult'; import { GraphQLClient, GraphQLRequestClient } from '@sitecore-jss/sitecore-jss'; +/** + * @description helper function to generate a random GUID + * @returns a randomly generated GUID for testing purposes + * @see https://www.tutorialspoint.com/how-to-create-guid-uuid-in-javascript + */ +function generateGUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + /* eslint no-bitwise: off */ + const r = (Math.random() * 16) | 0, + v = c === 'x' ? r : (r & 0x3) | 0x8; + const GUID = v.toString(16); + return `{${GUID}}`; + }); +} + class TestService extends GraphQLSitemapService { public client: GraphQLClient; constructor(options: GraphQLSitemapServiceConfig) { @@ -318,6 +333,42 @@ describe('GraphQLSitemapService', () => { expect(sitemap).to.deep.equal(sitemapServiceResult); }); + it('should use a jssTemplateId, if provided', async () => { + const jssAppTemplateId = generateGUID(); + // mock a random template id + const randomId = generateGUID(); + + 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) From 784cd5454910f27801abf755da932c77d9407de6 Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Wed, 4 Aug 2021 07:22:16 -0500 Subject: [PATCH 07/10] Fix linting --- .../sitecore-jss/src/i18n/graphql-dictionary-service.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts index e2490bf747..008c1d23ed 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts @@ -12,7 +12,7 @@ import appRootQueryResponse from '../testData/mockAppRootQueryResponse.json'; * @returns a randomly generated GUID for testing purposes * @see https://www.tutorialspoint.com/how-to-create-guid-uuid-in-javascript */ - function generateGUID() { +function generateGUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { /* eslint no-bitwise: off */ const r = (Math.random() * 16) | 0, From 912b02524a20fdcc5ac10ea61a6a2a79cca48ec0 Mon Sep 17 00:00:00 2001 From: Coby Sher <63015754+CobyPear@users.noreply.github.com> Date: Wed, 4 Aug 2021 07:31:14 -0500 Subject: [PATCH 08/10] Quell Vue sample lint warning a

tag will automatically be close by the parser if immediatly followed by a number of elements, including