diff --git a/src/GraphQL/Concerns/HasQueryOperations.php b/src/GraphQL/Concerns/HasQueryOperations.php index 8c69b59..c5e1d09 100644 --- a/src/GraphQL/Concerns/HasQueryOperations.php +++ b/src/GraphQL/Concerns/HasQueryOperations.php @@ -29,6 +29,7 @@ use ThothApi\GraphQL\Models\Title; use ThothApi\GraphQL\Models\Work; use ThothApi\GraphQL\Models\WorkFeaturedVideo; +use ThothApi\GraphQL\Queries\ImprintQuery; trait HasQueryOperations { @@ -227,9 +228,15 @@ public function imprint(string $imprintId): Imprint return $this->get('imprint', $imprintId); } - public function imprints(array $args = []): array + public function imprints(array $args = [], bool $includeRestrictedFields = false): array { - return $this->getMany('imprint', $args); + if (!$includeRestrictedFields) { + return $this->getMany('imprint', $args); + } + + $query = (new ImprintQuery())->getManyQueryWithRestrictedFields(true); + $result = $this->runGraphqlQuery($query, array_filter($args, fn ($value) => $value !== null))->getData(); + return array_map(fn ($data) => new Imprint($data), $result['imprints']); } public function imprintCount(array $args = []): int diff --git a/src/GraphQL/Queries/ImprintQuery.php b/src/GraphQL/Queries/ImprintQuery.php index 384b514..6b1cf7f 100644 --- a/src/GraphQL/Queries/ImprintQuery.php +++ b/src/GraphQL/Queries/ImprintQuery.php @@ -6,43 +6,50 @@ class ImprintQuery extends AbstractQuery { public function getQuery(): string { - return $this->buildQuery( - <<buildQueryWithRestrictedFields( + <<<'GQL' + query($imprintId: Uuid!) { + imprint(imprintId: $imprintId) { ...imprintFields } } - GQL + GQL, + true ); } public function getManyQuery(): string { - return $this->buildQuery( - <<getManyQueryWithRestrictedFields(false); + } + + public function getManyQueryWithRestrictedFields(bool $includeRestrictedFields = false): string + { + return $this->buildQueryWithRestrictedFields( + <<<'GQL' query( - \$limit: Int = 100 - \$offset: Int = 0 - \$filter: String = "" - \$field: ImprintField = IMPRINT_NAME - \$direction: Direction = ASC - \$publishers: [Uuid!] = [] + $limit: Int = 100 + $offset: Int = 0 + $filter: String = "" + $field: ImprintField = IMPRINT_NAME + $direction: Direction = ASC + $publishers: [Uuid!] = [] ) { imprints( - limit: \$limit - offset: \$offset - filter: \$filter + limit: $limit + offset: $offset + filter: $filter order: { - field: \$field - direction: \$direction + field: $field + direction: $direction } - publishers: \$publishers + publishers: $publishers ) { ...imprintFields } } - GQL + GQL, + $includeRestrictedFields ); } @@ -63,6 +70,17 @@ public function getCountQuery(): string protected function getFieldsFragment(): string { + return $this->getFieldsFragmentWithRestrictedFields(true); + } + + protected function getFieldsFragmentWithRestrictedFields(bool $includeRestrictedFields = false): string + { + $restrictedFields = $includeRestrictedFields ? <<getFieldsFragmentWithRestrictedFields($includeRestrictedFields); + return <<getFieldsFragment(); + $fragment = $this->getFieldsFragment(true); $expectedQuery = <<getFieldsFragment(); + $fragment = $this->getFieldsFragment(false); $expectedQuery = <<assertSame($expectedQuery, $query); } + public function testGetImprintsQueryWithRestrictedFields(): void + { + $fragment = $this->getFieldsFragment(true); + $expectedQuery = <<imprintQuery->getManyQueryWithRestrictedFields(true); + $this->assertSame($expectedQuery, $query); + } + public function testGetImprintCountQuery(): void { $expectedQuery = <<assertSame($expectedQuery, $query); } - public function getFieldsFragment(): string + public function getFieldsFragment(bool $includeRestrictedFields): string { + $restrictedFields = $includeRestrictedFields ? <<