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.
-
do you get any idea about this issue...prabhakaran7– prabhakaran72020年01月23日 05:07:30 +00:00Commented Jan 23, 2020 at 5:07
-
Still not getting any idea.Chirag Patel– Chirag Patel2020年01月23日 06:12:16 +00:00Commented Jan 23, 2020 at 6:12
-
'dates' => $storeHoliday[0]['dates'] ---- here are you getting data...?prabhakaran7– prabhakaran72020年01月23日 08:11:57 +00:00Commented Jan 23, 2020 at 8:11
-
Yes, I can get data, you can see array in my question.Chirag Patel– Chirag Patel2020年01月23日 09:01:45 +00:00Commented Jan 23, 2020 at 9:01
-
in query you set repetitive: Int as int but your returning a array to that...prabhakaran7– prabhakaran72020年01月23日 09:03:44 +00:00Commented Jan 23, 2020 at 9:03
2 Answers 2
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.
-
It working like charm! You are a genius..... Actually I have defined
dates: holidayDatesis an array. after remove defineholidayDatesas a simple string it's working.Chirag Patel– Chirag Patel2020年01月23日 13:19:47 +00:00Commented Jan 23, 2020 at 13:19
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
(
)
)
-
Thanks for your time but What is the difference between
[repetitive]and[repetitive_list]at the end, both are a return array and you definerepetitive_list: int= Int but it's value return array.Chirag Patel– Chirag Patel2020年01月23日 09:36:26 +00:00Commented 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 ...prabhakaran7– prabhakaran72020年01月23日 09:38:44 +00:00Commented Jan 23, 2020 at 9:38
-
between [repetitive] and [repetitive_list] ----- both are printing an array with array of arrayprabhakaran7– prabhakaran72020年01月23日 09:39:56 +00:00Commented 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 {}Chirag Patel– Chirag Patel2020年01月23日 09:40:27 +00:00Commented Jan 23, 2020 at 9:40 -
still not yet solved ah..prabhakaran7– prabhakaran72020年01月23日 09:43:18 +00:00Commented Jan 23, 2020 at 9:43