Magento 2 How to Create Custom Graphql

Hello Guys! 👋 

In this blog, we learn how to create custom Graphql in Magento 2 and also when to use type query, and when to use type mutation.

Graphql is a query language that is developed by Facebook in 2012 but it was publicly released in 2015.

Type query is used to get data or retrieve data from the Magento server

While mutations is used for changes in data. Mutations can create, update and delete the data.

The following example is for creating graphql with query and mutations.

Step1 : Create schema.graphqls in following file path.

app\code\Vendor\Extension\etc

type Mutation {
    setCustomData(input: customInput!): customOutput @resolver(class: "\\Vendor\\Extension\\Model\\Resolver\\CreateCustomData")
}
type Query {
    getCustomData(id: Int!): customOutput @resolver(class: "\\Vendor\\Extension\\Model\\Resolver\\GetCustomData")
}
input customInput {
    name: String
}
type customOutput {
    id: Int
    name: String
}
Step 2: Add CreateCustomData.php in following file path

app\code\Vendor\Extension\Model\Resolver

<?php

declare(strict_types=1);

namespace Vendor\Extension\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class CreateCustomData implements ResolverInterface
{
    protected $customFactory;

    public function __construct(
        \Vendor\Extension\Model\CustomFactory $customFactory
    )
    {
        $this->customFactory = $customFactory;
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
              $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $model = $this->customFactory->create();
       
        if(isset($args['input'])) {
            $model->setData($args['input']);
            $model->save();
        }

        return $model->getData();
    }
}

Step 3: Now create GetCustomData.php file in following file path

app\code\Vendor\Extension\Model\Resolver

<?php

declare(strict_types=1);

namespace Vendor\Extension\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class GetCustomData implements ResolverInterface
{
    protected $customFactory;

    public function __construct(
        \Vendor\Extension\Model\CustomFactory $customFactory
    )
    {
        $this->customFactory = $customFactory;
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
              $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $model = $this->customFactory->create();
        $collection = $model->getCollection();
        
        $data = [];
        foreach($collection as $item){
            $data[] = [
                'id' => $item->getId(),
                'name' => $item->getName()
            ];
        }
        
        return $data;
        
    }
}

Hope! It will help you

Thank you 😊

How useful was this post?

Click on a star to rate it!

Average rating 4.6 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *