为什么 apollo-angular 查询返回空数组?
Why is apollo-angular Query returning empty array?
我最近安装并设置了 apollo-angular,但无法从查询中获取数据。我提到了处理类似问题的 ,但它们没有帮助。我可以确定的是:
- 我已经成功测试了一个查询,所以我知道 Apollo 和 Hasura 正在通信。
- 授权 headers 通过附加来自 Auth0 的令牌的拦截器将请求附加到 graphql 端点;网络请求选项卡显示 200,除了空数组外一切看起来都很正常。
- 查询 returns 我在 Hasura 上的 Graphiql 工具上使用它时的预期结果 back-end。
- 在 back-end 中的 table 上设置了权限,以便可以访问。
- 没有可用的控制台错误或其他反馈。
我希望从此查询中获取字符串值,并且当我记录响应数据的结果时,我得到的只是一个带有 table 名称的空数组标签。组件代码:
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”约束修复了问题。
我最近安装并设置了 apollo-angular,但无法从查询中获取数据。我提到了处理类似问题的
- 我已经成功测试了一个查询,所以我知道 Apollo 和 Hasura 正在通信。
- 授权 headers 通过附加来自 Auth0 的令牌的拦截器将请求附加到 graphql 端点;网络请求选项卡显示 200,除了空数组外一切看起来都很正常。
- 查询 returns 我在 Hasura 上的 Graphiql 工具上使用它时的预期结果 back-end。
- 在 back-end 中的 table 上设置了权限,以便可以访问。
- 没有可用的控制台错误或其他反馈。
我希望从此查询中获取字符串值,并且当我记录响应数据的结果时,我得到的只是一个带有 table 名称的空数组标签。组件代码:
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”约束修复了问题。