4

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.

Vishwas Bhatnagar
4,7193 gold badges39 silver badges61 bronze badges
asked Jan 22, 2020 at 13:09
12
  • do you get any idea about this issue... Commented Jan 23, 2020 at 5:07
  • Still not getting any idea. Commented Jan 23, 2020 at 6:12
  • 'dates' => $storeHoliday[0]['dates'] ---- here are you getting data...? Commented Jan 23, 2020 at 8:11
  • Yes, I can get data, you can see array in my question. Commented Jan 23, 2020 at 9:01
  • in query you set repetitive: Int as int but your returning a array to that... Commented Jan 23, 2020 at 9:03

2 Answers 2

6

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.

Chirag Patel
6,1662 gold badges26 silver badges66 bronze badges
answered Jan 23, 2020 at 13:14
1
  • It working like charm! You are a genius..... Actually I have defined dates: holidayDates is an array. after remove define holidayDates as a simple string it's working. Commented Jan 23, 2020 at 13:19
2

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
 (
 )
 )
answered Jan 23, 2020 at 9:20
8
  • Thanks for your time but What is the difference between [repetitive] and [repetitive_list] at the end, both are a return array and you define repetitive_list: int = Int but it's value return array. Commented Jan 23, 2020 at 9:36
  • you already defined correct query but again you need an extra array to print array value's on array --- not an Int value ... Commented Jan 23, 2020 at 9:38
  • between [repetitive] and [repetitive_list] ----- both are printing an array with array of array Commented Jan 23, 2020 at 9:39
  • The issue is in the same position. I would like to know what type are define for an index array return? If an array is associative so you can assign Int, String,Float etc.. to key like type holidayDetails {} Commented Jan 23, 2020 at 9:40
  • still not yet solved ah.. Commented Jan 23, 2020 at 9:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.