-
Notifications
You must be signed in to change notification settings - Fork 149
magento/graphql-ce#41: [Query] My Account > My Orders #212
Changes from 1 commit
ad85c95
584eb7d
4a31f4f
9043bc9
1cfa17f
0b22554
f0dedde
198fc61
a043177
abc43ac
0aad3fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\SalesGraphQl\Model\Resolver; | ||
|
|
||
| use Magento\Authorization\Model\UserContextInterface; | ||
| use Magento\Framework\GraphQl\Config\Element\Field; | ||
| use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
| use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
| use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface; | ||
| use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccountInterface; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| class Orders implements ResolverInterface | ||
| { | ||
| /** | ||
| * @var UserContextInterface | ||
| */ | ||
| private $userContext; | ||
|
|
||
| /** | ||
| * @var CollectionFactoryInterface | ||
| */ | ||
| private $collectionFactory; | ||
|
|
||
| /** | ||
| * @var CheckCustomerAccountInterface | ||
| */ | ||
| private $checkCustomerAccount; | ||
|
|
||
| /** | ||
| * Orders constructor. | ||
| * @param UserContextInterface $userContext | ||
| * @param CollectionFactoryInterface $collectionFactory | ||
| */ | ||
| public function __construct( | ||
| UserContextInterface $userContext, | ||
| CollectionFactoryInterface $collectionFactory, | ||
| CheckCustomerAccountInterface $checkCustomerAccount | ||
| ) { | ||
| $this->userContext = $userContext; | ||
| $this->collectionFactory = $collectionFactory; | ||
| $this->checkCustomerAccount = $checkCustomerAccount; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function resolve( | ||
| Field $field, | ||
| $context, | ||
| ResolveInfo $info, | ||
| array $value = null, | ||
| array $args = null | ||
| ) { | ||
|
|
||
| $customerId = $this->userContext->getUserId(); | ||
|
|
||
| $this->checkCustomerAccount->execute($customerId, $this->userContext->getUserType()); | ||
|
|
||
| $orders = $this->collectionFactory->create($customerId); | ||
| $items = []; | ||
|
|
||
| // @TODO Add shipping & billing address in response | ||
|
||
| // @TODO Add order currency object in response | ||
| /** @var \Magento\Sales\Model\Order $order */ | ||
| foreach ($orders as $order) { | ||
| $items[] = [ | ||
| 'id' => $order->getId(), | ||
| 'increment_id' => $order->getIncrementId(), | ||
| 'created_at' => $order->getCreatedAt(), | ||
| 'grant_total' => $order->getGrandTotal(), | ||
| 'state' => $order->getState(), | ||
| 'status' => $order->getStatus() | ||
| ]; | ||
| } | ||
|
|
||
| return ['items' => $items]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # SalesGraphQl | ||
|
|
||
| **SalesGraphQl** provides type and resolver information for the GraphQl module | ||
| to generate sales orders information endpoints. | ||
|
||
|
|
||
| Also will provides endpoints for modifying an order. | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "name": "magento/module-sales-graph-ql", | ||
| "description": "N/A", | ||
| "type": "magento2-module", | ||
| "require": { | ||
| "php": "~7.1.3||~7.2.0", | ||
| "magento/framework": "*", | ||
| "magento/module-customer": "*", | ||
| "magento/module-catalog": "*", | ||
| "magento/module-store": "*" | ||
| }, | ||
| "suggest": { | ||
| "magento/module-graph-ql": "*" | ||
| }, | ||
| "license": [ | ||
| "OSL-3.0", | ||
| "AFL-3.0" | ||
| ], | ||
| "autoload": { | ||
| "files": [ | ||
| "registration.php" | ||
| ], | ||
| "psr-4": { | ||
| "Magento\\SalesGraphQl\\": "" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?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_SalesGraphQl"/> | ||
| </config> |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||
| # Copyright © Magento, Inc. All rights reserved. | ||||
| # See COPYING.txt for license details. | ||||
|
|
||||
| type Query { | ||||
| customerOrders: Orders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Orders") @doc(description: "List of customer orders") | ||||
| } | ||||
|
|
||||
| type Order @doc(description: "Order mapping fields") { | ||||
|
||||
| id: Int | ||||
| increment_id: String | ||||
| created_at: String | ||||
| grant_total: Float | ||||
|
||||
| state: String | ||||
|
||||
| state: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for info. I changed already
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Orders => CustomerOrders
| 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_SalesGraphQl', __DIR__); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
||
| <!-- | ||
| /** | ||
| * PHPUnit configuration for GraphQL web API functional tests. | ||
| * | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| --> | ||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd" | ||
| colors="true" | ||
| columns="max" | ||
| beStrictAboutTestsThatDoNotTestAnything="false" | ||
| bootstrap="./framework/bootstrap.php" | ||
| > | ||
| <!-- Test suites definition --> | ||
| <testsuites> | ||
| <testsuite name="Magento GraphQL web API functional tests"> | ||
| <directory suffix="Test.php">testsuite/Magento/GraphQl</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <!-- PHP INI settings and constants definition --> | ||
| <php> | ||
| <includePath>./testsuite</includePath> | ||
| <const name="TESTS_INSTALL_CONFIG_FILE" value="config/install-config-mysql.php"/> | ||
| <const name="TESTS_GLOBAL_CONFIG_FILE" value="config/config-global.php"/> | ||
| <!-- Webserver URL --> | ||
| <const name="TESTS_BASE_URL" value="http://magento.inno"/> | ||
| <!-- Webserver API user --> | ||
| <const name="TESTS_WEBSERVICE_USER" value="admin"/> | ||
| <!-- Webserver API key --> | ||
| <const name="TESTS_WEBSERVICE_APIKEY" value="123123q"/> | ||
| <!-- Define if debugger should be started using XDEBUG_SESSION cookie --> | ||
| <const name="TESTS_XDEBUG_ENABLED" value="true"/> | ||
| <!-- Define XDEBUG_SESSION cookie value--> | ||
| <const name="TESTS_XDEBUG_SESSION" value="MEETMAGENTO" /> | ||
|
|
||
| <ini name="date.timezone" value="America/Los_Angeles"/> | ||
|
|
||
| <!-- Semicolon-separated 'glob' patterns, that match global XML configuration files --> | ||
| <const name="TESTS_GLOBAL_CONFIG_DIR" value="../../../app/etc"/> | ||
| <!-- Whether to cleanup the application before running tests or not --> | ||
| <const name="TESTS_CLEANUP" value="enabled"/> | ||
| <!--Defines if Magento should be installed before tests execution--> | ||
| <const name="TESTS_MAGENTO_INSTALLATION" value="disabled"/> | ||
| <!-- Magento mode for tests execution. Possible values are "default", "developer" and "production". --> | ||
| <const name="TESTS_MAGENTO_MODE" value="default"/> | ||
| </php> | ||
|
|
||
| <!-- Test listeners --> | ||
| <listeners> | ||
| <listener class="Magento\TestFramework\Event\PhpUnit"/> | ||
| </listeners> | ||
| </phpunit> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\GraphQl\Sales; | ||
|
|
||
| use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
| use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
| use Magento\TestFramework\Helper\Bootstrap; | ||
|
|
||
| /** | ||
| * Class OrdersTest | ||
| */ | ||
| class OrdersTest extends GraphQlAbstract | ||
| { | ||
| /** | ||
| * @var CustomerTokenServiceInterface | ||
| */ | ||
| private $customerTokenService; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function setUp() | ||
| { | ||
| parent::setUp(); | ||
| $objectManager = Bootstrap::getObjectManager(); | ||
| $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); | ||
| } | ||
|
|
||
| /** | ||
| * @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
| * @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php | ||
| */ | ||
| public function testOrdersQuery() | ||
| { | ||
| $query = | ||
| <<<QUERY | ||
| query { | ||
| customerOrders { | ||
| items { | ||
| id | ||
|
||
| increment_id | ||
| created_at | ||
| grant_total | ||
| state | ||
| status | ||
| } | ||
| } | ||
| } | ||
| QUERY; | ||
|
|
||
| $currentEmail = '[email protected]'; | ||
| $currentPassword = 'password'; | ||
|
|
||
| $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); | ||
|
|
||
| $expectedData = [ | ||
| [ | ||
| 'increment_id' => '100000002', | ||
| 'state' => \Magento\Sales\Model\Order::STATE_NEW, | ||
| 'status' => 'processing', | ||
| 'grant_total' => 120.00 | ||
| ], | ||
| [ | ||
| 'increment_id' => '100000003', | ||
| 'state' => \Magento\Sales\Model\Order::STATE_PROCESSING, | ||
| 'status' => 'processing', | ||
| 'grant_total' => 130.00 | ||
| ], | ||
| [ | ||
| 'increment_id' => '100000004', | ||
| 'state' => \Magento\Sales\Model\Order::STATE_PROCESSING, | ||
| 'status' => 'closed', | ||
| 'grant_total' => 140.00 | ||
| ], | ||
| [ | ||
| 'increment_id' => '100000005', | ||
| 'state' => \Magento\Sales\Model\Order::STATE_COMPLETE, | ||
| 'status' => 'complete', | ||
| 'grant_total' => 150.00 | ||
| ], | ||
| [ | ||
| 'increment_id' => '100000006', | ||
| 'state' => \Magento\Sales\Model\Order::STATE_COMPLETE, | ||
| 'status' => 'complete', | ||
| 'grant_total' => 160.00 | ||
| ] | ||
| ]; | ||
|
|
||
| $actualData = $response['customerOrders']['items']; | ||
|
|
||
| foreach ($expectedData as $key => $data) { | ||
| $this->assertEquals($data['increment_id'], $actualData[$key]['increment_id']); | ||
| $this->assertEquals($data['grant_total'], $actualData[$key]['grant_total']); | ||
| $this->assertEquals($data['state'], $actualData[$key]['state']); | ||
| $this->assertEquals($data['status'], $actualData[$key]['status']); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @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]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,4 @@ | |
| * See COPYING.txt for license details. | ||
| */ | ||
|
|
||
| use Magento\Sales\Model\Order; | ||
|
|
||
| require 'default_rollback.php'; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency purposes it is better to get info about current user from the
$contextobject, it is done this way in all other resolvers.