GraphQl : Mutation

Mutation input
mutation addnewOrder{
 addOrder(
   input:{
     email:"xxx@gmail.com"
     customer_id:1
     store_id:1
     currency_code:"GBP" 
     order_subtotal: 3
     order_grandtotal: 3
     ship_method : "flatrate_flatrate"
     shipping_amount : 10 
     ship_description : "flatrate_flatrate"
     payment_method : "checkmo" 
     order_qty : 3 
     billing_address :  {firstname:"bfname",country_id:"IN",lastname:"blname",city:"bcity",company:"bcomp",street:"bcomp",region:"bcomp",postcode:"bcomp",telephone:"1234567890",save_in_address_book:false} 
     shipping_address : {firstname:"bfname",country_id:"IN",lastname:"blname",city:"bcity",company:"bcomp",street:"bcomp",region:"bcomp",postcode:"bcomp",telephone:"1234567890",save_in_address_book:false} 
     items : [{product_id:2,qty:2,price:10,name:"test",sku:"test"},{product_id:1,qty:1,price:10,name:"test",sku:"test"}]
   }
 ){
   order_id
 }
}
Mutation Return Order Id
Mutation output
{
  "data": {
    "addOrder": {
      "order_id": 100000001,
    }
  }
}

Define the GraphQL schema for a module.A GraphQL module’s schema.graphqls file defines how the attributes defined in the module can be used in a GraphQL query.
The base schema.graphqls file, located in the  <module_name>/etc directory

type Mutation { 
    addOrder( 
        input: OrderInput 
    ): OrderOutput @resolver( class: "NameSpace\\ModuleName\\Model\\Resolver\\Order\\Create")@doc(description: "Create categories") 
 
}

input OrderInput { 
    email: String! 
    customer_id: Int! 
    store_id: Int!
    currency_code: String!
    order_subtotal: Int! 
    order_grandtotal: Int!
    ship_method: String!
    ship_description: String!
    payment_method: String!
    shipping_amount: Int!
    order_qty : Int!
    billing_address: BillingInput
    shipping_address: ShippingInput
    items: [ItemInput]
} 

input ItemInput {
 product_id: Int!
 qty: Int!
 price: Int! 
 name: String!
 sku: String!
}
 
input ShippingInput {
    firstname: String! 
    lastname: String!
    lastname: String!
    city : String! 
    company : String!
    street : String!
    region : String!
    country_id : String!
    postcode : String!
    telephone : String!
    save_in_address_book : Boolean! 
}

input BillingInput {
    firstname: String! 
    lastname: String!
    lastname: String!
    city : String! 
    company : String!
    country_id : String!
    street : String!
    region : String!
    postcode : String!
    telephone : String!
    save_in_address_book : Boolean!
}

type OrderOutput @doc(description:"Response after adding order"){ 
    order_id: Int  @doc(description:"Id of the order just created") 
    url_path: String  @doc(description:"URL Path") 
    path: String @doc(description:"Path") 
} 

The typeResolver parameter specifies the path to the Resolver object, which interprets the GraphQL query.
declare(strict_types=1); 
 
namespace NameSpace\ModuleName\Model\Resolver\Order; 
 
use Magento\Framework\GraphQl\Config\Element\Field; 
use Magento\Framework\GraphQl\Exception\GraphQlInputException; 
use Magento\Framework\GraphQl\Query\ResolverInterface; 
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; 
use NameSpace\ModuleName\Model\Order\Create as Order; 
 
class Create implements ResolverInterface 
{ 
    
    private $order;
    
    public function __construct(Order $order) 
    { 
        $this->order = $order; 
    }
    
    
    public function resolve( 
        Field $field, 
        $context, 
        ResolveInfo $info, 
        array $value = null, 
        array $args = null 
    ) {
        
        if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) { 
            throw new GraphQlInputException(__('"input" value should be specified')); 
        } 

        $result = $this->order->setOrderData($args['input'])->execute();
        
        return $result;
       
    } 
} 


Comments