How To

Magento 2: How to Pass Multi-Dimensional Array as an Argument in GraphQL

Hello Friends,

In this blog we will learn about How to Pass Multi-Dimensional Array as an Argument in GraphQL in Magento 2.

Magento 2’s GraphQL functionality offers a powerful way to interact with your store’s data. But what if you need to pass complex information, like a multi-dimensional array, as an argument in your GraphQL queries? One of the common challenges developers face is passing multi-dimensional arrays as arguments in GraphQL queries or mutations.

In this blog post, we’ll explore how to pass multi-dimensional arrays in GraphQL with Magento 2.

Steps to Pass Multi-Dimensional Array as an Argument in GraphQL in Magento 2:

Step 1: First, we need to create a schema.graphql file inside our extension at the following path

app\code\Vendor\Extension\etc\schema.graphqls

Then add the code as follows

input OrderInput {
  orderId: String!
  comment: String!
}

type Mutation {
    setFormData (
        orderarray: [OrderInput!]!
    ): setData @resolver(class: "Vendor\\Extension\\Model\\Resolver\\Setdata")
    
}

type setData {
    status: Boolean
    message: String
}

Step 2: Now, we need to create a Setdata.php file inside our extension at the following path

app\code\Vendor\Extension\Model\Resolver\Setdata.php

Then add the code as given below

<?php

namespace Vendor\Extension\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;

class Setdata implements ResolverInterface
{

   public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
    
       $data = [];
       $Ordervalue = $this->setOrderdata($args['orderarray']);
       $data = ["status"=>true, "message"=> "passes data successfully."];
       return [$data];
    
    }
    
    public function setOrderdata($orderarray){
      $requestData = json_decode(file_get_contents('php://input'), true);
        $Ordervalue = $requestData['orderarray'];
     return $Ordervalue;
    
    } 
}

You need to call above API as per below parameters.

{
    "orderarray": [
        {orderId: "1", comment: "test order"},
        {orderId: "2", comment: "demo order"}
    ]
}

Conclusion

By following these steps, you’ll be well-equipped to handle multi-dimensional arrays with confidence in your Magento 2 GraphQL queries.

Related Article – Magento 2: How to Pass Multi-Dimensional Array as an Argument in REST API

Share the solution with your friends and stay in touch with us for more.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Display Minimum Order Amount Message on Minicart in Magento 2?

Hello Magento Friends, In today’s blog, I will provide the steps for displaying the minimum…

2 days ago

How to Use Laravel Eloquent WHEREIN Query?

The Laravel Eloquent ORM provides a powerful way to interact with your database concisely and…

4 days ago

WooCommerce vs Shopify: A Detailed Comparison for 2024

WooCommerce vs Shopify: A Detailed Comparison for 2024 Choosing the right e-commerce platform is critical…

5 days ago

How to Create an Effective SEO Strategy

In today’s digital landscape, having a strong online presence is paramount for businesses and content…

5 days ago

Google Launches June 2024 Spam Update

Google's search results are constantly evolving, and a key part of that evolution is keeping…

5 days ago

How to Get Products Upto 250 Without Pagination in Shopify Remix App?

In this blog post, we'll show you how to get products upto 250 without pagination…

5 days ago