add graphql endpoint to get comments
This commit is contained in:
parent
356f839f9a
commit
63c2fde0de
3 changed files with 41 additions and 23 deletions
|
@ -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);
|
32
public/API/queries/comments.php
Normal file
32
public/API/queries/comments.php
Normal 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;
|
||||
|
||||
}
|
|
@ -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"]),
|
||||
]
|
||||
|
||||
],
|
||||
]);
|
||||
|
|
Loading…
Reference in a new issue