Skip to content

Commit 6bcebbf

Browse files
authored
Merge pull request #177 from episerver/bugfix/CMS-47140-paths-order
Change sorting links in getPath
2 parents ecd883d + 375e32f commit 6bcebbf

File tree

1 file changed

+38
-13
lines changed
  • packages/optimizely-cms-sdk/src/graph

1 file changed

+38
-13
lines changed

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ query GetPath($where: _ContentWhereInput, $locale: [Locales]) {
6060
_Content(where: $where, locale: $locale) {
6161
item {
6262
_id
63+
_metadata {
64+
...on InstanceMetadata {
65+
path
66+
}
67+
}
6368
_link(type: PATH) {
6469
_Page {
6570
items {
@@ -70,6 +75,7 @@ query GetPath($where: _ContentWhereInput, $locale: [Locales]) {
7075
locale
7176
types
7277
url {
78+
base
7379
hierarchical
7480
default
7581
}
@@ -86,6 +92,11 @@ query GetPath($where: _ContentWhereInput, $locale: [Locales]) {
8692
_Content(where: $where, locale: $locale) {
8793
item {
8894
_id
95+
_metadata {
96+
...on InstanceMetadata {
97+
path
98+
}
99+
}
89100
_link(type: ITEMS) {
90101
_Page {
91102
items {
@@ -96,6 +107,7 @@ query GetPath($where: _ContentWhereInput, $locale: [Locales]) {
96107
locale
97108
types
98109
url {
110+
base
99111
hierarchical
100112
default
101113
}
@@ -111,6 +123,9 @@ type GetLinksResponse = {
111123
_Content: {
112124
item: {
113125
_id: string | null;
126+
_metadata: {
127+
path?: string[];
128+
};
114129
_link: {
115130
_Page: {
116131
items: Array<{
@@ -121,6 +136,7 @@ type GetLinksResponse = {
121136
locale?: string;
122137
types: string[];
123138
url?: {
139+
base?: string;
124140
hierarchical?: string;
125141
default?: string;
126142
};
@@ -350,21 +366,30 @@ export class GraphClient {
350366
return null;
351367
}
352368

353-
const links = data?._Content?.item._link._Page.items;
369+
const links = data._Content.item._link._Page.items;
370+
const sortedKeys = data._Content.item._metadata.path;
354371

355-
// Return sorted by "hierarchical"
356-
return links.toSorted((a, b) => {
357-
const ha = a?._metadata?.url?.hierarchical ?? '';
358-
const hb = b?._metadata?.url?.hierarchical ?? '';
372+
if (!sortedKeys) {
373+
// This is an error
374+
throw new GraphResponseError(
375+
'The `_metadata` does not contain any `path` field. Ensure that the path you requested is an actual page and not a block. If the problem persists, contact Optimizely support',
376+
{
377+
request: {
378+
query: GET_PATH_QUERY,
379+
variables: {
380+
...pathFilter(path, options?.host),
381+
...localeFilter(options?.locales),
382+
},
383+
},
384+
}
385+
);
386+
}
359387

360-
if (ha > hb) {
361-
return 1;
362-
}
363-
if (ha < hb) {
364-
return -1;
365-
}
366-
return 0;
367-
});
388+
// Return sorted by the "sortedKeys"
389+
const linkMap = new Map(links.map((link) => [link._metadata?.key, link]));
390+
return sortedKeys
391+
.map((key) => linkMap.get(key))
392+
.filter((item) => item !== undefined);
368393
}
369394

370395
/**

0 commit comments

Comments
 (0)