Apollo GraphQL Query работает только один раз.Как я могу запустить его снова? - PullRequest
0 голосов
/ 19 февраля 2019

Я использую Angular 7 , Apollo Server , MongoDB в моем проекте.У меня проблема.

Запрос Apollo Graphql выполняется один раз в функции ngOnInit .Но, как вы можете видеть из моего примера кода, я хочу обновить запрос при запуске функции onPageChanged . OnPageChanged или когда я помещаю свои коды GraphQL в другую функцию, коды никогда не работают во второй раз.Что мне нужно сделать, чтобы регулировать мой запрос в любое время.не могли бы вы помочь мне, пожалуйста.

import { Component, OnInit } from '@angular/core';
import { AdminAuthorsGQL, Authors } from 'src/generated-models';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { PageEvent } from '@angular/material';

@Component({
  selector: 'app-admin-authors',
  templateUrl: './admin-authors.component.html',
  styleUrls: ['./admin-authors.component.scss']
})

export class AdminAuthorsComponent implements OnInit {
  public displayedColumns: string[] = ['no', 'image', 'name', 'translator', 'books', 'transactions'];

public table = {
    display: false,
    text: 'table'
  };

public form = {
    display: false,
    text: 'form'
  };

authors$: Observable<Authors.Authors[]>;

pageEvent: PageEvent;

pageSize: number = 25; // default

skip: number = 0;

length = 1000;

pageSizeOptions: number[] = [5, 10, 25, 50, 100];

isLoadingResults: boolean = false;
isNoDataFound: boolean = false;

constructor(private adminAuthorsGql: AdminAuthorsGQL) {}

ngOnInit() {
    this.authors$ = this.adminAuthorsGql
    .watch({ skip: this.skip, limit: this.pageSize })
    .valueChanges.pipe(map(result => result.data.authorsWithPaginate));

    this.isLoadingResults = false;
    this.isNoDataFound = false;
  }

setPageSizeOptions(setPageSizeOptionsInput: string) {
    this.pageSizeOptions = setPageSizeOptionsInput.split(',').map(str => +str);
  }

onPageChanged(e) {
    console.log(e);
    this.pageSize = e.pageSize;
    this.authors$ = this.adminAuthorsGql
    .watch({ skip: this.skip, limit: this.pageSize })
    .valueChanges.pipe(
     map(result => result.data.authorsWithPaginate)
);
}

authorEdit(id) {
    this.table.display = this.table.display === false;
    console.log('authorEdit:',id, this.table.display);
    return this.authors$;
  }

authorDelete(id) {
    this.table.display = this.table.display === false;
    console.log('authorDelete:',id, this.table.display);
    return this.authors$;
    }

onRowClicked(row) {
    console.log('Row clicked: ', row);    
    }

}
...