Skip to content

Instantly share code, notes, and snippets.

@SlimenTN
Last active May 6, 2020 14:20
Show Gist options
  • Save SlimenTN/242f49f59ae451686d23f676b2a562e4 to your computer and use it in GitHub Desktop.
Save SlimenTN/242f49f59ae451686d23f676b2a562e4 to your computer and use it in GitHub Desktop.
GraphQL example with Angular
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