[英]How to change Method type of API in apollo-angular
我正在使用Apollo角度客戶端在angular中調用API,我的一些API是POST類型,有些是GET類型,並且我想根據API(GET或POST)更改API方法類型。 我的代碼:
export class AppModule {
constructor(
apollo: Apollo,
private httpLink: HttpLink
) {
const auth = setContext((_, { headers }) => {
const token = sessionStorage.getItem('token');
if (!token) {
return {};
} else {
return {
headers: headers.append('Authorization', token)
};
}
});
const http = this.httpLink.create({ uri: '/api/graph', method: 'GET' });
apollo.create({
link: auth.concat(http),
cache: new InMemoryCache()
});
}
}
api方法(獲取類型):
constructor(private router: Router, private apollo: Apollo, private appService: AppService) { } login() { this.apollo.query({query: MyQuery }) .subscribe((data) => { if (data['data']) { console.log(err); } }, err => { console.log(err); }); }
API方法(POST類型)代碼:
this.apollo.query({query:MyQuery}).subscribe((data)=> {console.log(data);},err => {console.log(err);});
我無法調用方法2(POST類型方法),因為實例創建為GET類型,並且我想根據我的API方法類型進行更改
請在這件事上給予我幫助..
您想使用get進行Graphql查詢嗎? 還是只想發出獲取請求? 在這種情況下,您應該使用httpclientmodule
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.