-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntitiesType.php
97 lines (89 loc) · 4.41 KB
/
EntitiesType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace AlanKent\GraphQL\Types;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\NonNull;
/**
* Object type for querying the avaialble entities (*RepositoryInterface service contracts).
*/
class EntitiesType extends ObjectType
{
/**
* @param \AlanKent\GraphQL\Types\ProductTypeFactory $productTypeFactory
*/
public function __construct(\AlanKent\GraphQL\Types\ProductTypeFactory $productTypeFactory)
{
$config = [
'name' => 'Entities',
'description' => 'All queriable entities exposed via GraphQL.',
'fields' => [
'product' => [
'type' => $productTypeFactory->create(),
'description' => 'Retrieve product for the specified SKU or id.',
'args' => [
// TODO: One recommendation was to always have one input arg with an InputType, not multiple args like is done here.
'sku' => [
'type' => Type::string(),
'description' => 'The SKU of the product to search for. (Supply SKU or id.)'
],
'id' => [
'type' => Type::int(),
'description' => 'The id of the product to search for. (Supply SKU or id.)'
],
'storeId' => [
'type' => Type::int(),
'description' => 'The store view id, or omitted for the default store view.',
//'defaultValue' => 0 // Default should be null? Is null same as zero?
]
],
'resolve' => function($val, $args, $context, $info) {
// Lazy load at runtime.
$sc = $context->getServiceContract(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$storeId = isset($args['storeId']) ? $args['storeId'] : null;
if (isset($args['id']) && isset($args['sku'])) {
throw new \Exception('Specify either "sku" or "id", not both.');
}
if (isset($args['id'])) {
$val = $sc->getById($args['id'], false, $storeId);
if (!($val instanceof \Magento\Catalog\Api\Data\ProductInterface)) { var_dump($val); throw new \Exception("Sky is falling"); }
return $val;
}
if (isset($args['sku'])) {
return $sc->get($args['sku'], false, $storeId);
}
throw new \Exception('You must specify either "sku" or "id".');
}
],
'category' => [
'type' => new CategoryType(),
'description' => 'Retrieve category for the specified id.',
'args' => [
'id' => [
'type' => new NonNull(Type::int()),
'description' => 'The id of the category to return.'
],
//'storeId' => [
//'type' => Type::int(),
//'description' => 'The store view id, or omitted for the default store view.',
////TODO: 'defaultValue' => 0 // Default should be null? Is null same as zero?
//]
],
'resolve' => function($val, $args, $context, $info) {
// Lazy load at runtime.
$sc = $context->getServiceContract(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
//$storeId = isset($args['storeId']) ? $args['storeId'] : null;
return $sc->get($args['id']/*, false, $storeId*/);
}
],
],
//'resolveField' => function($val, $args, $context, ResolveInfo $info) {
//return self::{$info->fieldName}($val, $args, $context, $info);
//}
];
parent::__construct($config);
}
//public static function product($val, $args, $context, $info)
//{
//}
}