i have two tables in mysql , products table (ids , name , …. etc ) and gallery table (include same ids & links).
i need to create graphql response using data inside the product table in addition the links from gallery table filter by product id
so how can i filter the gallery response based on the products id value
the main requirement for the task is :
– You need to create a GraphQL schema for categories/products and their fields – We expect to see attributes as part of Product Schema however they should be implemented as a type of their own and resolved through their own set of classes and not directly on the Product Schema/Resolver – You need to create a GraphQL mutation for inserting orders
- You need to create a GraphQL schema for categories/products and their fields
- We expect to see attributes as part of Product Schema however they should be implemented as a type of their own and resolved through their own set of classes and not directly on the Product Schema/Resolver
- You need to create a GraphQL mutation for inserting orders
my query :
===========
query Products {
products {
products {
id
gallery {
gallery {
product_idhuarache-x-stussy-le{
}
}
}
}
}
my result
==========
"data": {
"products": {
"products": [
{
"id": "huarache-x-stussy-le",
"gallery": {
"gallery": [
{
"product_id": "huarache-x-stussy-le"
},
{
"product_id": "jacket-canada-goosee"
},
]
}
},
{
"id": "jacket-canada-goosee",
"gallery": {
"gallery": [
{
"product_id": "huarache-x-stussy-le"
},
{
"product_id": "jacket-canada-goosee"
}, ]
}
}
]
}
}
}
Required Query
================
query Products {
products {
id
gallery {
product_id
}
}
}
Required Response
================
"data": {
"products": {
"products": [
{
"id": "huarache-x-stussy-le",
"gallery": {
"gallery": [
{
"product_id": "huarache-x-stussy-le"
},
]
}
},
{
"id": "jacket-canada-goosee",
"gallery": {
"gallery": [
{
"product_id": "jacket-canada-goosee"
}, ]
}
}
]
}
}
}
Products query code
=====================
class ProductsQuery
{
public function create_products_query()
{
$gallery = new GalleryQuery;
$gallery = $gallery->create_gallery_query();
//[1] list definition
$productslist = new ObjectType(
[
'name' => 'productstype',
'fields' => [
'id' => Type::string(),
'name' => Type::string(),
'inStock' => Type::string(),
'description' => Type::string(),
'gallery' => $gallery,
'category' => Type::string(),
'brand' => Type::string(),
'__typename' => Type::string(),
]
]
);
//[2] group the list inside an array
$allproductstype = new ObjectType(
[
'name' => 'allproductstype',
'fields' => [
'products' => Type::listof($productslist),
]
]
);
$products = [
'type' => $allproductstype,
// 'args' => [
// 'id' => Type::string(),
// ],
'resolve' => function ($rootValue, $args) {
$products_array = new Productstable();
$products = $products_array->products_array();
echo $products[0]['id'];
return
[
'products' => $products
// 'products' => array_filter($products, function ($products) use ($args) {
// if ($products['id'] == $args['id'])
// return true;
// else
// return false;
// })
];
}
];
return $products;
}
}
gallery query code
====================
class GalleryQuery
{
public function create_gallery_query()
{
// ===============================================================================
//[1] list definition
$gallerylist = new ObjectType(
[
'name' => 'gallerylist',
'fields' => [
'product_id' => Type::string(),
'link' => Type::string(),
]
]
);
//[2] group the list inside an array
$gallerytype = new ObjectType(
[
'name' => 'gallerytype',
'fields' => [
'gallery' => Type::listof($gallerylist)
]
]
);
//[3] put data inside the array
$gallery = [
'type' => $gallerytype,
// 'args' => [
// 'id' => Type::string(),
// ],
'resolve' => function ($rootValue, $args) {
$gallery_array = new Gallery;
$gallery = $gallery_array->gallery_array();
return
[
'gallery' => $gallery
// array_filter(
// $gallery,
// function ($gallery) use ($args) {
// if ($gallery['product_id'] == $args['id']) {
// return true;
// } else {
// return false;
// }
// }
// )
];
}
];
return $gallery;
}
}
My schema
============
$queryType = new ObjectType(
[
'name' => 'type1',
'fields' => [
'products' => $products,
],
]
);
//schema definition
$schema = new Schema(
(new SchemaConfig())
->setQuery($queryType)
);
Mena Nasr Bakhet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.