AWS Appsync Graphql 查询获取项目列表 returns 空数组,即使 dynamodb table 中有项目

AWS Appsync Graphql query to get list of items returns empty array even though the dynamodb table has items in it

我是 运行 一个 Angular 11 应用程序,它使用 GraphQL 和 dynamoDB 作为后端与 AWS Amplify 和 Appsync 集成。

这是我的 Graphql 模式:-

type School
  @model
  @auth(
    rules: [{ allow: owner, ownerField: "admins", operations: [update, read] }]
  ) {
  id: ID!
  name: String!
  admins: [Member]
  classes: [Class] @connection(name: "SchoolClasses")
  members: [Member] @connection(name: "SchoolMembers")
}

type Class
  @model
  @auth(
    rules: [{ allow: owner, ownerField: "admins", operations: [update, read] }]
  ) {
  id: ID!
  name: String!
  school: School @connection(name: "SchoolClasses")
  admins: [Member]
  members: [Member] @connection(name: "ClassMembers")
}

type Member @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  school: School @connection(name: "SchoolMembers")
  class: Class @connection(name: "ClassMembers")
}

这是我的客户定义:-

const client = new AWSAppSyncClient({
  url: awsconfig.aws_appsync_graphqlEndpoint,
  region: awsconfig.aws_appsync_region,
  auth: {
    type: awsconfig.aws_appsync_authenticationType,
    jwtToken: async () =>
      (await Auth.currentSession()).getAccessToken().getJwtToken(),
  },
  complexObjectsCredentials: () => Auth.currentCredentials(),
  cacheOptions: {
    dataIdFromObject: (obj: any) => `${obj.__typename}:${obj.myKey}`,
  },
});

这是我的查询方式:-

    client
      .query({
        query: ListSchools,
      })
      .then((data: any) => {
        console.log('data from listSchools ', data);
        console.log(data.data.listSchools.items);
      });
  };

这是我的查询定义:-

import gql from 'graphql-tag';

export default gql`
  query ListSchools(
    $filter: ModelSchoolFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listSchools(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        name
        admins {
          id
          name
          createdAt
          updatedAt
          owner
        }
        classes {
          nextToken
        }
        members {
          nextToken
        }
        createdAt
        updatedAt
      }
      nextToken
    }
  }
`;

控制台中的数据输出如下所示:-

{
   "data":{
      "listSchools":{
         "items":[],
         "nextToken":null,
         "__typename":"ModelSchoolConnection"
      }
   },
   "loading":false,
   "networkStatus":7,
   "stale":false
}

如您所见,items 是一个空数组。但目前我的 dynamoDB 中有 3 个项目 table:-

我做错了什么?

我已经检查了区域以查看它是否正在查询不同的区域,但它正在检查正确的区域,所以我应该会看到结果。另外,如果我们查询错误 table?

,它不会抛出错误吗?

我想通了。问题出在 GraphQL Schema 定义中,我将 @auth 参数设置为仅允许特定管理员访问列表,这就是我返回空数组的原因。我删除了 @auth 参数,它现在返回正确的项目列表。