فراخوانی GraphQL API در یک کلاینت Angular
نویسنده: ابوالفضل روشن ضمیر
تاریخ: ۱۳۹۸/۰۴/۲۴ ۱:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
ng new apollo-angular-project
ng add apollo-angular
const uri = 'https://localhost:5001/graphql';
npm install --save apollo-angular \ apollo-angular-link-http \ apollo-link \ apollo-client \ apollo-cache-inmemory \ graphql-tag \ graphql
{
"compilerOptions": {
// ...
"lib": [
"es2017",
"dom",
"esnext.asynciterable"
]
}
} import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from "@angular/common/http";
import { ApolloModule, APOLLO_OPTIONS } from "apollo-angular";
import { HttpLinkModule, HttpLink } from "apollo-angular-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
@NgModule({
declarations:
[
AppComponent
],
imports:
[
BrowserModule,
HttpClientModule,
ApolloModule,
HttpLinkModule
],
providers:
[
{
provide: APOLLO_OPTIONS,
useFactory: (httpLink: HttpLink) => {
return {
cache: new InMemoryCache(),
link: httpLink.create({
uri: "https://localhost:5001/graphql"
})
}
},
deps:
[
HttpLink
]
}],
bootstrap:
[
AppComponent
]
})
export class AppModule { } export type OwnerInputType = {
name: string;
address: string;
} export type AccountType = {
'id': string;
'description': string;
'ownerId' : string;
'type': string;
} import { AccountType } from './accountType';
export type OwnerType = {
'id': string;
'name': string;
'address': string;
'accounts': AccountType[];
} ng g s graphql
import { Injectable } from '@angular/core';
import { Apollo } from 'apollo-angular';
import gql from 'graphql-tag';
@Injectable({
providedIn: 'root'
})
export class GraphqlService {
constructor(private apollo: Apollo) { }
} public getOwners = () => {
return this.apollo.query({
query: gql`query getOwners{
owners{
id,
name,
address,
accounts{
id,
description,
type
}
}
}`
});
} export class AppComponent implements OnInit {
public owners: OwnerType[];
public loading = true;
constructor(private graphQLService: GraphqlService) { }
ngOnInit() {
this.graphQLService.getOwners().subscribe(result => {
this.owners = result.data["owners"] as OwnerType[];
this.loading = result.loading;
});
}
} <div>
<div *ngIf="!this.loading">
<table>
<thead>
<tr>
<th>
#
</th>
<th>
نام و نام خانوادگی
</th>
<th>
آدرس
</th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let item of this.owners;let idx=index" [ngTemplateOutlet]="innertable"
[ngTemplateOutletContext]="{item:item, index:idx}"></ng-container>
</tbody>
</table>
</div>
<div *ngIf="this.loading">
<p>
در حال بارگذاری لیست ...
</p>
</div>
</div>
<ng-template #innertable let-item="item" let-idx="index">
<tr>
<td>{{idx+1}}</td>
<td>{{item.name}}</td>
<td>{{item.address}}</td>
</tr>
<tr *ngIf="this.item.accounts && this.item.accounts.length > 0">
<td colspan="4">
<div>
<p>Accounts</p>
</div>
<div>
<table>
<thead>
<tr>
<th>#</th>
<th>نوع</th>
<th>توضیحات</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let innerItem of this.item.accounts;let innerIndex=index">
<td>
{{innerIndex+1}}
</td>
<td>
{{innerItem.type}}
</td>
<td>
{{innerItem.description}}
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</ng-template> dotnet restore dotnet run
ng serve
public getOwners = () => {
return this.apollo.watchQuery<any>({
query: gql`query getOwners{
owners{
id,
name,
address,
accounts{
id,
description,
type
}
}
}`
})
} export class AppComponent implements OnInit {
loading: boolean;
public owners: OwnerType[];
private querySubscription: Subscription;
constructor(private graphQLService: GraphqlService) { }
ngOnInit() {
this.querySubscription = this.graphQLService.getOwners()
.valueChanges
.subscribe(result => {
this.loading = result.loading;
this.owners = result.data["owners"] as OwnerType[];
});
}
ngOnDestroy() {
this.querySubscription.unsubscribe();
}
} public getOwner = (id) => {
return this.apollo.query({
query: gql`query getOwner($ownerID: ID!){
owner(ownerId: $ownerID){
id,
name,
address,
accounts{
id,
description,
type
}
}
}`,
variables: { ownerID: id }
})
} public createOwner = (ownerToCreate: OwnerInputType) => {
return this.apollo.mutate({
mutation: gql`mutation($owner: ownerInput!){
createOwner(owner: $owner){
id,
name,
address
}
}`,
variables: { owner: ownerToCreate }
})
} public updateOwner = (ownerToUpdate: OwnerInputType, id: string) => {
return this.apollo.mutate({
mutation: gql`mutation($owner: ownerInput!, $ownerId: ID!){
updateOwner(owner: $owner, ownerId: $ownerId){
id,
name,
address
}
}`,
variables: { owner: ownerToUpdate, ownerId: id }
})
} public deleteOwner = (id: string) => {
return this.apollo.mutate({
mutation: gql`mutation($ownerId: ID!){
deleteOwner(ownerId: $ownerId)
}`,
variables: { ownerId: id }
})
}