Skip to content

Commit 94dd6d2

Browse files
author
Valeriy Naida
authored
ENGCOM-3441: magento/graphql-ce#41: [Query] My Account > My Orders #212
2 parents b6ec026 + 42ce3e5 commit 94dd6d2

File tree

12 files changed

+354
-3
lines changed

12 files changed

+354
-3
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
14+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
15+
16+
/**
17+
* Orders data reslover
18+
*/
19+
class Orders implements ResolverInterface
20+
{
21+
/**
22+
* @var CollectionFactoryInterface
23+
*/
24+
private $collectionFactory;
25+
26+
/**
27+
* @var CheckCustomerAccount
28+
*/
29+
private $checkCustomerAccount;
30+
31+
/**
32+
* @param CollectionFactoryInterface $collectionFactory
33+
* @param CheckCustomerAccount $checkCustomerAccount
34+
*/
35+
public function __construct(
36+
CollectionFactoryInterface $collectionFactory,
37+
CheckCustomerAccount $checkCustomerAccount
38+
) {
39+
$this->collectionFactory = $collectionFactory;
40+
$this->checkCustomerAccount = $checkCustomerAccount;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function resolve(
47+
Field $field,
48+
$context,
49+
ResolveInfo $info,
50+
array $value = null,
51+
array $args = null
52+
) {
53+
$customerId = $context->getUserId();
54+
$this->checkCustomerAccount->execute($customerId, $context->getUserType());
55+
56+
$items = [];
57+
$orders = $this->collectionFactory->create($customerId);
58+
59+
/** @var \Magento\Sales\Model\Order $order */
60+
foreach ($orders as $order) {
61+
$items[] = [
62+
'id' => $order->getId(),
63+
'increment_id' => $order->getIncrementId(),
64+
'created_at' => $order->getCreatedAt(),
65+
'grand_total' => $order->getGrandTotal(),
66+
'status' => $order->getStatus(),
67+
];
68+
}
69+
return ['items' => $items];
70+
}
71+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SalesGraphQl
2+
3+
**SalesGraphQl** provides type and resolver information for the GraphQl module
4+
to generate sales orders information.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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-graph-ql": "*",
9+
"magento/module-sales": "*"
10+
},
11+
"suggest": {
12+
"magento/module-graph-ql": "*"
13+
},
14+
"license": [
15+
"OSL-3.0",
16+
"AFL-3.0"
17+
],
18+
"autoload": {
19+
"files": [
20+
"registration.php"
21+
],
22+
"psr-4": {
23+
"Magento\\SalesGraphQl\\": ""
24+
}
25+
}
26+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
customerOrders: CustomerOrders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Orders") @doc(description: "List of customer orders")
6+
}
7+
8+
type CustomerOrder @doc(description: "Order mapping fields") {
9+
id: Int
10+
increment_id: String
11+
created_at: String
12+
grand_total: Float
13+
status: String
14+
}
15+
16+
type CustomerOrders {
17+
items: [CustomerOrder] @doc(description: "Array of orders")
18+
}
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__);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
"magento/module-rule": "*",
202202
"magento/module-sales": "*",
203203
"magento/module-sales-analytics": "*",
204+
"magento/module-sales-graph-ql": "*",
204205
"magento/module-sales-inventory": "*",
205206
"magento/module-sales-rule": "*",
206207
"magento/module-sales-sequence": "*",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
protected function setUp()
25+
{
26+
parent::setUp();
27+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
28+
}
29+
30+
/**
31+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
32+
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
33+
*/
34+
public function testOrdersQuery()
35+
{
36+
$query =
37+
<<<QUERY
38+
query {
39+
customerOrders {
40+
items {
41+
id
42+
increment_id
43+
created_at
44+
grand_total
45+
status
46+
}
47+
}
48+
}
49+
QUERY;
50+
51+
$currentEmail = 'customer@example.com';
52+
$currentPassword = 'password';
53+
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
54+
55+
$expectedData = [
56+
[
57+
'increment_id' => '100000002',
58+
'status' => 'processing',
59+
'grand_total' => 120.00
60+
],
61+
[
62+
'increment_id' => '100000003',
63+
'status' => 'processing',
64+
'grand_total' => 130.00
65+
],
66+
[
67+
'increment_id' => '100000004',
68+
'status' => 'closed',
69+
'grand_total' => 140.00
70+
],
71+
[
72+
'increment_id' => '100000005',
73+
'status' => 'complete',
74+
'grand_total' => 150.00
75+
],
76+
[
77+
'increment_id' => '100000006',
78+
'status' => 'complete',
79+
'grand_total' => 160.00
80+
]
81+
];
82+
83+
$actualData = $response['customerOrders']['items'];
84+
85+
foreach ($expectedData as $key => $data) {
86+
$this->assertEquals($data['increment_id'], $actualData[$key]['increment_id']);
87+
$this->assertEquals($data['grand_total'], $actualData[$key]['grand_total']);
88+
$this->assertEquals($data['status'], $actualData[$key]['status']);
89+
}
90+
}
91+
92+
/**
93+
* @param string $email
94+
* @param string $password
95+
* @return array
96+
* @throws \Magento\Framework\Exception\AuthenticationException
97+
*/
98+
private function getCustomerAuthHeaders(string $email, string $password): array
99+
{
100+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
101+
return ['Authorization' => 'Bearer ' . $customerToken];
102+
}
103+
}

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)