Fix Directus GraphQL queries for content_pages and projects
Co-authored-by: denshooter <44590296+denshooter@users.noreply.github.com>
This commit is contained in:
@@ -142,14 +142,19 @@ export async function getContentPage(
|
|||||||
const query = `
|
const query = `
|
||||||
query {
|
query {
|
||||||
content_pages(
|
content_pages(
|
||||||
filter: { slug: { _starts_with: "${slug}" } }
|
filter: {
|
||||||
limit: 25
|
_and: [
|
||||||
|
{ slug: { _eq: "${slug}" } },
|
||||||
|
{ locale: { _eq: "${directusLocale}" } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
limit: 1
|
||||||
) {
|
) {
|
||||||
id
|
|
||||||
slug
|
slug
|
||||||
locale
|
locale
|
||||||
title
|
title
|
||||||
content
|
content
|
||||||
|
status
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
@@ -161,11 +166,28 @@ export async function getContentPage(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const pages = (result as any)?.content_pages || [];
|
const pages = (result as any)?.content_pages || [];
|
||||||
if (pages.length === 0) return null;
|
if (pages.length === 0) {
|
||||||
|
// Try without locale filter
|
||||||
|
const fallbackQuery = `
|
||||||
|
query {
|
||||||
|
content_pages(
|
||||||
|
filter: { slug: { _eq: "${slug}" } }
|
||||||
|
limit: 1
|
||||||
|
) {
|
||||||
|
slug
|
||||||
|
locale
|
||||||
|
title
|
||||||
|
content
|
||||||
|
status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const fallbackResult = await directusRequest('', { body: { query: fallbackQuery } });
|
||||||
|
const fallbackPages = (fallbackResult as any)?.content_pages || [];
|
||||||
|
return fallbackPages[0] || null;
|
||||||
|
}
|
||||||
|
|
||||||
// Prefer exact locale, otherwise fall back to first available
|
return pages[0];
|
||||||
const exact = pages.find((p: any) => p.locale === directusLocale);
|
|
||||||
return exact || pages[0];
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to fetch content page ${slug} (${locale}):`, error);
|
console.error(`Failed to fetch content page ${slug} (${locale}):`, error);
|
||||||
return null;
|
return null;
|
||||||
@@ -443,12 +465,11 @@ export async function getProjects(
|
|||||||
const filters = ['status: { _eq: "published" }'];
|
const filters = ['status: { _eq: "published" }'];
|
||||||
|
|
||||||
if (options?.featured !== undefined) {
|
if (options?.featured !== undefined) {
|
||||||
filters.push(`featured: { _eq: ${options.featured} }`);
|
filters.push(`featured: { _eq: ${options.featured ? 1 : 0} }`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options?.published !== undefined) {
|
// Remove published filter since it doesn't exist in Directus schema
|
||||||
filters.push(`published: { _eq: ${options.published} }`);
|
// The status field already handles published/draft state
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.category) {
|
if (options?.category) {
|
||||||
filters.push(`category: { _eq: "${options.category}" }`);
|
filters.push(`category: { _eq: "${options.category}" }`);
|
||||||
@@ -474,7 +495,7 @@ export async function getProjects(
|
|||||||
projects(
|
projects(
|
||||||
${filterString}
|
${filterString}
|
||||||
${limitString}
|
${limitString}
|
||||||
sort: ["-featured", "-created_at"]
|
sort: ["-featured", "-date_created"]
|
||||||
) {
|
) {
|
||||||
id
|
id
|
||||||
slug
|
slug
|
||||||
@@ -482,16 +503,14 @@ export async function getProjects(
|
|||||||
difficulty
|
difficulty
|
||||||
tags
|
tags
|
||||||
technologies
|
technologies
|
||||||
github_url
|
github
|
||||||
live_url
|
live
|
||||||
image_url
|
image_url
|
||||||
demo_video_url
|
demo_video
|
||||||
performance_metrics
|
|
||||||
screenshots
|
|
||||||
featured
|
|
||||||
published
|
|
||||||
date_created
|
date_created
|
||||||
date_updated
|
date_updated
|
||||||
|
featured
|
||||||
|
status
|
||||||
translations {
|
translations {
|
||||||
title
|
title
|
||||||
description
|
description
|
||||||
@@ -534,14 +553,14 @@ export async function getProjects(
|
|||||||
challenges: trans.challenges,
|
challenges: trans.challenges,
|
||||||
lessons_learned: trans.lessons_learned,
|
lessons_learned: trans.lessons_learned,
|
||||||
future_improvements: trans.future_improvements,
|
future_improvements: trans.future_improvements,
|
||||||
github_url: proj.github_url,
|
github_url: proj.github,
|
||||||
live_url: proj.live_url,
|
live_url: proj.live,
|
||||||
image_url: proj.image_url,
|
image_url: proj.image_url,
|
||||||
demo_video_url: proj.demo_video_url,
|
demo_video_url: proj.demo_video,
|
||||||
performance_metrics: proj.performance_metrics,
|
performance_metrics: proj.performance_metrics,
|
||||||
screenshots: proj.screenshots || [],
|
screenshots: proj.screenshots || [],
|
||||||
featured: proj.featured || false,
|
featured: proj.featured === 1 || proj.featured === true,
|
||||||
published: proj.published || false,
|
published: proj.status === 'published',
|
||||||
created_at: proj.date_created,
|
created_at: proj.date_created,
|
||||||
updated_at: proj.date_updated
|
updated_at: proj.date_updated
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user