【问题标题】:Apollo Angular watchQuery returns no results or errorsApollo Angular watchQuery 不返回任何结果或错误
【发布时间】:2020-01-25 23:20:00
【问题描述】:

我决定将由ng add apollo-angular 创建的GraphQLModule 封装到我们自己的Angular 共享模块中。

https://www.apollographql.com/docs/angular/basics/setup/#installation-with-angular-schematics

在此模块内执行查询时,我收到来自服务器的响应。

但是,当在另一个应用程序中使用此模块时,我们没有收到任何响应或错误:

  myQuery = gql`
    {
      member {
        id
        details {
          id
        }
      }
    }
  `

  queryOptions: WatchQueryOptions = {query: this.myQuery, errorPolicy: 'all'}
  myQueryObs: Observable<any>

  constructor(private apollo: Apollo) {}

  ngOnInit() {
    this.myQueryObs = this.apollo.watchQuery<Query>(this.queryOptions)
    .valueChanges.pipe(map (results => {
      if (results.errors) {
        console.log('ERRORS!!! YAY!!!') // NEVER GET HERE
      }
      console.log('RESULTS') // NEVER GET HERE EITHER
    }));
  }

我已经记录了它的废话,无法确定它为什么根本没有响应。它的行为就像 Apollo 根本没有实例化,但控制台输出证明并非如此。

非常感谢任何建议或帮助!

【问题讨论】:

    标签: angular module apollo apollo-angular


    【解决方案1】:

    好的,答案很简单。上面的示例返回一个observable,这就是没有结果的原因。我仍然需要subscribe 到那个observable 才能得到结果。因此,要获得结果,最好:

       this.apollo
          .watchQuery(this.queryOptions)
          .valueChanges.subscribe((result: any) => {
            console.log('++++++++ RESULTS +++++++++', result)
          });
    

    【讨论】:

      猜你喜欢
      • 2018-05-01
      • 2020-12-25
      • 2018-09-12
      • 2020-08-03
      • 1970-01-01
      • 2017-07-05
      • 2021-05-22
      • 2012-01-25
      • 2021-06-23
      相关资源
      最近更新 更多