I am learning GraphQL server with PHP, MySQL and I have following problems with nested objects.
This is my ProductType.php:
<?php
namespace AppTypes;
use GraphQLTypeDefinitionType;
use GraphQLTypeDefinitionObjectType;
use AppServicesProductService;
use AppTypesCategoryType;
use AppConfigsDatabase;
use AppModelsCategory;
use RuntimeException;
// Es aris GraphQL type, amis gareshe fetch ar mushaobs
class ProductType extends ObjectType
{
public function __construct()
{
// $categoryType = new CategoryType();
$categoryType = new ObjectType([
'name' => 'Category',
'fields' => [
'id' => Type::int(),
'name' => Type::string(),
],
]);
$config = [
'name' => 'Product',
'fields' => [
'id' => Type::string(),
'name' => Type::string(),
'in_stock' => Type::boolean(),
'description' => Type::string(),
'brand' => Type::string(),
'category_id' => Type::int(),
'category' => [
'type' => $categoryType,
'resolve' => function ($product) {
// Log the category_id to ensure it is being passed correctly
error_log("Fetching category for product with category_id: " . $product['category_id']);
// Check if category_id is valid
if (!$product['category_id']) {
error_log("No category_id provided.");
return null;
}
$db = new Database();
$conn = $db->getConnection();
$stmt = $conn->prepare("SELECT id, name FROM categories WHERE id = ?");
if (!$stmt) {
error_log('Database prepare error: ' . $conn->error);
return null;
}
$stmt->bind_param('i', $product['category_id']);
$stmt->execute();
$result = $stmt->get_result();
if (!$result) {
error_log('Database query error: ' . $conn->error);
return null;
}
$category = $result->fetch_assoc();
error_log("Fetched category: " . json_encode($category));
return $category;
},
],
],
];
parent::__construct($config);
}
}
This is my Product.php:
<?php
namespace AppModels;
use AppConfigsDatabase;
class Product
{
private $conn;
public function __construct()
{
$database = new Database();
$this->conn = $database->getConnection();
}
public function getProducts()
{
// Join products with categories to get category details
$sql = "
SELECT * from products
";
// SELECT p.id, p.name, p.in_stock, p.description, p.brand,
// c.id AS category_id, c.name AS category_name
// FROM products p
// LEFT JOIN categories c ON p.category_id = c.id
$result = $this->conn->query($sql);
$products = [];
while ($row = $result->fetch_assoc()) {
$products[] = [
'id' => $row['id'],
'name' => $row['name'],
'in_stock' => $row['in_stock'],
'description' => $row['description'],
// 'category' => [
// 'id' => $row['category_id'],
// 'name' => $row['category_name'],
// ],
'brand' => $row['brand'],
'category_id' => $row['category_id'],
];
}
return $products;
}
}
It doesn’t matter what I am trying to query or what code I write here, everytime I nest category on product, it returns a internal server error:
{
"errors": [
{
"message": "Internal server error",
"locations": [
{
"line": 2,
"column": 5
}
],
"path": [
"products",
0
]
}
],
"data": {
"products": [
null
]
}
}
I know this code looks messy and doesn’t seem too professional but maybe someone puts me on correct rails, how that works? What am I missing?
Nick Helper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.