@@ -18,11 +18,14 @@ class HygraphApi
1818
1919 protected int $ ttl ;
2020
21+ protected string $ lang ;
22+
2123 public function __construct ()
2224 {
2325 $ this ->endpoint = config ('hygraph.content_api ' );
2426 $ this ->token = config ('hygraph.token ' );
2527 $ this ->ttl = config ('hygraph.cache_ttl ' );
28+ $ this ->lang = substr (app ()->getLocale (), 0 , 2 );
2629 }
2730
2831 protected function query (Query $ query ): array |object |null
@@ -60,9 +63,9 @@ public function pages(): array|object|null
6063
6164 public function page (string $ id ): array
6265 {
63- return Cache::remember ($ id , $ this ->ttl , function () use ($ id ) {
66+ return Cache::remember ($ this -> lang . ' _ ' . $ id , $ this ->ttl , function () use ($ id ) {
6467 $ gql = (new Query ('page ' ))
65- ->setArguments (['where ' => new RawObject ('{id: " ' .$ id .'"} ' )])
68+ ->setArguments (['locales ' => new RawObject ( $ this -> lang ), ' where ' => new RawObject ('{id: " ' .$ id .'"} ' )])
6669 ->setSelectionSet (
6770 [
6871 'id ' ,
@@ -152,9 +155,9 @@ public function redirects(): array
152155
153156 public function posts (): array |Collection |null
154157 {
155- return Cache::remember ('allPosts ' , $ this ->ttl , function () {
158+ return Cache::remember ($ this -> lang . ' _ ' . 'allPosts ' , $ this ->ttl , function () {
156159 $ gql = (new Query ('posts ' ))
157- ->setArguments (['orderBy ' => new RawObject ('publicationDate_DESC ' )])
160+ ->setArguments (['locales ' => new RawObject ( $ this -> lang ), ' orderBy ' => new RawObject ('publicationDate_DESC ' )])
158161 ->setSelectionSet (
159162 [
160163 'id ' ,
@@ -175,9 +178,9 @@ public function posts(): array|Collection|null
175178
176179 public function categories (): array |null
177180 {
178- return Cache::remember ('allPostCategories ' , $ this ->ttl , function () {
181+ return Cache::remember ($ this -> lang . ' _ ' . 'allPostCategories ' , $ this ->ttl , function () {
179182 $ gql = (new Query ('categories ' ))
180- ->setArguments (['orderBy ' => new RawObject ('name_ASC ' )])
183+ ->setArguments (['locales ' => new RawObject ( $ this -> lang ), ' orderBy ' => new RawObject ('name_ASC ' )])
181184 ->setSelectionSet (['id ' , 'name ' , 'slug ' ]);
182185
183186 return $ this ->query ($ gql )->categories ;
@@ -186,9 +189,9 @@ public function categories(): array|null
186189
187190 public function article (string $ slug ): object |null
188191 {
189- return Cache::remember ('article_ ' .$ slug , $ this ->ttl , function () use ($ slug ) {
192+ return Cache::remember ($ this -> lang . ' _ ' . 'article_ ' .$ slug , $ this ->ttl , function () use ($ slug ) {
190193 $ gql = (new Query ('post ' ))
191- ->setArguments (['where ' => new RawObject ('{slug: " ' .$ slug .'"} ' )])
194+ ->setArguments (['locales ' => new RawObject ( $ this -> lang ), ' where ' => new RawObject ('{slug: " ' .$ slug .'"} ' )])
192195 ->setSelectionSet (
193196 [
194197 'id ' ,
@@ -218,6 +221,11 @@ public function article(string $slug): object|null
218221 ),
219222 (new Query ('seo ' ))->setSelectionSet (['title ' , 'description ' , (new Query ('image ' ))->setSelectionSet (['url ' ])]),
220223 (new Query ('relatedPosts ' ))->setSelectionSet (['id ' ]),
224+ (new Query ('toc ' ))->setSelectionSet ([
225+ 'title ' ,
226+ 'slug ' ,
227+ (new Query ('tocLinks ' ))->setSelectionSet (['title ' , 'slug ' ]),
228+ ]),
221229 ]
222230 );
223231
@@ -231,9 +239,9 @@ public function article(string $slug): object|null
231239
232240 public function featuredPosts (): array |null
233241 {
234- return Cache::remember ('featuredPosts ' , $ this ->ttl , function () {
242+ return Cache::remember ($ this -> lang . ' _ ' . 'featuredPosts ' , $ this ->ttl , function () {
235243 $ gql = (new Query ('posts ' ))
236- ->setArguments (['where ' => new RawObject ('{isFeatured: true} ' ), 'orderBy ' => new RawObject ('publicationDate_DESC ' )])
244+ ->setArguments (['locales ' => new RawObject ( $ this -> lang ), ' where ' => new RawObject ('{isFeatured: true} ' ), 'orderBy ' => new RawObject ('publicationDate_DESC ' )])
237245 ->setSelectionSet (
238246 [
239247 'id ' ,
@@ -254,7 +262,7 @@ public function featuredPosts(): array|null
254262 public function relatedPosts (array $ ids ): array |null
255263 {
256264 $ gql = (new Query ('posts ' ))
257- ->setArguments (['where ' => new RawObject ('{id_in: [" ' .implode ('"," ' , $ ids ).'"]} ' )])
265+ ->setArguments (['locales ' => new RawObject ( $ this -> lang ), ' where ' => new RawObject ('{id_in: [" ' .implode ('"," ' , $ ids ).'"]} ' )])
258266 ->setSelectionSet (
259267 [
260268 'id ' ,
0 commit comments