Skip to content

Commit ad85c95

Browse files
committed
magento/graphql-ce#41: [Query] My Account > My Orders
1 parent 8a7f23e commit ad85c95

File tree

11 files changed

+438
-2
lines changed

11 files changed

+438
-2
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesGraphQl\Model\Resolver;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
15+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccountInterface;
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
class Orders implements ResolverInterface
21+
{
22+
/**
23+
* @var UserContextInterface
24+
*/
25+
private $userContext;
26+
27+
/**
28+
* @var CollectionFactoryInterface
29+
*/
30+
private $collectionFactory;
31+
32+
/**
33+
* @var CheckCustomerAccountInterface
34+
*/
35+
private $checkCustomerAccount;
36+
37+
/**
38+
* Orders constructor.
39+
* @param UserContextInterface $userContext
40+
* @param CollectionFactoryInterface $collectionFactory
41+
*/
42+
public function __construct(
43+
UserContextInterface $userContext,
44+
CollectionFactoryInterface $collectionFactory,
45+
CheckCustomerAccountInterface $checkCustomerAccount
46+
) {
47+
$this->userContext = $userContext;
48+
$this->collectionFactory = $collectionFactory;
49+
$this->checkCustomerAccount = $checkCustomerAccount;
50+
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function resolve(
57+
Field $field,
58+
$context,
59+
ResolveInfo $info,
60+
array $value = null,
61+
array $args = null
62+
) {
63+
64+
$customerId = $this->userContext->getUserId();
65+
66+
$this->checkCustomerAccount->execute($customerId, $this->userContext->getUserType());
67+
68+
$orders = $this->collectionFactory->create($customerId);
69+
$items = [];
70+
71+
// @TODO Add shipping & billing address in response
72+
// @TODO Add order currency object in response
73+
/** @var \Magento\Sales\Model\Order $order */
74+
foreach ($orders as $order) {
75+
$items[] = [
76+
'id' => $order->getId(),
77+
'increment_id' => $order->getIncrementId(),
78+
'created_at' => $order->getCreatedAt(),
79+
'grant_total' => $order->getGrandTotal(),
80+
'state' => $order->getState(),
81+
'status' => $order->getStatus()
82+
];
83+
}
84+
85+
return ['items' => $items];
86+
}
87+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SalesGraphQl
2+
3+
**SalesGraphQl** provides type and resolver information for the GraphQl module
4+
to generate sales orders information endpoints.
5+
6+
Also will provides endpoints for modifying an order.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "magento/module-sales-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-customer": "*",
9+
"magento/module-catalog": "*",
10+
"magento/module-store": "*"
11+
},
12+
"suggest": {
13+
"magento/module-graph-ql": "*"
14+
},
15+
"license": [
16+
"OSL-3.0",
17+
"AFL-3.0"
18+
],
19+
"autoload": {
20+
"files": [
21+
"registration.php"
22+
],
23+
"psr-4": {
24+
"Magento\\SalesGraphQl\\": ""
25+
}
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_SalesGraphQl"/>
10+
</config>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
customerOrders: Orders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Orders") @doc(description: "List of customer orders")
6+
}
7+
8+
type Order @doc(description: "Order mapping fields") {
9+
id: Int
10+
increment_id: String
11+
created_at: String
12+
grant_total: Float
13+
state: String
14+
status: String
15+
}
16+
17+
type Orders {
18+
items: [Order] @doc(description: "Array of orders")
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_SalesGraphQl', __DIR__);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* PHPUnit configuration for GraphQL web API functional tests.
5+
*
6+
* Copyright © Magento, Inc. All rights reserved.
7+
* See COPYING.txt for license details.
8+
*/
9+
-->
10+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd"
12+
colors="true"
13+
columns="max"
14+
beStrictAboutTestsThatDoNotTestAnything="false"
15+
bootstrap="./framework/bootstrap.php"
16+
>
17+
<!-- Test suites definition -->
18+
<testsuites>
19+
<testsuite name="Magento GraphQL web API functional tests">
20+
<directory suffix="Test.php">testsuite/Magento/GraphQl</directory>
21+
</testsuite>
22+
</testsuites>
23+
24+
<!-- PHP INI settings and constants definition -->
25+
<php>
26+
<includePath>./testsuite</includePath>
27+
<const name="TESTS_INSTALL_CONFIG_FILE" value="config/install-config-mysql.php"/>
28+
<const name="TESTS_GLOBAL_CONFIG_FILE" value="config/config-global.php"/>
29+
<!-- Webserver URL -->
30+
<const name="TESTS_BASE_URL" value="http://magento.inno"/>
31+
<!-- Webserver API user -->
32+
<const name="TESTS_WEBSERVICE_USER" value="admin"/>
33+
<!-- Webserver API key -->
34+
<const name="TESTS_WEBSERVICE_APIKEY" value="123123q"/>
35+
<!-- Define if debugger should be started using XDEBUG_SESSION cookie -->
36+
<const name="TESTS_XDEBUG_ENABLED" value="true"/>
37+
<!-- Define XDEBUG_SESSION cookie value-->
38+
<const name="TESTS_XDEBUG_SESSION" value="MEETMAGENTO" />
39+
40+
<ini name="date.timezone" value="America/Los_Angeles"/>
41+
42+
<!-- Semicolon-separated 'glob' patterns, that match global XML configuration files -->
43+
<const name="TESTS_GLOBAL_CONFIG_DIR" value="../../../app/etc"/>
44+
<!-- Whether to cleanup the application before running tests or not -->
45+
<const name="TESTS_CLEANUP" value="enabled"/>
46+
<!--Defines if Magento should be installed before tests execution-->
47+
<const name="TESTS_MAGENTO_INSTALLATION" value="disabled"/>
48+
<!-- Magento mode for tests execution. Possible values are "default", "developer" and "production". -->
49+
<const name="TESTS_MAGENTO_MODE" value="default"/>
50+
</php>
51+
52+
<!-- Test listeners -->
53+
<listeners>
54+
<listener class="Magento\TestFramework\Event\PhpUnit"/>
55+
</listeners>
56+
</phpunit>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Sales;
9+
10+
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
/**
15+
* Class OrdersTest
16+
*/
17+
class OrdersTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var CustomerTokenServiceInterface
21+
*/
22+
private $customerTokenService;
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function setUp()
28+
{
29+
parent::setUp();
30+
$objectManager = Bootstrap::getObjectManager();
31+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
32+
}
33+
34+
/**
35+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
36+
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
37+
*/
38+
public function testOrdersQuery()
39+
{
40+
$query =
41+
<<<QUERY
42+
query {
43+
customerOrders {
44+
items {
45+
id
46+
increment_id
47+
created_at
48+
grant_total
49+
state
50+
status
51+
}
52+
}
53+
}
54+
QUERY;
55+
56+
$currentEmail = '[email protected]';
57+
$currentPassword = 'password';
58+
59+
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
60+
61+
$expectedData = [
62+
[
63+
'increment_id' => '100000002',
64+
'state' => \Magento\Sales\Model\Order::STATE_NEW,
65+
'status' => 'processing',
66+
'grant_total' => 120.00
67+
],
68+
[
69+
'increment_id' => '100000003',
70+
'state' => \Magento\Sales\Model\Order::STATE_PROCESSING,
71+
'status' => 'processing',
72+
'grant_total' => 130.00
73+
],
74+
[
75+
'increment_id' => '100000004',
76+
'state' => \Magento\Sales\Model\Order::STATE_PROCESSING,
77+
'status' => 'closed',
78+
'grant_total' => 140.00
79+
],
80+
[
81+
'increment_id' => '100000005',
82+
'state' => \Magento\Sales\Model\Order::STATE_COMPLETE,
83+
'status' => 'complete',
84+
'grant_total' => 150.00
85+
],
86+
[
87+
'increment_id' => '100000006',
88+
'state' => \Magento\Sales\Model\Order::STATE_COMPLETE,
89+
'status' => 'complete',
90+
'grant_total' => 160.00
91+
]
92+
];
93+
94+
$actualData = $response['customerOrders']['items'];
95+
96+
foreach ($expectedData as $key => $data) {
97+
$this->assertEquals($data['increment_id'], $actualData[$key]['increment_id']);
98+
$this->assertEquals($data['grant_total'], $actualData[$key]['grant_total']);
99+
$this->assertEquals($data['state'], $actualData[$key]['state']);
100+
$this->assertEquals($data['status'], $actualData[$key]['status']);
101+
}
102+
}
103+
104+
/**
105+
* @param string $email
106+
* @param string $password
107+
* @return array
108+
* @throws \Magento\Framework\Exception\AuthenticationException
109+
*/
110+
private function getCustomerAuthHeaders(string $email, string $password): array
111+
{
112+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
113+
return ['Authorization' => 'Bearer ' . $customerToken];
114+
}
115+
}

dev/tests/integration/testsuite/Magento/Sales/_files/order_list_rollback.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
use Magento\Sales\Model\Order;
8-
97
require 'default_rollback.php';

0 commit comments

Comments
 (0)