diff --git a/api/index.php b/api/index.php new file mode 100644 index 0000000000000000000000000000000000000000..e0c782fe18c5a2113d2f912cb2bb70d983f20e64 --- /dev/null +++ b/api/index.php @@ -0,0 +1,41 @@ +<?php +use GraphQL\GraphQL; +use GraphQL\Type\Schema; + +require "vendor/autoload.php"; +require "config.php"; +require "queries.php"; + +$conn = new mysqli($mysqlServer, $mysqlUser, $mysqlPassword, $mysqlDatabase); +// Check connection +if ($conn->connect_error) { + + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); +} + +$schema = new Schema([ + 'query' => $queryType +]); + +$rawInput = file_get_contents('php://input'); +$input = json_decode($rawInput, true); +$query = $input['query']; +$variableValues = isset($input['variables']) ? $input['variables'] : null; + +try { + $rootValue = [ + "db"=> $conn + ]; + $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues); + $output = $result->toArray(); +} catch (\Exception $e) { + $output = [ + 'errors' => [ + [ + 'message' => $e->getMessage() + ] + ] + ]; +} +header('Content-Type: application/json'); +echo json_encode($output); \ No newline at end of file diff --git a/api/queries.php b/api/queries.php new file mode 100644 index 0000000000000000000000000000000000000000..710469350122eb479add5d481f107e40a39edbeb --- /dev/null +++ b/api/queries.php @@ -0,0 +1,25 @@ +<?php +use GraphQL\Type\Definition\ObjectType; +use GraphQL\Type\Definition\Type; + +require "queries/products.php"; + +$queryType = new ObjectType([ + 'name' => 'Query', + 'fields' => [ + "products" => [ + 'type' => Type::listOf($product), + "args" => [ + "limit" => [ + "type" => Type::int(), + "defaultValue" => 100 + ], + "offset" => [ + "type" => Type::int(), + "defaultValue" => 0 + ] + ], + 'resolve' => fn ($rootValue, $args) => products($rootValue["db"], $args["limit"], $args["offset"]), + ] + ], +]); diff --git a/api/queries/products.php b/api/queries/products.php new file mode 100644 index 0000000000000000000000000000000000000000..2060f4ca1cbb9bb1774997b32c72f31f5165ee0e --- /dev/null +++ b/api/queries/products.php @@ -0,0 +1,29 @@ +<?php + +use GraphQL\Type\Definition\Type; +use GraphQL\Type\Definition\ObjectType; + +$product = new ObjectType([ + "name" => "Product", + "fields" => [ + "id" => Type::int(), + "manufacturer" => Type::string(), + "manufacturerid" => Type::int(), + "name" => Type::string(), + "amount" => Type::int(), + "position" => Type::string(), + "positionid" => Type::int(), + ] +]); + +function products($conn, $limit = -1, $offset = 0) +{ + $limit = $conn->real_escape_string($limit); + $offset = $conn->real_escape_string($offset); + $query = "SELECT products.id, manufacturer.name AS manufacturer, manufacturer.id AS manufacturerid, products.name, amount, position.name AS position, position.id AS positionid FROM products INNER JOIN position ON products.position = position.id INNER JOIN manufacturer ON products.manufacturer = manufacturer.id"; + if ($limit > 0) { + $query .= " LIMIT $limit OFFSET $offset"; + } + $result = $conn->query($query); + return $result->fetch_all(MYSQLI_ASSOC); +} \ No newline at end of file