Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Magento_SalesGraphQlAux to provide order state #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions SalesGraphQlAux/Model/Resolver/OrderState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SalesGraphQlAux\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Api\Data\OrderInterface;

/**
* @inheritdoc
*/
class OrderState implements ResolverInterface
{
/**
* @inheritDoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!(($value['model'] ?? null) instanceof OrderInterface)) {
throw new LocalizedException(__('"model" value should be specified, use customer query.'));
}

/** @var OrderInterface $order */
$order = $value['model'];

return $order->getState();
}
}
3 changes: 3 additions & 0 deletions SalesGraphQlAux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Magento_SalesGraphQlAux module

magento/module-sales-graph-ql-aux (Magento_SalesGraphQlAux) any changes which affect the core SalesGraphQl module which lives as magento/module-sales-graph-ql
25 changes: 25 additions & 0 deletions SalesGraphQlAux/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "magento/module-sales-graph-ql-aux",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.4.0||~8.1.0",
"magento/framework": "*",
"magento/module-sales-graph-ql": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\SalesGraphQlAux\\": ""
}
}
}
14 changes: 14 additions & 0 deletions SalesGraphQlAux/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_SalesGraphQlAux" >
<sequence>
<module name="Magento_SalesGraphQl"/>
</sequence>
</module>
</config>
6 changes: 6 additions & 0 deletions SalesGraphQlAux/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type CustomerOrder {
state: String! @doc(description: "The current state of the order.") @resolver(class: "Magento\\SalesGraphQlAux\\Model\\Resolver\\OrderState")
}
10 changes: 10 additions & 0 deletions SalesGraphQlAux/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_SalesGraphQlAux', __DIR__);
3 changes: 2 additions & 1 deletion _metapackage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"magento/module-catalog-graph-ql-aux": "*",
"magento/module-weee-graph-ql-aux": "*",
"magento/module-re-captcha-pwa": "*",
"magento/module-re-captcha-graph-ql-pwa": "*"
"magento/module-re-captcha-graph-ql-pwa": "*",
"magento/module-sales-graph-ql-aux": "*"
},
"type": "metapackage"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\SalesGraphQlAux;

use Magento\Sales\Model\Order;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class OrderStateTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

protected function setUp(): void
{
parent::setUp();
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
*/
public function testOrdersQuery()
{
$query =
<<<QUERY
query {
customer {
orders {
items {
order_number
state
}
}
}
}
QUERY;

$currentEmail = '[email protected]';
$currentPassword = 'password';
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));

$expectedData = [
[
'order_number' => '100000002',
'state' => Order::STATE_NEW
],
[
'order_number' => '100000003',
'state' => Order::STATE_PROCESSING
],
[
'order_number' => '100000004',
'state' => Order::STATE_PROCESSING
],
[
'order_number' => '100000005',
'state' => Order::STATE_COMPLETE
],
[
'order_number' => '100000006',
'state' => Order::STATE_COMPLETE
]
];

$actualData = $response['customerOrders']['items'];

foreach ($expectedData as $key => $data) {
$this->assertEquals(
$data['order_number'],
$actualData[$key]['order_number'],
"order_number is different than the expected for order - " . $data['order_number']
);
$this->assertEquals(
$data['state'],
$actualData[$key]['state'],
"state is different than the expected for order - " . $data['order_number']
);
}
}

/**
* @param string $email
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getCustomerAuthHeaders(string $email, string $password): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
}