首页 > 解决方案 > 为什么阿波罗角查询返回空数组?

问题描述

我最近安装并设置了 apollo-angular,但无法从查询中获取数据。我提到了其他处理类似问题的问题,但它们没有帮助。我可以肯定的是:

我希望从此查询中获取字符串值,并且我记录了响应数据的结果,我得到的只是一个带有表名的空数组 labeleld。组件代码:

import { Component, OnInit, OnDestroy } from '@angular/core';

import { Apollo } from 'apollo-angular';
import gql from 'graphql-tag';

const getGoal = gql`
  query GetGoal($survey_id: String!) {
    goals(where: { survey_id: { _eq: $survey_id } }) {
      survey_id
      goal
    }
  }
`;

@Component({
  selector: 'app-goals',
  templateUrl: './goals.component.html',
  styleUrls: ['./goals.component.css']
})
export class GoalsComponent implements OnInit {
  loading: boolean;
  goal: any;

  constructor(private apollo: Apollo) {}

  ngOnInit() {
    this.apollo
      .watchQuery<any>({
        query: getGoal,
        variables: {
          survey_id: 'test_survey'
        }
      })
      .valueChanges.subscribe(({ data, loading }) => {
        this.loading = loading;
        this.goal = data.goals.goal;
      });
  }
}

标签: angulargraphqlapollohasuraapollo-angular

解决方案


这里的问题是权限配置不正确,所以如果您遇到类似的问题,请检查您的权限,确保嵌套表正确连接等:我没有过多处理嵌套权限,所以我不知道是否有解决这个问题的更有效的方法,但是向user_id我正在查询的表添加一个字段,并添加标准的“user_id _eq x-hasura-user-id”约束解决了这个问题。


推荐阅读