Question

I would like to create custom GraphQL for a custom module. but I am facing an issue. see below GraphQL response. graphql-response

In the above image, you can see I am able to get the response of days and **details** value but unable to get the value of dates it will return always null but actually value is present.

Below is my schema.graphql

type Query {
    getInformation(id: String!): StoreHolidayInfo @resolver (class: "\\Namespace\\Modulename\\Model\\Resolver\\StoreHolidayInfo") @doc(description:"Returns holiday information about store")
}
type StoreHolidayInfo {
   dates: [holidayDates]
   days: String
   details: [holidayDetails]
}
type holidayDates {
   repetitive: Int
   normal: Int
   shipping_off_day: Int
}

type holidayDetails {
   holiday_id: Int
   holiday_name: String
   holiday_applied_stores: String
   holiday_date_from: String
   holiday_date_to: String
   holiday_comment: String
   is_repetitive: Int
   is_active: Int
   all_store: Int
}

Below is my Resolver class.

<?php
declare(strict_types=1);

namespace Namespace\Modulename\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Namespace\Modulename\Api\StoreInformationManagementInterface;

/**
 * Class StoreHolidayInfo
 * @package Namespace\Modulename\Model\Resolver
 */
class StoreHolidayInfo implements ResolverInterface
{
    /**
     * @var StoreInformationManagementInterface
     */
    protected $storeInformationManagement;

    /**
     * StoreHolidayInfo constructor.
     * @param StoreInformationManagementInterface $storeInformationManagement
     */
    public function __construct(
        StoreInformationManagementInterface $storeInformationManagement
    ) {
        $this->storeInformationManagement = $storeInformationManagement;
    }

    /**
     * @inheritdoc
     */
    public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
    {
        if (!isset($args['id']) || empty($args['id'])) {
            throw new GraphQlInputException(__('"id" argument should be specified and not empty'));
        }

        $storeHoliday = $this->storeInformationManagement->getStoreHolidayInformation($args['storelocator_id']);

        return [
            'dates' => $storeHoliday[0]['dates'],
            'days' => $storeHoliday[0]['days'],
            'details' => $storeHoliday[0]['details']
        ];
    }
}

If I print the $storeHoliday object in Resolver file I am getting the below response.

Array
(
    [0] => Array
        (
            [dates] => Array
                (
                    [repetitive] => Array
                        (
                            [0] => 1-14
                            [1] => 1-15
                            [2] => 1-14
                            [3] => 1-15
                            [4] => 1-26
                        )

                    [normal] => Array
                        (
                            [0] => 01-14-2020
                            [1] => 01-15-2020
                            [2] => 01-26-2020
                        )

                    [shipping_off_day] => Array
                        (
                        )

                )

            [days] => 1
            [details] => Array
                (
                    [0] => Array
                        (
                            [holiday_id] => 1
                            [holiday_name] => Kites festival
                            [holiday_applied_stores] => 2,3,4
                            [holiday_date_from] => 2020-01-14
                            [holiday_date_to] => 2020-01-15
                            [holiday_comment] => <p>Kites festival</p>
                            [is_repetitive] => 1
                            [is_active] => 1
                            [all_store] => 1
                        )

                    [1] => Array
                        (
                            [holiday_id] => 2
                            [holiday_name] => Republic day
                            [holiday_applied_stores] => 2,3,4
                            [holiday_date_from] => 2020-01-26
                            [holiday_date_to] => 2020-01-26
                            [holiday_comment] => <p>Republic day</p>
                            [is_repetitive] => 1
                            [is_active] => 1
                            [all_store] => 0
                        )

                )

        )

)

So, in short, I am unable to get the value of dates. what's going wrong in the above code. can anybody help me to solve this issue?

Any help would be appreciated! Thanks.

Était-ce utile?

La solution

Try with below code.

type StoreHolidayInfo {
    dates: holidayDates,
    days: String,
    details: [holidayDetails]
}

type holidayDates {
    repetitive: [String] @doc(description: "holiday id"),
    normal: [String] @doc(description: "holiday id"),
    shipping_off_day: [String] @doc(description: "holiday id")
}

type holidayDetails {
    holiday_id: Int @doc(description: "holiday id"),
    holiday_name: String @doc(description: "holiday name"),
    holiday_applied_stores: String @doc(description: "holiday applied stores"),
    holiday_date_from: String @doc(description: "holiday date from"),
    holiday_date_to: String @doc(description: "holiday date to"),
    holiday_comment: String @doc(description: "holiday comment"),
    is_repetitive: Int @doc(description: "yearly repitive"),
    is_active: Int @doc(description: "is active holiday"),
    all_store: Int @doc(description: "all store"),
}

And add change below code in resolver.

return [
        'dates' => array(
            'repetitive' => $storeHoliday[0]['dates']['repetitive'],
            'normal' => $storeHoliday[0]['dates']['normal'],
            'shipping_off_day' => $storeHoliday[0]['dates']['shipping_off_day']
        ),
        'days' => $storeHoliday[0]['days'],
        'details' => $storeHoliday[0]['details']
    ];

It should work then.

Autres conseils

i'm not sure but just try out

you will get idea

type StoreHolidayInfo {
   dates: [holidayDates]
   days: String
   details: [holidayDetails]
}


type holidayDates {
   repetitive: [repetitive_list]  
   normal: Int
   shipping_off_day: Int
}

type repetitive_list{
repetitive_list:int
}

i just gave for one element

just try then change to all

and also you need just change in your

array print [repetitive][repetitive_list]

[dates] => Array
                (
                    [repetitive] => Array
                        (
       [repetitive_list]=>Array(

                            [0] => 1-14
                            [1] => 1-15
                            [2] => 1-14
                            [3] => 1-15
                            [4] => 1-26
                        )
    )

                    [normal] => Array
                        (
                            [0] => 01-14-2020
                            [1] => 01-15-2020
                            [2] => 01-26-2020
                        )

                    [shipping_off_day] => Array
                        (
                        )

                )
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top