mirror of
https://github.com/AnimeThemes/animethemes-web.git
synced 2026-07-11 01:24:31 +02:00
fix: Fixed pagination not working with limit (#213)
This commit is contained in:
@@ -287,6 +287,7 @@ export type QueryPlaylistArgs = {
|
||||
|
||||
export type QueryPlaylistAllArgs = {
|
||||
limit: InputMaybe<Scalars['Int']>;
|
||||
onlyNonEmpty: InputMaybe<Scalars['Boolean']>;
|
||||
orderBy: InputMaybe<Scalars['String']>;
|
||||
orderDesc: InputMaybe<Scalars['Boolean']>;
|
||||
};
|
||||
|
||||
@@ -277,7 +277,7 @@ export function apiResolver(config: ApiResolverConfig): GraphQLFieldResolver<Rec
|
||||
devLog.info(path + ": " + url);
|
||||
|
||||
// Limiting the concurrect requests is necessary to prevent timeouts.
|
||||
return await limit(() => (async () => {
|
||||
return limit(() => (async () => {
|
||||
if (!pagination) {
|
||||
devLog.info(`Fetching: ${url}`);
|
||||
let json: Record<string, unknown> | null;
|
||||
@@ -303,13 +303,14 @@ export function apiResolver(config: ApiResolverConfig): GraphQLFieldResolver<Rec
|
||||
} else {
|
||||
devLog.info(`Collecting: ${url}`);
|
||||
const results = [];
|
||||
let nextUrl = `${url}${url.includes("?") ? "&" : "?"}page[size]=${PAGINATION_PAGE_SIZE ?? 25}`;
|
||||
const pageSize = args.limit ?? PAGINATION_PAGE_SIZE ?? 25;
|
||||
let nextUrl: string | null = `${url}${url.includes("?") ? "&" : "?"}page[size]=${pageSize}`;
|
||||
while (nextUrl) {
|
||||
const json = await fetchJson(nextUrl, { headers }) as Record<string, unknown> & { links: { next: string } };
|
||||
context.apiRequests++;
|
||||
results.push(...transformer(extractor(json, parent, args), parent, args) as Array<unknown>);
|
||||
devLog.info(`Collecting: ${url}, Got ${results.length}`);
|
||||
nextUrl = json.links.next;
|
||||
nextUrl = !args.limit ? json.links.next : null;
|
||||
}
|
||||
|
||||
return results;
|
||||
|
||||
@@ -17,9 +17,10 @@ const resolvers: IResolvers = {
|
||||
extractor: (result) => result.animetheme
|
||||
}),
|
||||
themeAll: apiResolver({
|
||||
endpoint: (_, { limit, orderBy, orderDesc, has }) =>
|
||||
`/animetheme?sort=${orderDesc ? "-" : ""}${orderBy}&page[size]=${limit}&filter[has]=${has}`,
|
||||
extractor: (result) => result.animethemes
|
||||
endpoint: (_, { orderBy, orderDesc, has }) =>
|
||||
`/animetheme?sort=${orderDesc ? "-" : ""}${orderBy}&filter[has]=${has}`,
|
||||
extractor: (result) => result.animethemes,
|
||||
pagination: true,
|
||||
}),
|
||||
artist: apiResolver({
|
||||
endpoint: (_, { slug }) => `/artist/${slug}`,
|
||||
@@ -49,9 +50,10 @@ const resolvers: IResolvers = {
|
||||
pagination: true
|
||||
}),
|
||||
videoAll: apiResolver({
|
||||
endpoint: (_, { limit, orderBy, orderDesc }) =>
|
||||
`/video?sort=${orderDesc ? "-" : ""}${orderBy}&page[size]=${limit}`,
|
||||
extractor: (result) => result.videos
|
||||
endpoint: (_, { orderBy, orderDesc }) =>
|
||||
`/video?sort=${orderDesc ? "-" : ""}${orderBy}`,
|
||||
extractor: (result) => result.videos,
|
||||
pagination: true,
|
||||
}),
|
||||
year: (_, { value }) => ({ value }),
|
||||
yearAll: apiResolver({
|
||||
@@ -97,8 +99,8 @@ const resolvers: IResolvers = {
|
||||
extractor: (result) => result.playlist,
|
||||
}),
|
||||
playlistAll: apiResolver({
|
||||
endpoint: (_, { limit, orderBy, orderDesc }) =>
|
||||
`/playlist?sort=${orderDesc ? "-" : ""}${orderBy}&page[size]=${limit}&fields[playlist]=id,name,visibility,tracks_count`,
|
||||
endpoint: (_, { orderBy, orderDesc, onlyNonEmpty }) =>
|
||||
`/playlist?sort=${orderDesc ? "-" : ""}${orderBy}&fields[playlist]=id,name,visibility,tracks_count${onlyNonEmpty ? "&filter[playlist][tracks_count-gte]=1" : ""}`,
|
||||
extractor: (result) => result.playlists,
|
||||
pagination: true,
|
||||
}),
|
||||
|
||||
@@ -23,7 +23,7 @@ const typeDefs = `
|
||||
featuredTheme: FeaturedTheme
|
||||
dumpAll: [Dump!]!
|
||||
playlist(id: String!): Playlist
|
||||
playlistAll(limit: Int, orderBy: String, orderDesc: Boolean): [Playlist!]!
|
||||
playlistAll(limit: Int, orderBy: String, orderDesc: Boolean, onlyNonEmpty: Boolean): [Playlist!]!
|
||||
announcementAll: [Announcement!]!
|
||||
me: UserScopedQuery!
|
||||
}
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ export default function HomePage({ featuredTheme, announcementSources }: HomePag
|
||||
${PlaylistSummaryCard.fragments.showOwner}
|
||||
|
||||
query HomePageRecentlyAddedPlaylists {
|
||||
playlistAll(orderBy: "created_at", orderDesc: true, limit: 10) {
|
||||
playlistAll(orderBy: "created_at", orderDesc: true, limit: 10, onlyNonEmpty: true) {
|
||||
...PlaylistSummaryCardPlaylist
|
||||
...PlaylistSummaryCardShowOwner
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user