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

[sitecore-jss] Add GraphQLLayoutService #614

Merged
merged 8 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/sitecore-jss/src/layout-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,59 @@ describe('LayoutService', () => {
});
});

it('should fetch layout data using custom search query', async () => {
nock('http://sctest')
.post('/graphql', (body) => {
return (
body.query.replace(/\n|\s/g, '') ===
'query{layout111(site:"supersite",routePath:"/styleguide",language:"en"){item{rendered}}}'
);
})
.reply(200, {
data: {
layout: {
item: {
rendered: {
sitecore: {
context: {
pageEditing: false,
site: { name: 'JssNextWeb' },
},
route: {
name: 'styleguide',
layoutId: 'xxx',
},
},
},
},
},
},
});

const service = new GraphQLLayoutService({
endpoint: 'http://sctest/graphql',
siteName: 'supersite',
formatSearchQuery: (siteName, itemPath, locale) =>
`query{layout111(site:"${siteName}",routePath:"${itemPath}",language:"${locale ||
'en'}"){item{rendered}}}`,
});

const data = await service.fetchLayoutData('/styleguide');

expect(data).to.deep.equal({
sitecore: {
context: {
pageEditing: false,
site: { name: 'JssNextWeb' },
},
route: {
name: 'styleguide',
layoutId: 'xxx',
},
},
});
});

it('should return error', async () => {
nock('http://sctest')
.post('/graphql')
Expand Down
14 changes: 12 additions & 2 deletions packages/sitecore-jss/src/layout-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export type GraphQLLayoutServiceConfig = {
* The JSS application name
*/
siteName: string;
/**
* Override default search query
* @param {string} siteName
* @param {string} itemPath
* @param {string} [locale]
* @returns {string} custom search query
*/
formatSearchQuery?: (siteName: string, itemPath: string, locale?: string) => string;
};
ambrauer marked this conversation as resolved.
Show resolved Hide resolved

interface FetchParams {
Expand Down Expand Up @@ -228,13 +236,15 @@ export class GraphQLLayoutService implements LayoutService {
* @returns {Promise<LayoutServiceData>} layout service data
*/
async fetchLayoutData(itemPath: string, language?: string): Promise<LayoutServiceData> {
const query = this.getLayoutQuery(itemPath, language);
const query = this.serviceConfig.formatSearchQuery
? this.serviceConfig.formatSearchQuery(this.serviceConfig.siteName, itemPath, language)
: this.getLayoutQuery(itemPath, language);
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved

const data = await this.createClient().request<{
layout: { item: { rendered: LayoutServiceData } };
}>(query);

return data.layout.item.rendered;
return data?.layout.item.rendered;
}

/**
Expand Down