angular - 为什么阿波罗角查询返回空数组?
问题描述
我最近安装并设置了 apollo-angular,但无法从查询中获取数据。我提到了其他处理类似问题的问题,但它们没有帮助。我可以肯定的是:
- 我已经成功测试了一个查询,所以我知道 Apollo 和 Hasura 正在通信。
- 授权标头通过附加来自 Auth0 的令牌的拦截器将请求附加到 graphql 端点;网络请求选项卡显示 200,除了空数组外,一切看起来都很正常。
- 当我在 Hasura 后端的 Graphiql 工具上使用该查询时,该查询返回预期结果。
- 权限在后端的表上设置,以便可以访问。
- 没有控制台错误或其他反馈可用。
我希望从此查询中获取字符串值,并且我记录了响应数据的结果,我得到的只是一个带有表名的空数组 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;
});
}
}
解决方案
这里的问题是权限配置不正确,所以如果您遇到类似的问题,请检查您的权限,确保嵌套表正确连接等:我没有过多处理嵌套权限,所以我不知道是否有解决这个问题的更有效的方法,但是向user_id
我正在查询的表添加一个字段,并添加标准的“user_id _eq x-hasura-user-id”约束解决了这个问题。
推荐阅读
- javascript - 在 Javascript 端保存处理程序之前计算 vTiger 字段(内部代码)
- reactjs - 过多的项目渲染
- javascript - 角度材质 - 对材质表进行排序后,动画过渡开始状态未更新
- unity3d - 在 Unity 的多选下拉菜单中显示 ScriptableObjects
- reactjs - 传递参数 onClick, React
- android - 唯一约束失败:type.ps
- yii2 - Yii2 使用 ajax 提交模态表单
- java - 在 jooq 3.11.11 试用版的 META-INF/maven/plugin.xml 中找不到插件描述符
- android - 跳尺的制作方法
- android - 如何在 Android 上显示来自 Base64 字符串的 PDF?