I would like to create custom GraphQL for a custom module. but I am facing an issue. see below 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 the 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 it. what's going wrong in the above code. can anybody help me to solve this issue?
Solved! Go to Solution.
Actually I have defined dates: holidayDates is an array. after define dates: holidayDates as a string in the schema.graphql reason for this I am getting a null response. and also some change response in Resolver.
Below is my updated schema.graphql
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 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'] ];
I would like Thanks Yash Shah
Actually I have defined dates: holidayDates is an array. after define dates: holidayDates as a string in the schema.graphql reason for this I am getting a null response. and also some change response in Resolver.
Below is my updated schema.graphql
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 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'] ];
I would like Thanks Yash Shah