Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add site filter to TermsQuery #10131

Merged
merged 2 commits into from
May 28, 2024
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
5 changes: 5 additions & 0 deletions src/GraphQL/Queries/TermsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function args(): array
'page' => GraphQL::int(),
'filter' => GraphQL::type(JsonArgument::NAME),
'sort' => GraphQL::listOf(GraphQL::string()),
'site' => GraphQL::string(),
];
}

Expand All @@ -59,6 +60,10 @@ public function resolve($root, $args)
$this->sortQuery($query, $sort);
}

if ($site = $args['site'] ?? null) {
$query->where('site', $site);
}

return $query->paginate($args['limit'] ?? 1000);
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/GraphQL/TermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,40 @@ public function it_queries_all_terms()
]]]]);
}

/** @test */
public function it_queries_all_terms_in_a_specific_site()
{
$this->createTaxonomies()->createTerms()->createBlueprints();

$this->setSites([
'en' => ['url' => 'http://localhost/', 'locale' => 'en'],
'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]);

Taxonomy::find('tags')->sites(['en', 'fr'])->save();

Term::find('tags::alpha')->in('fr')->data(['title' => 'Le Alpha'])->save();

$query = <<<'GQL'
{
terms(site: "fr") {
data {
id
title
}
}
}
GQL;

$this
->withoutExceptionHandling()
->post('/graphql', ['query' => $query])
->assertGqlOk()
->assertExactJson(['data' => ['terms' => ['data' => [
['id' => 'tags::alpha', 'title' => 'Le Alpha'],
]]]]);
}

/** @test */
public function it_queries_only_terms_on_allowed_sub_resources()
{
Expand Down
Loading