add graphql endpoint to get comments

This commit is contained in:
Jonas Leder 2022-03-08 12:18:31 +01:00
parent 356f839f9a
commit 63c2fde0de
No known key found for this signature in database
GPG key ID: 8A53DD45A7D7B44B
3 changed files with 41 additions and 23 deletions

View file

@ -1,23 +0,0 @@
<?php
include "./lib/config.php";
include "./lib/mysql.php";
include "./lib/getGravatar.php";
$article = $conn->real_escape_string($_GET["article"]);
$responseJSON = [];
$result = $conn->query("SELECT * FROM comments WHERE article='$article'");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$commentElement = [
"name" => $row["name"],
"comment" => $row["comment"],
"gravatarURL" => get_gravatar($row["email"])
];
array_push($responseJSON, $commentElement);
}
}
header('Content-Type: application/json');
echo json_encode($responseJSON);

View file

@ -0,0 +1,32 @@
<?php
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
include "lib/getGravatar.php";
$commentField = new ObjectType([
"name" => "Comment",
"fields" => [
"name" => Type::string(),
"comment" => Type::string(),
"gravatarURL" => Type::string(),
"id" => Type::int()
],
]);
function comments($article, $conn) {
$response = [];
$result = $conn->query("SELECT * FROM comments WHERE article='$article'");
while ($row = $result->fetch_assoc()) {
$commentElement = [
"name" => $row["name"],
"comment" => $row["comment"],
"gravatarURL" => get_gravatar($row["email"]),
"id" => $row["id"]
];
array_push($response, $commentElement);
}
return $response;
}

View file

@ -4,6 +4,7 @@ use GraphQL\Type\Definition\Type;
require "./queries/skills.php";
require "./queries/blogPost.php";
require "./queries/comments.php";
$queryType = new ObjectType([
'name' => 'Query',
@ -34,6 +35,14 @@ $queryType = new ObjectType([
],
'resolve' => fn ($rootValue, $args) => blogPosts($args["count"], $args["contentLength"], $rootValue["db"]),
],
'comments' => [
"type" => Type::listOf($commentField),
"args" => [
"article" => Type::nonNull(Type::string()),
],
'resolve' => fn ($rootValue, $args) => comments($args["article"], $rootValue["db"]),
]
],
]);