Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 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
76 changes: 76 additions & 0 deletions app/code/Magento/SalesGraphQl/Model/Resolver/Orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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;

/**
* Class Orders
*/
class Orders implements ResolverInterface
{
/**
* @var CollectionFactoryInterface
*/
private $collectionFactory;

/**
* @var CheckCustomerAccountInterface
*/
private $checkCustomerAccount;

/**
* Orders constructor.
* @param CollectionFactoryInterface $collectionFactory
* @param CheckCustomerAccountInterface $checkCustomerAccount
*/
public function __construct(
CollectionFactoryInterface $collectionFactory,
CheckCustomerAccountInterface $checkCustomerAccount
) {
$this->collectionFactory = $collectionFactory;
$this->checkCustomerAccount = $checkCustomerAccount;

}

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

$this->checkCustomerAccount->execute($customerId, $context->getUserType());

$items = [];
$orders = $this->collectionFactory->create($customerId);

/** @var \Magento\Sales\Model\Order $order */
foreach ($orders as $order) {
$items[] = [
'id' => $order->getId(),
'increment_id' => $order->getIncrementId(),
'created_at' => $order->getCreatedAt(),
'grand_total' => $order->getGrandTotal(),
'status' => $order->getStatus()
];
}

return ['items' => $items];
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/SalesGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SalesGraphQl

**SalesGraphQl** provides type and resolver information for the GraphQl module
to generate sales orders information.
27 changes: 27 additions & 0 deletions app/code/Magento/SalesGraphQl/composer.json
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\\": ""
}
}
}
10 changes: 10 additions & 0 deletions app/code/Magento/SalesGraphQl/etc/module.xml
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>
18 changes: 18 additions & 0 deletions app/code/Magento/SalesGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Query {
customerOrders: CustomerOrders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Orders") @doc(description: "List of customer orders")
}

type CustomerOrder @doc(description: "Order mapping fields") {
id: Int
increment_id: String
created_at: String
grand_total: Float
status: String
}

type CustomerOrders {
items: [CustomerOrder] @doc(description: "Array of orders")
}
10 changes: 10 additions & 0 deletions app/code/Magento/SalesGraphQl/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_SalesGraphQl', __DIR__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
"magento/module-rule": "*",
"magento/module-sales": "*",
"magento/module-sales-analytics": "*",
"magento/module-sales-graph-ql": "*",
"magento/module-sales-inventory": "*",
"magento/module-sales-rule": "*",
"magento/module-sales-sequence": "*",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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
grand_total
status
}
}
}
QUERY;

$currentEmail = '[email protected]';
$currentPassword = 'password';

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

$expectedData = [
[
'increment_id' => '100000002',
'status' => 'processing',
'grand_total' => 120.00
],
[
'increment_id' => '100000003',
'status' => 'processing',
'grand_total' => 130.00
],
[
'increment_id' => '100000004',
'status' => 'closed',
'grand_total' => 140.00
],
[
'increment_id' => '100000005',
'status' => 'complete',
'grand_total' => 150.00
],
[
'increment_id' => '100000006',
'status' => 'complete',
'grand_total' => 160.00
]
];

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

foreach ($expectedData as $key => $data) {
$this->assertEquals($data['increment_id'], $actualData[$key]['increment_id']);
$this->assertEquals($data['grand_total'], $actualData[$key]['grand_total']);
$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
Expand Up @@ -4,6 +4,4 @@
* See COPYING.txt for license details.
*/

use Magento\Sales\Model\Order;

require 'default_rollback.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Sales\Model\Order;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Address as OrderAddress;

require 'order.php';
/** @var Order $order */
/** @var Order\Payment $payment */
/** @var Order\Item $orderItem */
/** @var array $addressData Data for creating addresses for the orders. */
$orders = [
[
'increment_id' => '100000002',
'state' => \Magento\Sales\Model\Order::STATE_NEW,
'status' => 'processing',
'grand_total' => 120.00,
'subtotal' => 120.00,
'base_grand_total' => 120.00,
'store_id' => 1,
'website_id' => 1,
'payment' => $payment
],
[
'increment_id' => '100000003',
'state' => \Magento\Sales\Model\Order::STATE_PROCESSING,
'status' => 'processing',
'grand_total' => 130.00,
'base_grand_total' => 130.00,
'subtotal' => 130.00,
'store_id' => 0,
'website_id' => 0,
'payment' => $payment
],
[
'increment_id' => '100000004',
'state' => \Magento\Sales\Model\Order::STATE_PROCESSING,
'status' => 'closed',
'grand_total' => 140.00,
'base_grand_total' => 140.00,
'subtotal' => 140.00,
'store_id' => 1,
'website_id' => 1,
'payment' => $payment
],
[
'increment_id' => '100000005',
'state' => \Magento\Sales\Model\Order::STATE_COMPLETE,
'status' => 'complete',
'grand_total' => 150.00,
'base_grand_total' => 150.00,
'subtotal' => 150.00,
'store_id' => 1,
'website_id' => 1,
'payment' => $payment
],
[
'increment_id' => '100000006',
'state' => \Magento\Sales\Model\Order::STATE_COMPLETE,
'status' => 'complete',
'grand_total' => 160.00,
'base_grand_total' => 160.00,
'subtotal' => 160.00,
'store_id' => 1,
'website_id' => 1,
'payment' => $payment
],
];

/** @var OrderRepositoryInterface $orderRepository */
$orderRepository = $objectManager->create(OrderRepositoryInterface::class);
/** @var array $orderData */
foreach ($orders as $orderData) {
/** @var $order \Magento\Sales\Model\Order */
$order = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Sales\Model\Order::class
);

// Reset addresses
/** @var Order\Address $billingAddress */
$billingAddress = $objectManager->create(OrderAddress::class, ['data' => $addressData]);
$billingAddress->setAddressType('billing');

$shippingAddress = clone $billingAddress;
$shippingAddress->setId(null)->setAddressType('shipping');

$order
->setData($orderData)
->addItem($orderItem)
->setCustomerIsGuest(false)
->setCustomerId(1)
->setCustomerEmail('[email protected]')
->setBillingAddress($billingAddress)
->setShippingAddress($shippingAddress);

$orderRepository->save($order);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require 'default_rollback.php';