Added graphql support #162-2

This commit is contained in:
Sven Heidemann 2023-02-09 16:36:50 +01:00
parent 6aca981fd1
commit f7c27b77ee
6 changed files with 91 additions and 47 deletions

View File

@ -0,0 +1,4 @@
export interface Page {
pageIndex?: number;
pageSize?: number;
}

View File

@ -0,0 +1,4 @@
export interface Sort {
sortColumn?: string;
sortDirection?: string;
}

View File

@ -1,8 +1,8 @@
export class Queries { export class Queries {
static serverInfoQuery = ` static serverInfoQuery = `
query { query ServerInfo($filter: ServerFilter, $page: Page, $sort: Sort) {
serverCount serverCount
servers { servers(filter: $filter, page: $page, sort: $sort) {
id id
name name
iconURL iconURL

View File

@ -0,0 +1,8 @@
import { Page } from "./filter/page.model";
import { Sort } from "./filter/sort.model";
export interface Variables {
filter?: object;
page?: Page;
sort?: Sort;
}

View File

@ -12,11 +12,14 @@ import { ToastService } from "src/app/services/toast/toast.service";
import { Server } from "../../../../../models/data/server.model"; import { Server } from "../../../../../models/data/server.model";
import { catchError } from "rxjs/operators"; import { catchError } from "rxjs/operators";
import { Queries } from "../../../../../models/graphql/queries.model"; import { Queries } from "../../../../../models/graphql/queries.model";
import { Page } from "../../../../../models/graphql/filter/page.model";
import { Sort } from "../../../../../models/graphql/filter/sort.model";
import { Query } from "../../../../../models/graphql/query.model";
@Component({ @Component({
selector: 'app-dashboard', selector: "app-dashboard",
templateUrl: './dashboard.component.html', templateUrl: "./dashboard.component.html",
styleUrls: ['./dashboard.component.scss'] styleUrls: ["./dashboard.component.scss"]
}) })
export class DashboardComponent implements OnInit { export class DashboardComponent implements OnInit {
@ -24,6 +27,16 @@ export class DashboardComponent implements OnInit {
totalRecords!: number; totalRecords!: number;
filter = {
name: ""
};
page: Page = {
pageIndex: 0,
pageSize: 10
};
sort!: Sort;
filterForm!: FormGroup<{ filterForm!: FormGroup<{
name: FormControl<string | null>, name: FormControl<string | null>,
@ -38,62 +51,68 @@ export class DashboardComponent implements OnInit {
private translate: TranslateService, private translate: TranslateService,
private router: Router, private router: Router,
private serverService: ServerService private serverService: ServerService
) { } ) {
}
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
this.spinnerService.showSpinner(); this.spinnerService.showSpinner();
this.setFilterForm(); this.setFilterForm();
await this.loadNextPage(); this.loadNextPage();
} }
setFilterForm() { setFilterForm() {
this.filterForm = this.fb.group({ this.filterForm = this.fb.group({
name: [''], name: [""]
}); });
this.filterForm.valueChanges.pipe( this.filterForm.valueChanges.pipe(
debounceTime(600) debounceTime(600)
).subscribe(async changes => { ).subscribe(async changes => {
// if (changes.name) { if (changes.name) {
// this.searchCriterions.name = changes.name; this.filter.name = changes.name;
// } else { }
// this.searchCriterions.name = null;
// } if (this.page.pageSize)
// this.page.pageSize = 10;
// if (this.searchCriterions.pageSize)
// this.searchCriterions.pageSize = 10; if (this.page.pageSize)
// this.page.pageIndex = 0;
// if (this.searchCriterions.pageSize)
// this.searchCriterions.pageIndex = 0;
await this.loadNextPage(); await this.loadNextPage();
}); });
} }
async loadNextPage() { loadNextPage() {
this.spinnerService.showSpinner(); this.spinnerService.showSpinner();
this.data.query(Queries.serverInfoQuery).pipe(catchError(err => { this.data.query<Query>(Queries.serverInfoQuery,{
filter: this.filter,
page: this.page,
sort: this.sort,
}).pipe(catchError(err => {
this.spinnerService.hideSpinner(); this.spinnerService.hideSpinner();
return throwError(() => err); return throwError(() => err);
})).subscribe(data => { })).subscribe(data => {
this.totalRecords = data.data.serverCount; this.totalRecords = data.serverCount;
this.servers = data.data.servers; this.servers = data.servers;
this.spinnerService.hideSpinner(); this.spinnerService.hideSpinner();
}); });
} }
nextPage(event: LazyLoadEvent) { nextPage(event: LazyLoadEvent) {
// this.searchCriterions.pageSize = event.rows ?? 0; this.page.pageSize = event.rows ?? 0;
// if (event.first != null && event.rows != null) if (event.first != null && event.rows != null)
// this.searchCriterions.pageIndex = event.first / event.rows; this.page.pageIndex = event.first / event.rows;
// this.searchCriterions.sortColumn = event.sortField ?? null;
// this.searchCriterions.sortDirection = event.sortOrder === 1 ? 'asc' : event.sortOrder === -1 ? 'desc' : 'asc'; this.sort = {
// sortColumn: event.sortField ?? "",
// if (event.filters) { sortDirection: event.sortOrder === 1 ? "asc" : event.sortOrder === -1 ? "desc" : "asc"
// // + "" => convert to string };
// this.searchCriterions.name = event.filters['name'] ? event.filters['name'] + "" : null;
// } if (event.filters) {
// + "" => convert to string
this.filter.name = event.filters["name"] ? event.filters["name"] + "" : "";
}
this.loadNextPage(); this.loadNextPage();
} }
@ -104,7 +123,7 @@ export class DashboardComponent implements OnInit {
selectServer(server: Server) { selectServer(server: Server) {
this.serverService.server$.next(server); this.serverService.server$.next(server);
this.router.navigate(['/server']); this.router.navigate(["/server"]);
} }
} }

View File

@ -1,8 +1,8 @@
import { HttpClient, HttpHeaders } from "@angular/common/http"; import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core"; import { Injectable } from "@angular/core";
import { SettingsService } from "../settings/settings.service"; import { SettingsService } from "../settings/settings.service";
import { Observable } from "rxjs"; import { map, Observable } from "rxjs";
import { QueryResult } from "../../models/graphql/result.model"; import { Variables } from "../../models/graphql/variables.model";
@Injectable({ @Injectable({
providedIn: "root" providedIn: "root"
@ -15,16 +15,25 @@ export class DataService {
) { ) {
} }
query(query: string, variables: object = {}): Observable<QueryResult> { // query(query: string, variables: object = {}): Observable<QueryResult> {
return this.http.post<QueryResult>(`${this.appsettings.getApiURL()}/api/graphql`, // return this.http.post<QueryResult>(`${this.appsettings.getApiURL()}/api/graphql`,
JSON.stringify({ // JSON.stringify({
query, variables // query, variables
}), { // }), {
headers: new HttpHeaders({ // headers: new HttpHeaders({
"Content-Type": "application/json" // "Content-Type": "application/json"
}) // })
} // }
); // );
// }
public query<T>(query: string, variables?: Variables): Observable<T> {
return this.http
.post<{ data: T }>(`${this.appsettings.getApiURL()}/api/graphql`, {
query: query,
variables: variables
})
.pipe(map((d) => d.data));
} }
} }