在Apollo Angular中,如果watchQuery没有返回结果或错误,可以按照以下步骤进行解决:
import { ApolloModule, Apollo } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
@NgModule({
imports: [
ApolloModule,
HttpLinkModule
]
})
export class AppModule {
constructor(
apollo: Apollo,
httpLink: HttpLink
) {
apollo.create({
link: httpLink.create({ uri: 'your-graphql-endpoint' }),
cache: new InMemoryCache()
});
}
}
import { Apollo } from 'apollo-angular';
@Component({
// ...
})
export class YourComponent {
constructor(private apollo: Apollo) { }
}
import { Apollo } from 'apollo-angular';
import { WatchQueryOptions } from 'apollo-client';
import { YourQuery } from './your-query-file';
@Component({
// ...
})
export class YourComponent {
constructor(private apollo: Apollo) { }
ngOnInit() {
const queryOptions: WatchQueryOptions = {
query: YourQuery, // 你的GraphQL查询
variables: {
// 可选的查询变量
}
};
this.apollo.watchQuery(queryOptions)
.valueChanges
.subscribe(({ data, loading, error }) => {
if (loading) {
// 显示加载状态
}
if (data) {
// 处理数据
}
if (error) {
// 处理错误
}
});
}
}
确保GraphQL查询在服务端正确执行,并返回了预期的结果。可以使用GraphQL Playground或其他工具进行测试。
通过检查这些步骤,你可以解决Apollo Angular的watchQuery没有返回结果或错误的问题。