diff --git a/public/API/getPost.php b/public/API/getPost.php deleted file mode 100644 index 9ac86e8..0000000 --- a/public/API/getPost.php +++ /dev/null @@ -1,24 +0,0 @@ -real_escape_string($_GET["id"]); -$result = $conn->query("SELECT * FROM posts WHERE id=$id"); -if ($result->num_rows > 0) { - $row = $result->fetch_assoc(); -} else { - die("Post not found"); -} - -$title = $row["title"]; -$content = $row["content"]; -$date = $row["date"]; -$id = $row["id"]; - -header('Content-Type: application/json'); -echo json_encode([ - "title" => $title, - "content" => $content, - "date" => $date, - "id" => $id -]); \ No newline at end of file diff --git a/public/API/graphql.php b/public/API/graphql.php index e7d7e59..cec07bc 100644 --- a/public/API/graphql.php +++ b/public/API/graphql.php @@ -17,7 +17,9 @@ $query = $input['query']; $variableValues = isset($input['variables']) ? $input['variables'] : null; try { - $rootValue = []; + $rootValue = [ + "db"=> $conn + ]; $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues); $output = $result->toArray(); } catch (\Exception $e) { diff --git a/public/API/queries/blogPost.php b/public/API/queries/blogPost.php new file mode 100644 index 0000000..0a7443d --- /dev/null +++ b/public/API/queries/blogPost.php @@ -0,0 +1,41 @@ + "Blog", + "fields" => [ + "title" => Type::string(), + "content" => Type::string(), + "date" => Type::string(), + "id" => Type::string(), + "error" => Type::string(), + ], +]); + +function blogPost($id, $conn) +{ + $id = $conn->real_escape_string($id); + $result = $conn->query("SELECT * FROM posts WHERE id=$id"); + if ($result->num_rows > 0) { + $row = $result->fetch_assoc(); + } else { + return [ + "error" => "Post not found" + ]; + } + + $title = $row["title"]; + $content = $row["content"]; + $date = $row["date"]; + $id = $row["id"]; + + return [ + "title" => $title, + "content" => $content, + "date" => $date, + "id" => $id, + "error" => "" + ]; +} diff --git a/public/API/queries/queries.php b/public/API/queries/queries.php index 5b89f41..5dd8d8c 100644 --- a/public/API/queries/queries.php +++ b/public/API/queries/queries.php @@ -3,6 +3,7 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; require "./queries/skills.php"; +require "./queries/blogPost.php"; $queryType = new ObjectType([ 'name' => 'Query', @@ -15,5 +16,12 @@ $queryType = new ObjectType([ 'type' => Type::listOf(Type::string()), 'resolve' => fn ($rootValue, $args) => getSkills(), ], + 'blogPost' => [ + "type" => $blogPostFields, + 'args' => [ + 'id' => Type::nonNull(Type::string()), + ], + 'resolve' => fn ($rootValue, $args) => blogPost($args["id"], $rootValue["db"]), + ], ], ]);