Skip to content

Commit

Permalink
Merge pull request #29 from TomHAnderson/feature/lazy-collections
Browse files Browse the repository at this point in the history
Enabled lazy collections
  • Loading branch information
TomHAnderson authored Oct 6, 2020
2 parents 217e3ba + 9083875 commit fa7c10d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 43 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"zendframework/zend-hydrator": "^2.4",
"zendframework/zend-modulemanager": "^2.8",
"doctrine/common": "^2.8",
"doctrine/doctrine-orm-module": "^1.1.8",
"zendframework/zend-mvc-console": "^1.2",
"zendframework/zend-config": "^3.2",
"api-skeletons/zf-doctrine-criteria": "^1.0",
Expand Down
23 changes: 11 additions & 12 deletions src/Type/EntityTypeAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
) {
$collection = $fieldResolver($source, $args, $context, $resolveInfo);

// Do not process empty collections
if (! sizeof($collection)) {
return [];
}
// It is better to process empty collections than extract an entire collection
// just to get its count. Lazy loading will fetch a whole collection to get
// a count but extra lazy will not.
// There was a check here for collection size; now removed.

$filter = $args['filter'] ?? [];
$filterArray = [];
Expand Down Expand Up @@ -232,7 +232,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$distinctField = $field;
}
break;
case 'memberof':
default:
$filterArray[] = [
'type' => $filter,
Expand All @@ -244,7 +243,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}
}

$entityClassName = ClassUtils::getRealClass(get_class($collection->first()));
$entityClassName = $collection->getTypeClass()->name;
$metadata = $objectManager->getClassMetadata($entityClassName);

foreach ($filterArray as $key => $filter) {
Expand All @@ -265,14 +264,14 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}

//Rebuild collection using hydrators
$entityClassName = ClassUtils::getRealClass(get_class($collection->first()));
$hydratorAlias = 'ZF\\Doctrine\\GraphQL\\Hydrator\\'
. str_replace('\\', '_', $entityClassName);

$data = $hydratorExtractTool
->extractToCollection($collection, $hydratorAlias, $options);

$matching = $data->matching($criteria);
$matching = $hydratorExtractTool
->extractToCollection(
$collection->matching($criteria),
$hydratorAlias,
$options
);

if ($distinctField) {
$distinctValueArray = [];
Expand Down
17 changes: 1 addition & 16 deletions test/GraphQL/CriteriaFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function testIsNotNull($schemaName, $context)
$result = GraphQL::executeQuery($schema, $query, $rootValue = null, $context, $variableValues = null);
$output = $result->toArray();

$this->assertEquals(3, sizeof($output['data']['artist'][0]['performance']));
$this->assertEquals(2, sizeof($output['data']['artist'][0]['performance']));
}

/**
Expand Down Expand Up @@ -294,21 +294,6 @@ public function testOverTheLimit($schemaName, $context)
$this->assertEquals(5, sizeof($output['data']['artist'][0]['performance']));
}

/**
* @dataProvider schemaDataProvider
*/
public function testMemberOf($schemaName, $context)
{
$schema = $this->getSchema($schemaName);

$query = "{ user ( filter: { id:1 } ) { name artist ( filter: { alias_memberof:\"b2\" } ) { id alias } } }";

$result = GraphQL::executeQuery($schema, $query, $rootValue = null, $context, $variableValues = null);
$output = $result->toArray();

$this->assertEquals(['b1', 'b2', 'b3'], $output['data']['user'][0]['artist'][0]['alias']);
}

/**
* @dataProvider schemaDataProvider
*/
Expand Down
15 changes: 0 additions & 15 deletions test/GraphQL/QueryBuilderFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,6 @@ public function testOverTheLimit($schemaName, $context)
$this->assertEquals(5, sizeof($output['data']['performance']));
}

/**
* @dataProvider schemaDataProvider
*/
public function testMemberOf($schemaName, $context)
{
$schema = $this->getSchema($schemaName);

$query = "{ artist ( filter: { alias_memberof:\"b2\" } ) { id alias } }";

$result = GraphQL::executeQuery($schema, $query, $rootValue = null, $context, $variableValues = null);
$output = $result->toArray();

$this->assertEquals(['b1', 'b2', 'b3'], $output['data']['artist'][0]['alias']);
}

/**
* @dataProvider schemaDataProvider
*/
Expand Down

0 comments on commit fa7c10d

Please sign in to comment.