46
loading...
This website collects cookies to deliver better user experience
https://graphql.anilist.co
const axios = require("axios");
const query = `
query ($page: Int, $perPage: Int, $search: String) {
Page (page: $page, perPage: $perPage) {
pageInfo {
total
perPage
}
media (search: $search, type: ANIME, sort: FAVOURITES_DESC){
id
title {
romaji
english
native
}
type
genres
}
}
};`
query
is the beginning of our query in which we have to initialize the variables that we want to use. By default, the API returns a single result object so if we want multiple results, we have to wrap our media query inside a Page
query. The Page query also provides the pageInfo
field which provides information about the current page and total results count.media
query is where the magic happens. Here we can just pass in the variable we passed in our parent query
to get corresponding results filtered by that search string. In my example, I am just fetching the id, title, type and the genres of top 3 anime based on users rating and the search query (at the time of writing) but you can fetch different kinds of data and apply multiple types of sorting. This is the type of flexibility that GraphQL offers making it much easier to make API calls. You can check out the Anilist GraphQL Reference guide here.variables
object. Here, I am just getting a single page of result with only 3 items.let variables = {
search: query,
page: 1,
perPage: 3,
};
async function getAnime(query) {
const query = `
query ($page: Int, $perPage: Int, $search: String) {
Page(page: $page, perPage: $perPage) {
pageInfo {
total
perPage
}
media(search: $search, type: ANIME, sort: FAVOURITES_DESC) {
id
title {
romaji
english
native
}
type
genres
}
}
}
`;
let variables = {
search: query,
page: 1,
perPage: 3,
};
const headers = {
"Content-Type": "application/json",
Accept: "application/json",
};
const result = await axios({
method: 'post',
query,
variables,
headers
}).catch((err) => console.log(err.message));
{
"data": {
"Page": {
"pageInfo": {
"total": 57,
"perPage": 5
},
"media": [
{
"id": 16498,
"title": {
"romaji": "Shingeki no Kyojin",
"english": "Attack on Titan",
"native": "進撃の巨人"
},
"type": "ANIME",
"genres": [
"Action",
"Drama",
"Fantasy",
"Mystery"
]
},
{
"id": 110277,
"title": {
"romaji": "Shingeki no Kyojin: The Final Season",
"english": "Attack on Titan Final Season",
"native": "進撃の巨人 The Final Season"
},
"type": "ANIME",
"genres": [
"Action",
"Drama",
"Fantasy",
"Mystery"
]
},
{
"id": 104578,
"title": {
"romaji": "Shingeki no Kyojin 3 Part 2",
"english": "Attack on Titan Season 3 Part 2",
"native": "進撃の巨人 3 Part.2"
},
"type": "ANIME",
"genres": [
"Action",
"Drama",
"Fantasy",
"Mystery"
]
},
{
"id": 30,
"title": {
"romaji": "Shin Seiki Evangelion",
"english": "Neon Genesis Evangelion",
"native": "新世紀エヴァンゲリオン"
},
"type": "ANIME",
"genres": [
"Action",
"Drama",
"Mecha",
"Mystery",
"Psychological",
"Sci-Fi"
]
},
{
"id": 99147,
"title": {
"romaji": "Shingeki no Kyojin 3",
"english": "Attack on Titan Season 3",
"native": "進撃の巨人 3"
},
"type": "ANIME",
"genres": [
"Action",
"Drama",
"Fantasy",
"Mystery"
]
}
]
}
}
}