Skip to content

Commit

Permalink
Example of custom order field in graphql without reloadind the order
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheyUchukhlebau committed May 31, 2022
1 parent babf55a commit b9cc55d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Model/Resolver/OrderCustomField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
declare(strict_types=1);

namespace MageWorx\ExtendedCartGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class OrderCustomField implements \Magento\Framework\GraphQl\Query\ResolverInterface
{
/**
* @inheritDoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var \Magento\Sales\Api\Data\OrderInterface $order */
$order = $value['model'];

return (string)$order->getData('your_custom_field');
}
}
4 changes: 4 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ input SetPaymentMethodOnCartInput {
@resolver(class: "MageWorx\\ExtendedCartGraphQl\\Model\\Resolver\\CustomFieldOnPaymentMethod")
@doc(description: "Add custom field to payment method mutation and save it somwhere in DB")
}
type CustomerOrder {
my_custom_field: String
@resolver(class: "MageWorx\\ExtendedCartGraphQl\\Model\\Resolver\\OrderCustomField")
}

0 comments on commit b9cc55d

Please sign in to comment.