Skip to content

Instantly share code, notes, and snippets.

@waiholiu
Created February 8, 2020 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waiholiu/2651c6b324687e0e8c8b6085272d00ef to your computer and use it in GitHub Desktop.
Save waiholiu/2651c6b324687e0e8c8b6085272d00ef to your computer and use it in GitHub Desktop.
example of how to access graphql using apollo
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