Skip to content

Commit 00425ac

Browse files
committed
integration of Simple GraphQL service with Angular 6 code
1 parent 7659670 commit 00425ac

11 files changed

+269
-20
lines changed

package-lock.json

+139-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
"@angular/platform-browser": "^6.0.3",
2121
"@angular/platform-browser-dynamic": "^6.0.3",
2222
"@angular/router": "^6.0.3",
23+
"apollo-angular": "^1.1.2",
24+
"apollo-angular-link-http": "^1.1.1",
25+
"apollo-cache-inmemory": "^1.3.0-beta.6",
26+
"apollo-client": "^2.3.7",
2327
"core-js": "^2.5.4",
28+
"graphql": "^0.13.2",
29+
"graphql-tag": "^2.9.2",
2430
"rxjs": "^6.0.0",
2531
"zone.js": "^0.8.26"
2632
},

src/app/app.component.html

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@
33
<h1>
44
Welcome to {{ title }}!
55
</h1>
6-
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
6+
<app-friend-list></app-friend-list>
77
</div>
8-
<h2>Here are some links to help you start: </h2>
9-
<ul>
10-
<li>
11-
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
12-
</li>
13-
<li>
14-
<h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a></h2>
15-
</li>
16-
<li>
17-
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
18-
</li>
19-
</ul>
8+
9+
2010

src/app/app.module.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33

44
import { AppComponent } from './app.component';
5+
import { FriendListComponent } from './friend-list/friend-list.component';
6+
import { GraphQLModule } from './graphql.module';
57

68
@NgModule({
79
declarations: [
8-
AppComponent
10+
AppComponent,
11+
FriendListComponent
912
],
1013
imports: [
11-
BrowserModule
14+
BrowserModule,
15+
GraphQLModule
1216
],
1317
providers: [],
1418
bootstrap: [AppComponent]

src/app/friend-list/friend-list.component.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div>
2+
<div *ngIf="user">
3+
User JSON : {{user}}
4+
</div>
5+
<div *ngIf="company">
6+
Company JSON : {{company}}
7+
</div>
8+
</div>
9+
<div>
10+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { FriendListComponent } from './friend-list.component';
4+
5+
describe('FriendListComponent', () => {
6+
let component: FriendListComponent;
7+
let fixture: ComponentFixture<FriendListComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ FriendListComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(FriendListComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Apollo } from 'apollo-angular';
3+
4+
import { GET_FRIENDS } from './../graphql';
5+
6+
@Component({
7+
selector: 'app-friend-list',
8+
templateUrl: './friend-list.component.html',
9+
styleUrls: ['./friend-list.component.css']
10+
})
11+
export class FriendListComponent implements OnInit {
12+
13+
user: any;
14+
company: any;
15+
apollo: Apollo;
16+
constructor(apollo: Apollo) {
17+
this.apollo = apollo;
18+
}
19+
20+
ngOnInit() {
21+
this.friendsList = [];
22+
this.apollo.watchQuery({
23+
query: GET_FRIENDS
24+
}).valueChanges.subscribe((response) => {
25+
// 5
26+
if (response.data.user) {
27+
this.user = JSON.stringify(response.data.user);
28+
}
29+
if (response.data.company) {
30+
this.company = JSON.stringify(response.data.company);
31+
}
32+
});
33+
}
34+
35+
}

src/app/graphql.module.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { NgModule } from '@angular/core';
2+
import { HttpClientModule } from '@angular/common/http';
3+
4+
import { Apollo, ApolloModule } from 'apollo-angular';
5+
import { HttpLink, HttpLinkModule } from 'apollo-angular-link-http';
6+
import { InMemoryCache } from 'apollo-cache-inmemory';
7+
8+
import { ENDPOINT } from './graphql';
9+
10+
11+
12+
@NgModule({
13+
exports: [
14+
HttpClientModule,
15+
ApolloModule,
16+
HttpLinkModule
17+
]
18+
})
19+
export class GraphQLModule {
20+
constructor(apollo: Apollo, httpLink: HttpLink) {
21+
const uri = ENDPOINT;
22+
const http = httpLink.create({ uri });
23+
24+
apollo.create({
25+
link: http,
26+
cache: new InMemoryCache()
27+
});
28+
}
29+
}

src/app/graphql.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import gql from 'graphql-tag';
2+
3+
export const GET_FRIENDS = gql`
4+
{
5+
user(id: "1") {
6+
firstName
7+
age
8+
id
9+
}
10+
}
11+
`;
12+
13+
14+
export const ENDPOINT = 'http://localhost:4000/graphql';

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"lib": [
1616
"es2017",
1717
"dom"
18-
]
18+
],
19+
"skipLibCheck": true
1920
}
2021
}

0 commit comments

Comments
 (0)