Created
February 8, 2020 04:23
-
-
Save waiholiu/2651c6b324687e0e8c8b6085272d00ef to your computer and use it in GitHub Desktop.
example of how to access graphql using apollo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { AngularFireAuth } from '@angular/fire/auth'; | |
import { HttpClient, HttpHeaders } from '@angular/common/http'; | |
import gql from 'graphql-tag'; | |
import { Apollo } from 'apollo-angular'; | |
const gqlTestHelloWorld = gql` | |
query{ | |
helloworld(newArg: 21) | |
} | |
`; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.scss'] | |
}) | |
export class AppComponent { | |
title = 'paperbetClient'; | |
constructor(private afa: AngularFireAuth, private http: HttpClient,private apollo: Apollo) { | |
this.afa.idToken.subscribe((data) => { | |
console.log(data); | |
this.callServer(data); | |
}) | |
} | |
onLogOut() { | |
this.afa.auth.signOut(); | |
} | |
private callServer(token: string) { | |
const headers = new HttpHeaders().set("Authorization", "Bearer " + token); | |
this.http | |
.get<any>('https://localhost:5001/weatherforecast', { headers: headers }).subscribe(d => { | |
console.log(d); | |
}); | |
} | |
onClick() { | |
this.apollo | |
.watchQuery({ | |
query: gqlTestHelloWorld, | |
fetchPolicy: 'network-only' | |
}) | |
.valueChanges.subscribe(result => { | |
console.log(result.data); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment