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!






