Skip to content

Commit b382183

Browse files
committed
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
1 parent cb2dbc7 commit b382183

File tree

3 files changed

+99
-31
lines changed

3 files changed

+99
-31
lines changed

src/GraphQL/Concerns/HasQueryOperations.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use ThothApi\GraphQL\Models\Title;
3030
use ThothApi\GraphQL\Models\Work;
3131
use ThothApi\GraphQL\Models\WorkFeaturedVideo;
32+
use ThothApi\GraphQL\Queries\ImprintQuery;
3233

3334
trait HasQueryOperations
3435
{
@@ -227,9 +228,15 @@ public function imprint(string $imprintId): Imprint
227228
return $this->get('imprint', $imprintId);
228229
}
229230

230-
public function imprints(array $args = []): array
231+
public function imprints(array $args = [], bool $includeRestrictedFields = false): array
231232
{
232-
return $this->getMany('imprint', $args);
233+
if (!$includeRestrictedFields) {
234+
return $this->getMany('imprint', $args);
235+
}
236+
237+
$query = (new ImprintQuery())->getManyQueryWithRestrictedFields(true);
238+
$result = $this->runGraphqlQuery($query, array_filter($args, fn ($value) => $value !== null))->getData();
239+
return array_map(fn ($data) => new Imprint($data), $result['imprints']);
233240
}
234241

235242
public function imprintCount(array $args = []): int

src/GraphQL/Queries/ImprintQuery.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,50 @@ class ImprintQuery extends AbstractQuery
66
{
77
public function getQuery(): string
88
{
9-
return $this->buildQuery(
10-
<<<GQL
11-
query(\$imprintId: Uuid!) {
12-
imprint(imprintId: \$imprintId) {
9+
return $this->buildQueryWithRestrictedFields(
10+
<<<'GQL'
11+
query($imprintId: Uuid!) {
12+
imprint(imprintId: $imprintId) {
1313
...imprintFields
1414
}
1515
}
16-
GQL
16+
GQL,
17+
true
1718
);
1819
}
1920

2021
public function getManyQuery(): string
2122
{
22-
return $this->buildQuery(
23-
<<<GQL
23+
return $this->getManyQueryWithRestrictedFields(false);
24+
}
25+
26+
public function getManyQueryWithRestrictedFields(bool $includeRestrictedFields = false): string
27+
{
28+
return $this->buildQueryWithRestrictedFields(
29+
<<<'GQL'
2430
query(
25-
\$limit: Int = 100
26-
\$offset: Int = 0
27-
\$filter: String = ""
28-
\$field: ImprintField = IMPRINT_NAME
29-
\$direction: Direction = ASC
30-
\$publishers: [Uuid!] = []
31+
$limit: Int = 100
32+
$offset: Int = 0
33+
$filter: String = ""
34+
$field: ImprintField = IMPRINT_NAME
35+
$direction: Direction = ASC
36+
$publishers: [Uuid!] = []
3137
) {
3238
imprints(
33-
limit: \$limit
34-
offset: \$offset
35-
filter: \$filter
39+
limit: $limit
40+
offset: $offset
41+
filter: $filter
3642
order: {
37-
field: \$field
38-
direction: \$direction
43+
field: $field
44+
direction: $direction
3945
}
40-
publishers: \$publishers
46+
publishers: $publishers
4147
) {
4248
...imprintFields
4349
}
4450
}
45-
GQL
51+
GQL,
52+
$includeRestrictedFields
4653
);
4754
}
4855

@@ -63,20 +70,38 @@ public function getCountQuery(): string
6370

6471
protected function getFieldsFragment(): string
6572
{
73+
return $this->getFieldsFragmentWithRestrictedFields(true);
74+
}
75+
76+
protected function getFieldsFragmentWithRestrictedFields(bool $includeRestrictedFields = false): string
77+
{
78+
$restrictedFields = $includeRestrictedFields ? <<<GQL
79+
s3Bucket
80+
cdnDomain
81+
cloudfrontDistId
82+
GQL : '';
83+
6684
return <<<GQL
6785
fragment imprintFields on Imprint {
6886
imprintId
6987
publisherId
7088
imprintName
7189
imprintUrl
7290
crossmarkDoi
73-
s3Bucket
74-
cdnDomain
75-
cloudfrontDistId
91+
{$restrictedFields}
7692
defaultCurrency
7793
defaultPlace
7894
defaultLocale
7995
}
8096
GQL;
8197
}
98+
99+
private function buildQueryWithRestrictedFields(string $queryBody, bool $includeRestrictedFields): string
100+
{
101+
$fragment = $this->getFieldsFragmentWithRestrictedFields($includeRestrictedFields);
102+
return <<<GQL
103+
{$queryBody}
104+
{$fragment}
105+
GQL;
106+
}
82107
}

tests/GraphQL/Queries/ImprintQueryTest.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function setUp(): void
1616

1717
public function testGetImprintQuery(): void
1818
{
19-
$fragment = $this->getFieldsFragment();
19+
$fragment = $this->getFieldsFragment(true);
2020
$expectedQuery = <<<GQL
2121
query(\$imprintId: Uuid!) {
2222
imprint(imprintId: \$imprintId) {
@@ -32,7 +32,7 @@ public function testGetImprintQuery(): void
3232

3333
public function testGetImprintsQuery(): void
3434
{
35-
$fragment = $this->getFieldsFragment();
35+
$fragment = $this->getFieldsFragment(false);
3636
$expectedQuery = <<<GQL
3737
query(
3838
\$limit: Int = 100
@@ -62,6 +62,38 @@ public function testGetImprintsQuery(): void
6262
$this->assertSame($expectedQuery, $query);
6363
}
6464

65+
public function testGetImprintsQueryWithRestrictedFields(): void
66+
{
67+
$fragment = $this->getFieldsFragment(true);
68+
$expectedQuery = <<<GQL
69+
query(
70+
\$limit: Int = 100
71+
\$offset: Int = 0
72+
\$filter: String = ""
73+
\$field: ImprintField = IMPRINT_NAME
74+
\$direction: Direction = ASC
75+
\$publishers: [Uuid!] = []
76+
) {
77+
imprints(
78+
limit: \$limit
79+
offset: \$offset
80+
filter: \$filter
81+
order: {
82+
field: \$field
83+
direction: \$direction
84+
}
85+
publishers: \$publishers
86+
) {
87+
...imprintFields
88+
}
89+
}
90+
{$fragment}
91+
GQL;
92+
93+
$query = $this->imprintQuery->getManyQueryWithRestrictedFields(true);
94+
$this->assertSame($expectedQuery, $query);
95+
}
96+
6597
public function testGetImprintCountQuery(): void
6698
{
6799
$expectedQuery = <<<GQL
@@ -80,18 +112,22 @@ public function testGetImprintCountQuery(): void
80112
$this->assertSame($expectedQuery, $query);
81113
}
82114

83-
public function getFieldsFragment(): string
115+
public function getFieldsFragment(bool $includeRestrictedFields): string
84116
{
117+
$restrictedFields = $includeRestrictedFields ? <<<GQL
118+
s3Bucket
119+
cdnDomain
120+
cloudfrontDistId
121+
GQL : '';
122+
85123
return <<<GQL
86124
fragment imprintFields on Imprint {
87125
imprintId
88126
publisherId
89127
imprintName
90128
imprintUrl
91129
crossmarkDoi
92-
s3Bucket
93-
cdnDomain
94-
cloudfrontDistId
130+
{$restrictedFields}
95131
defaultCurrency
96132
defaultPlace
97133
defaultLocale

0 commit comments

Comments
 (0)