Last active
May 6, 2020 14:20
-
-
Save SlimenTN/242f49f59ae451686d23f676b2a562e4 to your computer and use it in GitHub Desktop.
GraphQL example with Angular
This file contains hidden or 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, OnInit } from '@angular/core'; | |
import { Apollo } from 'apollo-angular'; | |
import gql from 'graphql-tag'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent implements OnInit { | |
blogs: any[] = []; | |
constructor( | |
private _apollo: Apollo | |
){} | |
ngOnInit(){ | |
this._apollo | |
.watchQuery({ | |
query: gql` | |
{ | |
blogs{ | |
edges{ | |
node{ | |
id | |
title | |
description | |
author{ | |
id | |
lastName | |
firstName | |
} | |
} | |
} | |
} | |
} | |
` | |
}) | |
.valueChanges.subscribe((res: any) => { | |
console.log({res}); | |
let edges: any[] = res.data.blogs.edges; | |
this.blogs = edges.map(item => item.node) | |
}) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment