diff --git a/public/API/projectComments.php b/public/API/projectComments.php deleted file mode 100644 index e39b3fe9ea3a2ba8d73822f313c1b1f869ca73d3..0000000000000000000000000000000000000000 --- a/public/API/projectComments.php +++ /dev/null @@ -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); \ No newline at end of file diff --git a/public/API/queries/comments.php b/public/API/queries/comments.php new file mode 100644 index 0000000000000000000000000000000000000000..18abdfae6fe819eee3cb30c3cae4078c0fa0604c --- /dev/null +++ b/public/API/queries/comments.php @@ -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; + +} \ No newline at end of file diff --git a/public/API/queries/queries.php b/public/API/queries/queries.php index cdff9c395819165635f99571fd780dc4e18224d5..fe1d471be062864b5dafc6936da7fe7a175054ee 100644 --- a/public/API/queries/queries.php +++ b/public/API/queries/queries.php @@ -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"]), ] + ], ]);