Link to home
Start Free TrialLog in
Avatar of nicolas_maujean
nicolas_maujean

asked on

how to wait a graphql query is finished using apollo?

I have the following code :

  ngOnInit() {

    this.data = this.apollo.query({ query: ResidentQuery }).subscribe(({data, loading}) => {
      this.data = data;
      this.loading = loading;
    });

    if (!this.loading) {
      // using this.data
    }
   }
I want data to be loaded before processed themm after the (!this.loading). It is not the case as loading is asynchronous. How I can wait the data is loaded before using them ?

I am making a graphql query using apollo client. ResidentQuery is a string containing the graphql query.

Thank you for your feedbacks !
Avatar of leakim971
leakim971
Flag of Guadeloupe image

What about :
ngOnInit() {

	var p = new Promise((resolve, reject) => {
		this.data = this.apollo.query({ query: ResidentQuery }).subscribe(({data, loading}) => {
			resolve(data,loading);
		});
	});

	this.loading = loading;

	p.then((_data, _loading) => {
		this.loading = !loading;
		// using _data
	});

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nicolas_maujean
nicolas_maujean

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nicolas_maujean
nicolas_maujean

ASKER

used it and it did what I want