From b382183f01f06b19449ae76eafb92cbdff08d4c8 Mon Sep 17 00:00:00 2001 From: Thiago Matos Date: Wed, 15 Apr 2026 17:30:47 -0400 Subject: [PATCH] fix(graphql): make restricted imprint fields optional Default imprint queries now omit restricted storage fields. Adds an opt-in flag to include restricted imprint fields when they are explicitly needed and updates the query tests accordingly. Refs: documentacao-e-tarefas/desenvolvimento_e_infra#1176 --- src/GraphQL/Concerns/HasQueryOperations.php | 11 +++- src/GraphQL/Queries/ImprintQuery.php | 71 ++++++++++++++------- tests/GraphQL/Queries/ImprintQueryTest.php | 48 ++++++++++++-- 3 files changed, 99 insertions(+), 31 deletions(-) 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 ? <<