add files
This commit is contained in:
99
src/lib/api/posts.ts
Normal file
99
src/lib/api/posts.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { fetchGraphQL } from './graphql-client.js';
|
||||
import type { ProfileArticle } from '../types/graphql.js';
|
||||
|
||||
// lib/api/posts.js
|
||||
export async function getLatestPosts(first = 12, after = null) {
|
||||
const query = `
|
||||
query GetLatestProfileArticles($first: Int!, $after: String) {
|
||||
profileArticles(
|
||||
first: $first,
|
||||
after: $after,
|
||||
where: {orderby: { field: DATE, order: DESC }}
|
||||
) {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
endCursor
|
||||
}
|
||||
edges {
|
||||
cursor
|
||||
node {
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
uri
|
||||
date
|
||||
featuredImage {
|
||||
node {
|
||||
sourceUrl(size: LARGE)
|
||||
altText
|
||||
}
|
||||
}
|
||||
author {
|
||||
node {
|
||||
id
|
||||
name
|
||||
firstName
|
||||
lastName
|
||||
avatar {
|
||||
url
|
||||
}
|
||||
uri
|
||||
}
|
||||
}
|
||||
categories {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
databaseId
|
||||
}
|
||||
}
|
||||
tags {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchGraphQL(query, { first, after });
|
||||
|
||||
// Преобразуем edges в nodes
|
||||
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
|
||||
|
||||
return {
|
||||
posts,
|
||||
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export interface AnewsPost {
|
||||
title: string;
|
||||
uri: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
export async function getLatestAnews(count = 12): Promise<AnewsPost[]> {
|
||||
const query = `
|
||||
query GetAnews($count: Int!) {
|
||||
aNews(first: $count, where: {orderby: {field: DATE, order: DESC}}) {
|
||||
nodes {
|
||||
title
|
||||
uri
|
||||
date
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchGraphQL(query, { count });
|
||||
return data.aNews?.nodes || []; // Исправлено: aNews вместо anews
|
||||
}
|
||||
Reference in New Issue
Block a user