can anyone help me?
how to mutate graphql query to add manga to anilist manga favorite by current login user?
this is how i done it
favorite.php
<?php
session_start();
header("Content-Type: application/json");
// Check if access token is available
if (!isset($_SESSION['access_token'])) {
echo json_encode(["error" => "Access token not found. Please log in first."]);
exit;
}
// Get Manga ID from AJAX request
$data = json_decode(file_get_contents("php://input"), true);
if (!isset($data['mangaId'])) {
echo json_encode(["error" => "No manga ID provided."]);
exit;
}
$mangaId = (int) $data['mangaId'];
// AniList API URL
$url = "https://graphql.anilist.co";
// GraphQL Mutation Query
$query = <<<GQL
mutation ($mangaId: Int) {
ToggleFavourite(mangaId: $mangaId) {
manga {
edges {
node {
id
title {
romaji
english
}
}
}
}
}
}
GQL;
// Prepare request payload
$variables = ["mangaId" => $mangaId];
$payload = json_encode(["query" => $query, "variables" => $variables]);
// Set headers
$headers = [
"Content-Type: application/json",
"Authorization: Bearer " . $_SESSION['access_token']
];
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute request and get response
$response = curl_exec($ch);
curl_close($ch);
file_put_contents("debug_log.txt", $response . PHP_EOL, FILE_APPEND);
echo $response;
?>
button
<div id="favoriteButton" class="bookmark dropright favourite">
<button class="btn btn-lg btn-secondary1 h-100" data-manga-id="<?php echo htmlspecialchars($manga['id']); ?>">
<span>Favorites</span>
<i class="fa-thin fa-heart"></i>
</button>
</div>
script
<script>
document.getElementById("favoriteButton").addEventListener("click", function () {
let mangaId = this.getAttribute("data-manga-id");
fetch("favorite.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ mangaId: mangaId })
})
.then(response => response.json())
.then(data => {
if (data.errors) {
alert("Error: " + data.errors[0].message);
} else {
alert("Manga successfully added to favorites!");
}
})
.catch(error => console.error("Error:", error));
});
</script>
the reponse return success but when i go to my anilist account, the manga is not there in favourites
the reponse return success but when i go to my anilist account, the manga is not there in favourites