Skip to content

Commit

Permalink
Add client-side GraphQL handling (#2219)
Browse files Browse the repository at this point in the history
Apollo is currently the most popular GraphQL client and caching library.
This PR eliminates my prior attempt at creating a custom GraphQL client,
and sets up Apollo for future use. As a proof-of-concept, I switched
`/projects` to a GraphQL backend to demonstrate Apollo's pagination
features. While doing so, I added Luxon for easy client-side date
handling.

Since the API endpoint for `/projects` is no longer used, I have marked
it as deprecated, with final removal scheduled for CDash 4.0. Users of
this endpoint are advised to use the GraphQL API instead.

Reviewers can see the pagination functionality in action by changing the
`default_count` value in `/config/lighthouse.php` to a small value.

---------

Co-authored-by: Shelly Belsky <71195502+sbelsk@users.noreply.github.com>
Co-authored-by: Zack Galbreath <zack.galbreath@kitware.com>
  • Loading branch information
3 people committed Jun 3, 2024
1 parent 79af657 commit 95bdb34
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 321 deletions.
13 changes: 12 additions & 1 deletion app/Http/Controllers/ViewProjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
final class ViewProjectsController extends AbstractController
{
public function viewAllProjects(): View|RedirectResponse
{
return $this->viewProjects(true);
}

public function viewActiveProjects(): View|RedirectResponse
{
return $this->viewProjects();
}

private function viewProjects(bool $all = false): View|RedirectResponse
{
$num_public_projects = (int) DB::select('
SELECT COUNT(*) AS c FROM project WHERE public=?
Expand All @@ -22,7 +32,8 @@ public function viewAllProjects(): View|RedirectResponse
return $this->redirectToLogin();
}

return $this->view("project.view-all-projects");
return $this->view("project.view-all-projects")
->with('show_all', $all);
}

public function fetchPageContent(): JsonResponse
Expand Down
16 changes: 16 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -216,6 +217,21 @@ public function builds(): HasMany
});
}

/**
* TODO: Share code with builds(). As of Laravel 10, aggregates added to hasMany relations
* with conditional clauses are unsupported/broken. A reusable scope may be a better approach.
*
* @return HasOne<Build>
*/
public function mostRecentBuild(): HasOne
{
return $this->hasOne(Build::class, 'projectid', 'id')
->ofMany(['submittime' => 'max'], function (Builder $query) {
$query->where('parentid', 0)
->orWhere('parentid', -1);
});
}

/**
* @return HasMany<BlockedBuild>
*/
Expand Down
5 changes: 3 additions & 2 deletions app/cdash/app/Controller/Api/ViewProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
use Illuminate\Support\Facades\DB;

/**
* TODO: (williamjallen) move all of this logic over to app/Http/Controllers/ViewProjectsController.php
*
* API controller for viewProjects.php.
*
* @deprecated 05/23/2024 This endpoint is deprecated in favor of the GraphQL API and will eventually be removed.
**/
class ViewProjects extends \CDash\Controller\Api
{
Expand All @@ -45,6 +45,7 @@ public function __construct(Database $db)
public function getResponse()
{
$response = begin_JSON_response();
$response['deprecated'] = 'This endpoint is deprecated and will eventually be removed.';

$global_banner = Banner::find(0);
if ($global_banner !== null && strlen($global_banner->text) > 0) {
Expand Down
6 changes: 6 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ type Project {
filters: _ @filter(inputType: "BuildFilterInput")
): [Build!]! @hasMany(type: CONNECTION) @orderBy(column: "id")

"""
The most recent build submitted to this server. Note: this is determined by
submission time, not build start time.
"""
mostRecentBuild: Build @hasOne

"The sites which have submitted a build to this project."
sites(
filters: _ @filter(inputType: "SiteFilterInput")
Expand Down
Loading

0 comments on commit 95bdb34

Please sign in to comment.