Skip to content

Commit fe1dc4d

Browse files
authored
Merge pull request #147 from episerver/feature/CMS-46146-split-methods
Split `getLinksByPath` into two methods
2 parents c189e70 + ebe1179 commit fe1dc4d

File tree

4 files changed

+93
-50
lines changed

4 files changed

+93
-50
lines changed

__test__/test-website/src/app/related/[...slug]/page.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { GraphClient } from '@optimizely/cms-sdk';
2-
import { OptimizelyComponent } from '@optimizely/cms-sdk/react/server';
32
import React from 'react';
43

54
type Props = {
@@ -17,38 +16,25 @@ export default async function Page({ params }: Props) {
1716
graphUrl: process.env.OPTIMIZELY_GRAPH_URL,
1817
});
1918

20-
const children = [
21-
...((await client.getLinksByPath(`/${slug.join('/')}/`)) ?? []),
22-
// NOTE: if you are using "simple address", you should fetch without trailing slash:
23-
...((await client.getLinksByPath(`/${slug.join('/')}`)) ?? []),
24-
];
25-
const ancestors = [
26-
...((await client.getLinksByPath(`/${slug.join('/')}/`, {
27-
type: 'PATH',
28-
})) ?? []),
29-
// Same here:
30-
...((await client.getLinksByPath(`/${slug.join('/')}`, {
31-
type: 'PATH',
32-
})) ?? []),
33-
];
34-
// .catch(handleGraphErrors);
19+
const children = (await client.getItems(`/${slug.join('/')}`)) ?? [];
20+
const ancestors = (await client.getPath(`/${slug.join('/')}`)) ?? [];
3521

3622
return (
3723
<div>
3824
<h1>Links from this page</h1>
3925
<h2>Children</h2>
4026
<ul>
4127
{children?.map((l) => (
42-
<li>
43-
{l?.displayName} ({l?.url?.default})
28+
<li key={l._metadata?.key}>
29+
{l?._metadata?.displayName} ({l?._metadata?.url?.default})
4430
</li>
4531
))}
4632
</ul>
4733
<h2>Ancestors (breadcrumbs)</h2>
4834
<ol>
4935
{ancestors?.map((l) => (
50-
<li>
51-
{l?.displayName} ({l?.url?.default})
36+
<li key={l._metadata?.key}>
37+
{l?._metadata?.displayName} ({l?._metadata?.url?.default})
5238
</li>
5339
))}
5440
</ol>

packages/optimizely-cms-sdk/src/graph/filters.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,17 @@ export function variationFilter(value: string): ContentInput {
9393
};
9494
}
9595

96+
export function localeFilter(locale?: string[]): ContentInput {
97+
return {
98+
locale,
99+
};
100+
}
101+
96102
/**
97103
* Arguments for querying content via the Graph API.
98104
*/
99105
export type ContentInput = {
106+
locale?: string[];
100107
variation?: GraphVariationInput;
101108
where?: ContentWhereInput;
102109
};

packages/optimizely-cms-sdk/src/graph/index.ts

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
pathFilter,
1414
previewFilter,
1515
GraphVariationInput,
16+
localeFilter,
1617
} from './filters.js';
1718

1819
/** Options for Graph */
@@ -35,7 +36,8 @@ export type GraphGetContentOptions = {
3536
};
3637

3738
export type GraphGetLinksOptions = {
38-
type?: 'DEFAULT' | 'ITEMS' | 'ASSETS' | 'PATH';
39+
host?: string;
40+
locales?: string[];
3941
};
4042

4143
export { GraphVariationInput };
@@ -53,15 +55,42 @@ query GetContentMetadata($where: _ContentWhereInput, $variation: VariationInput)
5355
}
5456
`;
5557

56-
const GET_LINKS_QUERY = `
57-
query GetLinks($where: _ContentWhereInput, $type: LinkTypes) {
58-
_Content(where: $where) {
58+
const GET_PATH_QUERY = `
59+
query GetPath($where: _ContentWhereInput, $locale: [Locale]) {
60+
_Content(where: $where, locale: $locale) {
5961
item {
6062
_id
61-
_link(type: $type) {
63+
_link(type: PATH) {
6264
_Page {
6365
items {
6466
_metadata {
67+
key
68+
sortOrder
69+
displayName
70+
locale
71+
types
72+
url {
73+
hierarchical
74+
default
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}`;
83+
84+
const GET_ITEMS_QUERY = `
85+
query GetPath($where: _ContentWhereInput, $locale: [Locale]) {
86+
_Content(where: $where, locale: $locale) {
87+
item {
88+
_id
89+
_link(type: ITEMS) {
90+
_Page {
91+
items {
92+
_metadata {
93+
key
6594
sortOrder
6695
displayName
6796
locale
@@ -86,6 +115,7 @@ type GetLinksResponse = {
86115
_Page: {
87116
items: Array<{
88117
_metadata?: {
118+
key: string;
89119
sortOrder?: number;
90120
displayName?: string;
91121
locale?: string;
@@ -254,41 +284,60 @@ export class GraphClient {
254284
return response?._Content?.items;
255285
}
256286

257-
async getLinksByPath(path: string, options?: GraphGetLinksOptions) {
258-
const input = {
259-
...pathFilter(path),
260-
type: options?.type,
261-
};
262-
const data = (await this.request(
263-
GET_LINKS_QUERY,
264-
input
265-
)) as GetLinksResponse;
287+
/**
288+
* Given the path of a page, return its "path" (i.e. a list of ancestor pages).
289+
*
290+
* @param path The URL of the current page
291+
* @returns A list with the metadata information of all ancestors sorted
292+
* from the top-most to the current
293+
*/
294+
async getPath(path: string, options?: GraphGetLinksOptions) {
295+
const data = (await this.request(GET_PATH_QUERY, {
296+
...pathFilter(path, options?.host),
297+
...localeFilter(options?.locales),
298+
})) as GetLinksResponse;
266299

267300
// Check if the page itself exist.
268301
if (!data._Content.item._id) {
269302
return null;
270303
}
271304

272-
const links = data?._Content?.item._link._Page.items.map(
273-
(i) => i._metadata
274-
);
305+
const links = data?._Content?.item._link._Page.items;
275306

276-
if (options?.type === 'PATH') {
277-
// Return sorted by "hierarchical"
278-
return links.toSorted((a, b) => {
279-
const ha = a?.url?.hierarchical ?? '';
280-
const hb = b?.url?.hierarchical ?? '';
307+
// Return sorted by "hierarchical"
308+
return links.toSorted((a, b) => {
309+
const ha = a?._metadata?.url?.hierarchical ?? '';
310+
const hb = b?._metadata?.url?.hierarchical ?? '';
281311

282-
if (ha > hb) {
283-
return 1;
284-
}
285-
if (ha < hb) {
286-
return -1;
287-
}
288-
return 0;
289-
});
312+
if (ha > hb) {
313+
return 1;
314+
}
315+
if (ha < hb) {
316+
return -1;
317+
}
318+
return 0;
319+
});
320+
}
321+
322+
/**
323+
* Given the path of a page, get its "items" (i.e. the children pages)
324+
*
325+
* @param path The URL of the current page
326+
* @returns A list with the metadata information of all child/descendant pages
327+
*/
328+
async getItems(path: string, options?: GraphGetLinksOptions) {
329+
const data = (await this.request(GET_ITEMS_QUERY, {
330+
...pathFilter(path, options?.host),
331+
...localeFilter(options?.locales),
332+
})) as GetLinksResponse;
333+
334+
// Check if the page itself exist.
335+
if (!data._Content.item._id) {
336+
return null;
290337
}
291338

339+
const links = data?._Content?.item._link._Page.items;
340+
292341
return links;
293342
}
294343

packages/optimizely-cms-sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
export {
1212
GraphClient,
1313
GraphGetContentOptions,
14+
GraphGetLinksOptions,
1415
GraphVariationInput,
1516
} from './graph/index.js';
1617
export type { PreviewParams } from './graph/index.js';

0 commit comments

Comments
 (0)