feat: create route to search for titles

Summary:

Test Plan:
This commit is contained in:
2024-05-16 01:21:06 -04:00
parent 359b73c7a8
commit 88b1a4ced5
13 changed files with 3808 additions and 4 deletions

View File

@@ -0,0 +1,43 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
const SearchQuery = graphql(`
query Search($query: String!, $page: Int!) {
Page(page: $page) {
media(search: $query, type: ANIME, sort: [POPULARITY_DESC, SCORE_DESC]) {
id
title {
userPreferred
english
}
coverImage {
extraLarge
large
medium
}
}
pageInfo {
hasNextPage
}
}
}
`);
export async function fetchSearchResultsFromAnilist(
query: string,
page: number,
) {
const client = new GraphQLClient("https://graphql.anilist.co/");
return client
.request(SearchQuery, { page, query })
.then((data) => data?.Page)
.then((page) => {
if (!page || page.media?.length === 0) {
return undefined;
}
const { media: results, pageInfo } = page;
return { results, hasNextPage: pageInfo?.hasNextPage };
});
}