@@ -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
3738export type GraphGetLinksOptions = {
38- type ?: 'DEFAULT' | 'ITEMS' | 'ASSETS' | 'PATH' ;
39+ host ?: string ;
40+ locales ?: string [ ] ;
3941} ;
4042
4143export { 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
0 commit comments