Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/GraphQL/Concerns/HasQueryOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
71 changes: 48 additions & 23 deletions src/GraphQL/Queries/ImprintQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,50 @@ class ImprintQuery extends AbstractQuery
{
public function getQuery(): string
{
return $this->buildQuery(
<<<GQL
query(\$imprintId: Uuid!) {
imprint(imprintId: \$imprintId) {
return $this->buildQueryWithRestrictedFields(
<<<'GQL'
query($imprintId: Uuid!) {
imprint(imprintId: $imprintId) {
...imprintFields
}
}
GQL
GQL,
true
);
}

public function getManyQuery(): string
{
return $this->buildQuery(
<<<GQL
return $this->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
);
}

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

protected function getFieldsFragment(): string
{
return $this->getFieldsFragmentWithRestrictedFields(true);
}

protected function getFieldsFragmentWithRestrictedFields(bool $includeRestrictedFields = false): string
{
$restrictedFields = $includeRestrictedFields ? <<<GQL
s3Bucket
cdnDomain
cloudfrontDistId
GQL : '';

return <<<GQL
fragment imprintFields on Imprint {
imprintId
publisherId
imprintName
imprintUrl
crossmarkDoi
s3Bucket
cdnDomain
cloudfrontDistId
{$restrictedFields}
defaultCurrency
defaultPlace
defaultLocale
}
GQL;
}

private function buildQueryWithRestrictedFields(string $queryBody, bool $includeRestrictedFields): string
{
$fragment = $this->getFieldsFragmentWithRestrictedFields($includeRestrictedFields);
return <<<GQL
{$queryBody}
{$fragment}
GQL;
}
}
48 changes: 42 additions & 6 deletions tests/GraphQL/Queries/ImprintQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void

public function testGetImprintQuery(): void
{
$fragment = $this->getFieldsFragment();
$fragment = $this->getFieldsFragment(true);
$expectedQuery = <<<GQL
query(\$imprintId: Uuid!) {
imprint(imprintId: \$imprintId) {
Expand All @@ -32,7 +32,7 @@ public function testGetImprintQuery(): void

public function testGetImprintsQuery(): void
{
$fragment = $this->getFieldsFragment();
$fragment = $this->getFieldsFragment(false);
$expectedQuery = <<<GQL
query(
\$limit: Int = 100
Expand Down Expand Up @@ -62,6 +62,38 @@ public function testGetImprintsQuery(): void
$this->assertSame($expectedQuery, $query);
}

public function testGetImprintsQueryWithRestrictedFields(): void
{
$fragment = $this->getFieldsFragment(true);
$expectedQuery = <<<GQL
query(
\$limit: Int = 100
\$offset: Int = 0
\$filter: String = ""
\$field: ImprintField = IMPRINT_NAME
\$direction: Direction = ASC
\$publishers: [Uuid!] = []
) {
imprints(
limit: \$limit
offset: \$offset
filter: \$filter
order: {
field: \$field
direction: \$direction
}
publishers: \$publishers
) {
...imprintFields
}
}
{$fragment}
GQL;

$query = $this->imprintQuery->getManyQueryWithRestrictedFields(true);
$this->assertSame($expectedQuery, $query);
}

public function testGetImprintCountQuery(): void
{
$expectedQuery = <<<GQL
Expand All @@ -80,18 +112,22 @@ public function testGetImprintCountQuery(): void
$this->assertSame($expectedQuery, $query);
}

public function getFieldsFragment(): string
public function getFieldsFragment(bool $includeRestrictedFields): string
{
$restrictedFields = $includeRestrictedFields ? <<<GQL
s3Bucket
cdnDomain
cloudfrontDistId
GQL : '';

return <<<GQL
fragment imprintFields on Imprint {
imprintId
publisherId
imprintName
imprintUrl
crossmarkDoi
s3Bucket
cdnDomain
cloudfrontDistId
{$restrictedFields}
defaultCurrency
defaultPlace
defaultLocale
Expand Down
Loading