77namespace Ibexa \GraphQL \Resolver ;
88
99use GraphQL \Error \UserError ;
10+ use Ibexa \Contracts \Core \Repository \Values \Content \Content ;
1011use Ibexa \Contracts \Core \Repository \Values \Content \Query ;
1112use Ibexa \Core \FieldType ;
1213use Ibexa \GraphQL \DataLoader \ContentLoader ;
1314use Ibexa \GraphQL \ItemFactory ;
15+ use Ibexa \GraphQL \Relay \PageAwareConnection ;
1416use Ibexa \GraphQL \Value \Field ;
17+ use Ibexa \GraphQL \Value \Item ;
18+ use Overblog \GraphQLBundle \Definition \Argument ;
19+ use Overblog \GraphQLBundle \Relay \Connection \Paginator ;
1520
1621final class RelationFieldResolver
1722{
18- /** @var \Ibexa\GraphQL\DataLoader\ContentLoader */
19- private $ contentLoader ;
23+ public const DEFAULT_LIMIT = 25 ;
2024
21- /** @var \Ibexa\GraphQL\ItemFactory */
22- private $ itemFactory ;
25+ private ContentLoader $ contentLoader ;
2326
24- public function __construct (ContentLoader $ contentLoader , ItemFactory $ relatedContentItemFactory )
25- {
27+ private ItemFactory $ itemFactory ;
28+
29+ private bool $ enablePagination ;
30+
31+ public function __construct (
32+ ContentLoader $ contentLoader ,
33+ ItemFactory $ relatedContentItemFactory ,
34+ bool $ enablePagination
35+ ) {
2636 $ this ->contentLoader = $ contentLoader ;
2737 $ this ->itemFactory = $ relatedContentItemFactory ;
38+ $ this ->enablePagination = $ enablePagination ;
2839 }
2940
30- public function resolveRelationFieldValue (Field $ field , $ multiple = false )
41+ public function resolveRelationFieldValue (Field $ field , $ multiple = false , ? Argument $ args = null )
3142 {
3243 $ destinationContentIds = $ this ->getContentIds ($ field );
3344
3445 if (empty ($ destinationContentIds ) || array_key_exists (0 , $ destinationContentIds ) && null === $ destinationContentIds [0 ]) {
3546 return $ multiple ? [] : null ;
3647 }
3748
38- $ contentItems = $ this -> contentLoader -> find ( new Query (
49+ $ query = new Query (
3950 ['filter ' => new Query \Criterion \ContentId ($ destinationContentIds )]
40- )) ;
51+ );
4152
4253 if ($ multiple ) {
43- return array_map (
44- function ($ contentId ) use ($ contentItems ) {
45- return $ this ->itemFactory ->fromContent (
46- $ contentItems [array_search ($ contentId , array_column ($ contentItems , 'id ' ))]
47- );
48- },
49- $ destinationContentIds
54+ if (!$ this ->enablePagination || $ args === null ) {
55+ $ contentItems = $ this ->contentLoader ->find ($ query );
56+
57+ return array_map (
58+ function (int $ contentId ) use ($ contentItems ): Item {
59+ return $ this ->itemFactory ->fromContent (
60+ $ contentItems [array_search ($ contentId , array_column ($ contentItems , 'id ' ), true )]
61+ );
62+ },
63+ $ destinationContentIds
64+ );
65+ }
66+
67+ $ paginator = new Paginator (function ($ offset , $ limit ) use ($ query ): array {
68+ $ query ->offset = $ offset ;
69+ $ query ->limit = $ limit ?? self ::DEFAULT_LIMIT ;
70+ $ contentItems = $ this ->contentLoader ->find ($ query );
71+
72+ return array_map (
73+ function (Content $ content ): Item {
74+ return $ this ->itemFactory ->fromContent (
75+ $ content
76+ );
77+ },
78+ $ contentItems
79+ );
80+ });
81+
82+ return PageAwareConnection::fromConnection (
83+ $ paginator ->auto (
84+ $ args ,
85+ function () use ($ query ): int {
86+ return $ this ->contentLoader ->count ($ query );
87+ }
88+ ),
89+ $ args
5090 );
5191 }
5292
93+ $ query ->limit = 1 ;
94+ $ contentItems = $ this ->contentLoader ->find ($ query );
95+
5396 return $ contentItems [0 ] ? $ this ->itemFactory ->fromContent ($ contentItems [0 ]) : null ;
5497 }
5598
@@ -62,11 +105,13 @@ private function getContentIds(Field $field): array
62105 {
63106 if ($ field ->value instanceof FieldType \RelationList \Value) {
64107 return $ field ->value ->destinationContentIds ;
65- } elseif ($ field ->value instanceof FieldType \Relation \Value) {
108+ }
109+
110+ if ($ field ->value instanceof FieldType \Relation \Value) {
66111 return [$ field ->value ->destinationContentId ];
67- } else {
68- throw new UserError ('\$field does not contain a RelationList or Relation Field value ' );
69112 }
113+
114+ throw new UserError ('\$field does not contain a RelationList or Relation Field value ' );
70115 }
71116}
72117
0 commit comments