Skip to content

Commit

Permalink
Fixing wishlist graphql error for missing model (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
annabic authored and Ilja Lapkovskis committed Jul 16, 2019
1 parent ab1b769 commit bfd097e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Model/Resolver/WishlistResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/wishlist-graphql
* @link https://github.com/scandipwa/wishlist-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\WishlistGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
use Magento\Wishlist\Model\Wishlist;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;

/**
* Fetches the Wishlist data according to the GraphQL schema
*/
class WishlistResolver implements ResolverInterface
{
/**
* @var WishlistResourceModel
*/
private $wishlistResource;

/**
* @var WishlistFactory
*/
private $wishlistFactory;

/**
* @param WishlistResourceModel $wishlistResource
* @param WishlistFactory $wishlistFactory
*/
public function __construct(WishlistResourceModel $wishlistResource, WishlistFactory $wishlistFactory)
{
$this->wishlistResource = $wishlistResource;
$this->wishlistFactory = $wishlistFactory;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$customerId = $context->getUserId();

/** @var Wishlist $wishlist */
$wishlist = $this->wishlistFactory->create();
$this->wishlistResource->load($wishlist, $customerId, 'customer_id');

if (null === $wishlist->getId()) {
return [
'model' => $wishlist,
];
}

return [
'sharing_code' => $wishlist->getSharingCode(),
'updated_at' => $wishlist->getUpdatedAt(),
'items_count' => $wishlist->getItemsCount(),
'name' => $wishlist->getName(),
'model' => $wishlist,
];
}
}
17 changes: 17 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/wishlist-graphql
* @link https://github.com/scandipwa/wishlist-graphql
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\WishlistGraphQl\Model\Resolver\WishlistResolver"
type="ScandiPWA\WishlistGraphQl\Model\Resolver\WishlistResolver"/>
</config>

0 comments on commit bfd097e

Please sign in to comment.