3
3
namespace AppBundle \GraphQL \Relay \Resolver ;
4
4
5
5
use AppBundle \Entity \Faction ;
6
+ use AppBundle \Entity \Repository \ShipRepository ;
6
7
use Overblog \GraphQLBundle \Relay \Connection \Output \ConnectionBuilder ;
7
8
use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
8
9
use Symfony \Component \DependencyInjection \ContainerAwareTrait ;
@@ -27,7 +28,46 @@ public function resolveEmpire()
27
28
28
29
public function resolveShips (Faction $ faction , $ args )
29
30
{
30
- return ConnectionBuilder::connectionFromArray ($ faction ->getShips ()->toArray (), $ args );
31
+ // $ships = $faction->getShips()->toArray();
32
+ // $connection = ConnectionBuilder::connectionFromArray($ships, $args);
33
+ // $connection->sliceSize = count($connection->edges);
34
+ //
35
+ // return $connection;
36
+
37
+ /** @var ShipRepository $repository */
38
+ $ repository = $ this ->container
39
+ ->get ('doctrine.orm.default_entity_manager ' )
40
+ ->getRepository ('AppBundle:Ship ' );
41
+
42
+ $ arrayLength = $ repository ->countAllByFactionId ($ faction ->getId ());
43
+ $ beforeOffset = ConnectionBuilder::getOffsetWithDefault ($ args ['before ' ], $ arrayLength );
44
+ $ afterOffset = ConnectionBuilder::getOffsetWithDefault ($ args ['after ' ], -1 );
45
+
46
+ $ startOffset = max ($ afterOffset , -1 ) + 1 ;
47
+ $ endOffset = min ($ beforeOffset , $ arrayLength );
48
+
49
+ if (is_numeric ($ args ['first ' ])) {
50
+ $ endOffset = min ($ endOffset , $ startOffset + $ args ['first ' ]);
51
+ }
52
+ if (is_numeric ($ args ['last ' ])) {
53
+ $ startOffset = max ($ startOffset , $ endOffset - $ args ['last ' ]);
54
+ }
55
+ $ offset = max ($ startOffset , 0 );
56
+ $ limit = $ endOffset - $ startOffset ;
57
+
58
+ $ ships = $ repository ->retrieveShipsByFactionId ($ faction ->getId (), $ offset , $ limit );
59
+
60
+ $ connection = ConnectionBuilder::connectionFromArraySlice (
61
+ $ ships ,
62
+ $ args ,
63
+ [
64
+ 'sliceStart ' => $ offset ,
65
+ 'arrayLength ' => $ arrayLength ,
66
+ ]
67
+ );
68
+ $ connection ->sliceSize = count ($ ships );
69
+
70
+ return $ connection ;
31
71
}
32
72
33
73
private function getFactionByType ($ type )
0 commit comments