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 1 commit
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
87 changes: 87 additions & 0 deletions app/code/Magento/SalesGraphQl/Model/Resolver/Orders.php
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());
Copy link
Contributor

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 $context object, it is done this way in all other resolvers.


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

// @TODO Add shipping & billing address in response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be addressed or moved to new issues before we can merge this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those todo's can be removed for now, i will come with updates in order to add more info on orders , if that's ok

// @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];
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/SalesGraphQl/README.md
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endpoints => queries
endpoints is REST terminology,
queries and mutations is used in GraphQL


Also will provides endpoints for modifying an order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endpoints for modifying an order => order mutations

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>
19 changes: 19 additions & 0 deletions app/code/Magento/SalesGraphQl/etc/schema.graphqls
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") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order => CustomerOrder

id: Int
increment_id: String
created_at: String
grant_total: Float
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grant_total => grand_total

state: String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to expose state on the storefront?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will tend to provide a lot of info on the order, so the query can be made to extract just you need.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

State is an internal concept, on storefront we normally expose only Status.

Suggested change
state: String

Copy link
Contributor Author

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

status: String
}

type Orders {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orders => CustomerOrders

items: [Order] @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__);
56 changes: 56 additions & 0 deletions dev/tests/api-functional/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this file was committed by accident. Please remove.

<!--
/**
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct indentation.

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
Expand Up @@ -4,6 +4,4 @@
* See COPYING.txt for license details.
*/

use Magento\Sales\Model\Order;

require 'default_rollback.php';
Loading