-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from TomHAnderson/feature/include-criteria
Created ExcludeCriteria trait and added includeCriteria to all attributes
- Loading branch information
Showing
9 changed files
with
257 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiSkeletons\Doctrine\GraphQL\Attribute; | ||
|
||
use ApiSkeletons\Doctrine\GraphQL\Criteria\Filters; | ||
use Exception; | ||
|
||
use function array_diff; | ||
use function array_intersect; | ||
|
||
trait ExcludeCriteria | ||
{ | ||
/** @return string[] */ | ||
public function getExcludeCriteria(): array | ||
{ | ||
if ($this->includeCriteria && $this->excludeCriteria) { | ||
throw new Exception('includeCriteria and excludeCriteria are mutually exclusive.'); | ||
} | ||
|
||
if ($this->includeCriteria) { | ||
$this->excludeCriteria = array_diff(Filters::toArray(), $this->includeCriteria); | ||
} elseif ($this->excludeCriteria) { | ||
$this->excludeCriteria = array_intersect(Filters::toArray(), $this->excludeCriteria); | ||
} | ||
|
||
return $this->excludeCriteria; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiSkeletonsTest\Doctrine\GraphQL\Feature\Criteria; | ||
|
||
use ApiSkeletons\Doctrine\GraphQL\Config; | ||
use ApiSkeletons\Doctrine\GraphQL\Driver; | ||
use ApiSkeletonsTest\Doctrine\GraphQL\AbstractTest; | ||
use ApiSkeletonsTest\Doctrine\GraphQL\Entity\Performance; | ||
use GraphQL\GraphQL; | ||
use GraphQL\Type\Definition\ObjectType; | ||
use GraphQL\Type\Schema; | ||
|
||
class IncludeCriteriaTest extends AbstractTest | ||
{ | ||
public function testExcludeCriteria(): void | ||
{ | ||
$config = new Config(['group' => 'IncludeCriteriaTest']); | ||
|
||
$driver = new Driver($this->getEntityManager(), $config); | ||
|
||
$schema = new Schema([ | ||
'query' => new ObjectType([ | ||
'name' => 'query', | ||
'fields' => [ | ||
'performances' => [ | ||
'type' => $driver->connection($driver->type(Performance::class)), | ||
'args' => [ | ||
'filter' => $driver->filter(Performance::class), | ||
'pagination' => $driver->pagination(), | ||
], | ||
'resolve' => $driver->resolve(Performance::class), | ||
], | ||
], | ||
]), | ||
]); | ||
|
||
// Test entity level included filters | ||
$query = '{ performances (filter: { venue: {contains: "Fillmore" } } ) { edges { node { venue } } } }'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
$data = $result->toArray()['data']; | ||
$this->assertEquals('Fillmore Auditorium', $data['performances']['edges'][0]['node']['venue']); | ||
|
||
$query = '{ performances (filter: { venue: {eq: "Fillmore Auditorium" } } ) { edges { node { venue } } } }'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
$data = $result->toArray()['data']; | ||
$this->assertEquals('Fillmore Auditorium', $data['performances']['edges'][0]['node']['venue']); | ||
|
||
// Test entity level excluded filters | ||
$query = '{ performances (filter: { venue: {in: ["Fillmore Auditorium"] } } ) { edges { node { venue } } } }'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
foreach ($result->errors as $error) { | ||
$this->assertEquals('Field "in" is not defined by type "ApiSkeletonsTest_Doctrine_GraphQL_Entity_Performance_IncludeCriteriaTest_filter_venue_filters".', $error->getMessage()); | ||
} | ||
|
||
// Test entity>field level included filters | ||
$query = '{ performances (filter: { city: {eq: "Salt Lake City" } } ) { edges { node { city } } } }'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
$data = $result->toArray()['data']; | ||
$this->assertEquals('Salt Lake City', $data['performances']['edges'][0]['node']['city']); | ||
|
||
// Test entity>field level excluded filters | ||
$query = '{ performances (filter: { city: {contains: "Salt Lake City" } } ) { edges { node { city } } } }'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
foreach ($result->errors as $error) { | ||
$this->assertEquals('Field "contains" is not defined by type "ApiSkeletonsTest_Doctrine_GraphQL_Entity_Performance_IncludeCriteriaTest_filter_city_filters".', $error->getMessage()); | ||
} | ||
|
||
// Test entity>field level included filters excluded by field level exclude | ||
$query = '{ performances (filter: { state: { eq: "UT" } } ) { edges { node { state } } } }'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
foreach ($result->errors as $error) { | ||
$this->assertEquals('Field "eq" is not defined by type "ApiSkeletonsTest_Doctrine_GraphQL_Entity_Performance_IncludeCriteriaTest_filter_state_filters". Did you mean "neq"?', $error->getMessage()); | ||
} | ||
|
||
// Test entity>association level included filters | ||
$query = '{ | ||
performances ( | ||
filter: { | ||
venue: { eq: "Delta Center" } | ||
} | ||
) { | ||
edges { | ||
node { | ||
recordings( | ||
filter: { | ||
source: { contains: "DSBD" } | ||
} | ||
) { | ||
edges { | ||
node { | ||
source | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
$data = $result->toArray()['data']; | ||
$this->assertEquals( | ||
'DSBD > 1C > DAT; Seeded to etree by Dan Stephens', | ||
$data['performances']['edges'][0]['node']['recordings']['edges'][0]['node']['source'], | ||
); | ||
|
||
// Test entity>association level included filters | ||
$query = '{ | ||
performances ( | ||
filter: { | ||
venue: { eq: "Delta Center" } | ||
} | ||
) { | ||
edges { | ||
node { | ||
recordings( | ||
filter: { | ||
source: { eq: "DSBD" } | ||
} | ||
) { | ||
edges { | ||
node { | ||
source | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}'; | ||
$result = GraphQL::executeQuery($schema, $query); | ||
foreach ($result->errors as $error) { | ||
$this->assertEquals('Field "eq" is not defined by type "ApiSkeletonsTest_Doctrine_GraphQL_Entity_Performance_IncludeCriteriaTest_recordings_filter_source_filters".', $error->getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.