Skip to content

Commit

Permalink
Merge pull request magento#3751 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Feb 14, 2019
2 parents b35ded1 + 7023a5e commit d1ce6a4
Show file tree
Hide file tree
Showing 19 changed files with 946 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,36 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;

/**
* Extract data from cart
*/
class ExtractDataFromCart
{
/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedQuoteId;

/**
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
*/
public function __construct(
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
) {
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
}

/**
* Extract data from cart
*
* @param Quote $cart
* @return array
* @throws NoSuchEntityException
*/
public function execute(Quote $cart): array
{
Expand All @@ -43,6 +60,7 @@ public function execute(Quote $cart): array
$appliedCoupon = $cart->getCouponCode();

return [
'cart_id' => $this->quoteIdToMaskedQuoteId->execute((int)$cart->getId()),
'items' => $items,
'applied_coupon' => $appliedCoupon ? ['code' => $appliedCoupon] : null
];
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/QuoteGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<arguments>
<argument name="supportedTypes" xsi:type="array">
<item name="simple" xsi:type="string">SimpleCartItem</item>
<item name="virtual" xsi:type="string">VirtualCartItem</item>
</argument>
</arguments>
</type>
Expand Down
19 changes: 19 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Mutation {
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
addVirtualProductsToCart(input: AddVirtualProductsToCartInput): AddVirtualProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
}

input SetShippingAddressesOnCartInput {
Expand Down Expand Up @@ -168,11 +169,21 @@ input AddSimpleProductsToCartInput {
cartItems: [SimpleProductCartItemInput!]!
}

input AddVirtualProductsToCartInput {
cart_id: String!
cartItems: [VirtualProductCartItemInput!]!
}

input SimpleProductCartItemInput {
data: CartItemInput!
customizable_options:[CustomizableOptionInput!]
}

input VirtualProductCartItemInput {
data: CartItemInput!
customizable_options:[CustomizableOptionInput!]
}

input CustomizableOptionInput {
id: Int!
value: String!
Expand All @@ -182,10 +193,18 @@ type AddSimpleProductsToCartOutput {
cart: Cart!
}

type AddVirtualProductsToCartOutput {
cart: Cart!
}

type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") {
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
}

type VirtualCartItem implements CartItemInterface @doc(description: "Virtual Cart Item") {
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
}

input CartItemInput {
sku: String!
qty: Float!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\VaultGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Vault\Api\PaymentTokenManagementInterface;
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;

/**
* Delete Payment Token resolver, used for GraphQL mutation processing.
*/
class DeletePaymentToken implements ResolverInterface
{
/**
* @var CheckCustomerAccount
*/
private $checkCustomerAccount;

/**
* @var PaymentTokenManagementInterface
*/
private $paymentTokenManagement;

/**
* @var PaymentTokenRepositoryInterface
*/
private $paymentTokenRepository;

/**
* @param CheckCustomerAccount $checkCustomerAccount
* @param PaymentTokenManagementInterface $paymentTokenManagement
* @param PaymentTokenRepositoryInterface $paymentTokenRepository
*/
public function __construct(
CheckCustomerAccount $checkCustomerAccount,
PaymentTokenManagementInterface $paymentTokenManagement,
PaymentTokenRepositoryInterface $paymentTokenRepository
) {
$this->checkCustomerAccount = $checkCustomerAccount;
$this->paymentTokenManagement = $paymentTokenManagement;
$this->paymentTokenRepository = $paymentTokenRepository;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($args['public_hash'])) {
throw new GraphQlInputException(__('Specify the "public_hash" value.'));
}

$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();

$this->checkCustomerAccount->execute($currentUserId, $currentUserType);

$token = $this->paymentTokenManagement->getByPublicHash($args['public_hash'], $currentUserId);
if (!$token) {
throw new GraphQlNoSuchEntityException(
__('Could not find a token using public hash: %1', $args['public_hash'])
);
}

return ['result' => $this->paymentTokenRepository->delete($token)];
}
}
72 changes: 72 additions & 0 deletions app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\VaultGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Vault\Model\PaymentTokenManagement;
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;

/**
* Customers Payment Tokens resolver, used for GraphQL request processing.
*/
class PaymentTokens implements ResolverInterface
{
/**
* @var PaymentTokenManagement
*/
private $paymentTokenManagement;

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

/**
* @param PaymentTokenManagement $paymentTokenManagement
* @param CheckCustomerAccount $checkCustomerAccount
*/
public function __construct(
PaymentTokenManagement $paymentTokenManagement,
CheckCustomerAccount $checkCustomerAccount
) {
$this->paymentTokenManagement = $paymentTokenManagement;
$this->checkCustomerAccount = $checkCustomerAccount;
}

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

$this->checkCustomerAccount->execute($currentUserId, $currentUserType);

$tokens = $this->paymentTokenManagement->getVisibleAvailableTokens($currentUserId);
$result = [];

foreach ($tokens as $token) {
$result[] = [
'public_hash' => $token->getPublicHash(),
'payment_method_code' => $token->getPaymentMethodCode(),
'type' => $token->getType(),
'details' => $token->getTokenDetails(),
];
}

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

**VaultGraphQl** provides type and resolver information for the GraphQl module
to generate Vault (stored payment information) information endpoints. This module also
provides mutations for modifying a payment token.
23 changes: 23 additions & 0 deletions app/code/Magento/VaultGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "magento/module-vault-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-vault": "*",
"magento/module-customer-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\VaultGraphQl\\": ""
}
}
}
10 changes: 10 additions & 0 deletions app/code/Magento/VaultGraphQl/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_VaultGraphQl"/>
</config>
31 changes: 31 additions & 0 deletions app/code/Magento/VaultGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Mutation {
deletePaymentToken(public_hash: String!): DeletePaymentTokenOutput @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\DeletePaymentToken") @doc(description:"Delete a customer payment token")
}

type DeletePaymentTokenOutput {
result: Boolean!
customerPaymentTokens: CustomerPaymentTokens @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\PaymentTokens")
}

type Query {
customerPaymentTokens: CustomerPaymentTokens @doc(description: "Return a list of customer payment tokens") @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\PaymentTokens")
}

type CustomerPaymentTokens @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\PaymentTokens") {
items: [PaymentToken]! @doc(description: "An array of payment tokens")
}

type PaymentToken @doc(description: "The stored payment method available to the customer") {
public_hash: String! @doc(description: "The public hash of the token")
payment_method_code: String! @doc(description: "The payment method code associated with the token")
type: PaymentTokenTypeEnum!
details: String @doc(description: "Stored account details")
}

enum PaymentTokenTypeEnum @doc(description: "The list of available payment token types") {
card
account
}
10 changes: 10 additions & 0 deletions app/code/Magento/VaultGraphQl/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_VaultGraphQl', __DIR__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
"magento/module-usps": "*",
"magento/module-variable": "*",
"magento/module-vault": "*",
"magento/module-vault-graph-ql": "*",
"magento/module-version": "*",
"magento/module-webapi": "*",
"magento/module-webapi-async": "*",
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.

Loading

0 comments on commit d1ce6a4

Please sign in to comment.