Skip to content

Commit 114a8f5

Browse files
authored
Merge pull request #10 from magefan/3773-graph-ql-related-posts-products
3773 graph ql related posts products
2 parents 2b93a20 + 0b53244 commit 114a8f5

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

Model/Resolver/DataProvider/Post.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public function __construct(
4242

4343
/**
4444
* @param string $postId
45+
* @param array|null $fields
4546
* @return array
4647
* @throws NoSuchEntityException
4748
*/
48-
public function getData(string $postId): array
49+
public function getData(string $postId, $fields = null): array
4950
{
5051
$post = $this->postRepository->getFactory()->create();
5152
$post->getResource()->load($post, $postId);
@@ -54,6 +55,6 @@ public function getData(string $postId): array
5455
throw new NoSuchEntityException();
5556
}
5657

57-
return $post->getDynamicData();
58+
return $post->getDynamicData($fields);
5859
}
5960
}

Model/Resolver/Post.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public function resolve(
4545
array $args = null
4646
) {
4747
$postId = $this->getPostId($args);
48-
$postData = $this->getPostData($postId);
48+
$fields = $info ? $info->getFieldSelection(10) : null;
49+
50+
try {
51+
$postData = $this->postDataProvider->getData($postId, $fields);
52+
} catch (NoSuchEntityException $e) {
53+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
54+
}
55+
4956
return $postData;
5057
}
5158

@@ -62,19 +69,4 @@ private function getPostId(array $args): string
6269

6370
return (string)$args['id'];
6471
}
65-
66-
/**
67-
* @param string $postId
68-
* @return array
69-
* @throws GraphQlNoSuchEntityException
70-
*/
71-
private function getPostData(string $postId): array
72-
{
73-
try {
74-
$postData = $this->postDataProvider->getData($postId);
75-
} catch (NoSuchEntityException $e) {
76-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
77-
}
78-
return $postData;
79-
}
8072
}

Model/Resolver/Posts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function resolve(
7575

7676
if (isset($args['sort'])) {
7777
$sortOrder = $this->sortOrderBuilder
78-
->setField($args['sortFiled'])
78+
->setField(isset($args['sortFiled']) ? $args['sortFiled'] : 'update_time')
7979
->setDirection($args['sort'][0])
8080
->create();
8181
$searchCriteria->setSortOrders([$sortOrder]);
@@ -101,9 +101,13 @@ public function resolve(
101101
}
102102

103103
$items = $searchResult->getItems();
104+
$fields = $info ? $info->getFieldSelection(10) : null;
104105

105106
foreach ($items as $k => $data) {
106-
$items[$k] = $this->postDataProvider->getData($data['post_id']);
107+
$items[$k] = $this->postDataProvider->getData(
108+
$data['post_id'],
109+
isset($fields['items']) ? $fields['items'] : null
110+
);
107111
}
108112

109113
return [

etc/schema.graphqls

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ type BlogPost {
116116
views_count: Int @doc(description: "Blog Post Views count")
117117
is_recent_posts_skip: Int @doc(description: "Blog Post Is recent posts skip")
118118
short_content: String @doc(description: "Blog Post Short content")
119-
#fb_auto_publish: Int @doc(description: "Blog Post Fb auto publish")
120-
#fb_post_format: String @doc(description: "Blog Post Fb post format")
121-
#fb_published: Int @doc(description: "Blog Post Fb published")
122-
#rp_conditions_serialized: String @doc(description: "Blog Post Rp conditions serialized")
123-
#rp_conditions_generation_time: String @doc(description: "Blog Post Rp conditions generation time")
119+
related_posts: [BlogPost] @doc(description: "Blog Related posts")
120+
related_products: [String] @doc(description: "Blog Related products")
124121
}
125122

126123
type BlogCategory {

0 commit comments

Comments
 (0)