【问题标题】:Angular Apollo client returning bad requestAngular Apollo 客户端返回错误请求
【发布时间】:2019-06-25 17:36:30
【问题描述】:

我正在为 Angular 练习 Graphql 和 Apollo Client。

我在从客户端创建查询时遇到问题。

我已经在后端成功创建了 graphql 查询。

下面是我的 GQL 查询,它返回数据并显示在 Playground

GQL 查询

{
  getData {
    data {
      type
    }
}

我的客户端代码,

Service.ts

export class ContentService {
  constructor(    private apollo: Apollo,
    ) {
      this.getContent();
    }

   async getContent(): Promise<any> {
    const response: any = await this.apollo.query({query: getContentQuery}).toPromise();
    return console.log(response);
  }
}

请告诉我我在这里做错了什么。

无法找到解决方案答案。请帮忙。

【问题讨论】:

    标签: angular graphql apollo-client


    【解决方案1】:

    所以,这是您在客户端的查询:

    import gql from 'graphql-tag';
    
    export const getContentQuery = gql`
      query getContent {
        metadata {
          type
        }
      }
    

    query getContent 是您在代码中为查询提供的名称,但它不是发送到后端的实际“查询名称”。 我认为您必须执行以下操作:

    import gql from 'graphql-tag';
    
    export const getContentQuery = gql`
      query getContent {
        getContent {
          metadata {
            type
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2019-02-14
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 2015-09-24
      相关资源
      最近更新 更多