0

I'm using graphql to get data to my PWA frontend project.Following is my schema.graphqls

type Query {
 comparelist: [ComparelistOutput] @resolver(class: "NeoSolax\\CompareList\\Model\\Resolver\\ComparelistItemResolver") @doc(description: "An array of items in the customer's compare list")
}
type ComparelistOutput {
 id:String
 sku:String
 url_key:String
}

ComparelistItemResolver.php

class ComparelistItemResolver implements ResolverInterface
{
 use NeoSolax\CompareList\Helper\getCompareList;
 public function __construct(
 getCompareList $getCompareList,
 
 ) {
 
 $this->getCompareList = $getCompareList
 
 }
 public function resolve(
 Field $field,
 $context,
 ResolveInfo $info,
 array $value = null,
 array $args = null
 ) {
 /** @var ContextInterface $context */
 $customerId = $context->getUserId();
 
 $comparelistItems = $this->getCompareList->getCompareListItems($customerId)->getItems();
 $data = [];
 foreach ($comparelistItems as $comparelistItem) {
 $itemProduct = $comparelistItem->getData();
 $data[] = [
 'id' => $itemProduct['product_id'],
 'sku' => $itemProduct['sku'],
 'url_key'=>$itemProduct['url_key']
 ];
 }
 return $data;
 
 }
}

Following is how I get my data to pwa frontend

const {data,loading:comparelistLoading}=useQuery(getCustomerCompareList);

data gets a empty array comparelist.What is wrong with my code?Please help

asked Dec 8, 2020 at 14:39
1
  • print_r($data):die; did you get any data while printing ? Commented Dec 9, 2020 at 5:46

2 Answers 2

0

Your $comparelistItems is empty. Therefore, you never perform the code inside the foreach statement, resulting in an empty $data array.

P.S.: $getCompareListis not a standard Magento class, therefore we cannot know if the problem resides inside the getCompareListItems() function you call to populate $compareListItems.

answered Dec 9, 2020 at 8:51
0

Try this

use NeoSolax\CompareList\Helper\getCompareList;
class ComparelistItemResolver implements ResolverInterface
{
 public function __construct(
 getCompareList $getCompareList,
 ) {
 $this->getCompareList = $getCompareList
 }
 public function resolve(
 Field $field,
 $context,
 ResolveInfo $info,
 array $value = null,
 array $args = null
 )
 {
 /** @var ContextInterface $context */
 $customerId = $context->getUserId();
 $comparelistItems = $this->getCompareList->getCompareListItems($customerId)->getItems();
 $data = [];
 $x = 0;
 foreach ($comparelistItems as $comparelistItem) {
 $itemProduct = $comparelistItem->getData();
 $data[$x]['id'] = $itemProduct['product_id'];
 $data[$x]['sku'] = $itemProduct['sku'];
 $data[$x]['url_key'] = $itemProduct['url_key'];
 $x++;
 }
 return $data;
 }
}

Reference link

answered Dec 9, 2020 at 9:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.