[WIP] Switched from rest to GraphQL #162-2

This commit is contained in:
Sven Heidemann 2023-02-09 16:09:34 +01:00
parent 4266db7e35
commit 6aca981fd1
7 changed files with 59 additions and 25 deletions

View File

@ -2,6 +2,7 @@ type Server implements TableQuery {
id: ID
discordId: String
name: String
iconURL: String
autoRoleCount: Int
autoRoles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole]

View File

@ -35,6 +35,7 @@ class ServerQuery(DataQueryABC):
self.set_field("id", self.resolve_id)
self.set_field("discordId", self.resolve_discord_id)
self.set_field("name", self.resolve_name)
self.set_field("iconURL", self.resolve_icon_url)
self.add_collection(
"autoRole", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id)
@ -54,3 +55,7 @@ class ServerQuery(DataQueryABC):
def resolve_name(self, server: Server, *_):
guild = self._bot.get_guild(server.discord_server_id)
return None if guild is None else guild.name
def resolve_icon_url(self, server: Server, *_):
guild = self._bot.get_guild(server.discord_server_id)
return None if guild is None else guild.icon.url

View File

@ -0,0 +1,13 @@
export class Queries {
static serverInfoQuery = `
query {
serverCount
servers {
id
name
iconURL
userCount
}
}
`;
}

View File

@ -0,0 +1,6 @@
import { Server } from "../data/server.model";
export interface Query {
serverCount: number;
servers: Server[];
}

View File

@ -0,0 +1,5 @@
import { Query } from "./query.model";
export interface QueryResult {
data: any;
}

View File

@ -3,13 +3,15 @@ import { FormBuilder, FormControl, FormGroup } from "@angular/forms";
import { Router } from "@angular/router";
import { TranslateService } from "@ngx-translate/core";
import { LazyLoadEvent } from "primeng/api";
import { debounceTime } from "rxjs";
import { debounceTime, throwError } from "rxjs";
import { ConfirmationDialogService } from "src/app/services/confirmation-dialog/confirmation-dialog.service";
import { DataService } from "src/app/services/data/data.service";
import { ServerService } from "src/app/services/data/server.service";
import { SpinnerService } from "src/app/services/spinner/spinner.service";
import { ToastService } from "src/app/services/toast/toast.service";
import { Server } from "../../../../../models/data/server.model";
import { catchError } from "rxjs/operators";
import { Queries } from "../../../../../models/graphql/queries.model";
@Component({
selector: 'app-dashboard',
@ -71,14 +73,14 @@ export class DashboardComponent implements OnInit {
async loadNextPage() {
this.spinnerService.showSpinner();
// this.data.getFilteredServers(this.searchCriterions).pipe(catchError(err => {
// this.spinnerService.hideSpinner();
// return throwError(() => err);
// })).subscribe(list => {
// this.totalRecords = list.totalCount;
// this.servers = list.servers;
// this.spinnerService.hideSpinner();
// });
this.data.query(Queries.serverInfoQuery).pipe(catchError(err => {
this.spinnerService.hideSpinner();
return throwError(() => err);
})).subscribe(data => {
this.totalRecords = data.data.serverCount;
this.servers = data.data.servers;
this.spinnerService.hideSpinner();
});
}
nextPage(event: LazyLoadEvent) {

View File

@ -1,28 +1,30 @@
import { HttpClient } from "@angular/common/http";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { SettingsService } from "../settings/settings.service";
import { Observable } from "rxjs";
import { QueryResult } from "../../models/graphql/result.model";
@Injectable({
providedIn: 'root'
providedIn: "root"
})
export class DataService {
// serverQuery = this.apollo.watchQuery({
// query: gql`
// {
// serverCount
// servers {
// id
// name
// }
// }
// `
// });
constructor(
private appsettings: SettingsService,
private http: HttpClient
) {}
) {
}
query(query: string, variables: object = {}): Observable<QueryResult> {
return this.http.post<QueryResult>(`${this.appsettings.getApiURL()}/api/graphql`,
JSON.stringify({
query, variables
}), {
headers: new HttpHeaders({
"Content-Type": "application/json"
})
}
);
}
}