Javascript 停留在aws amplify API上。graphql(…)调用运行查询,查询在graphql中工作

Javascript 停留在aws amplify API上。graphql(…)调用运行查询,查询在graphql中工作,javascript,reactjs,async-await,graphql,aws-amplify,Javascript,Reactjs,Async Await,Graphql,Aws Amplify,更新:我已经在最底层添加了之前工作的模式和生成查询,添加_版本似乎破坏了一些东西,我不知道为什么 我不太明白这一点。我得到以下错误 “对于不可为null的字段配方,不能返回null。\u版本。” 这是工作之前,但我使用aws放大了新的可视化数据建模工具,并添加在成分模型。运行放大拉,它似乎打破了我的代码,但不确切地知道为什么 任何帮助都会很好 谢谢 注意:我能够在graphql中很好地运行查询和突变 我有以下代码 //... import { listRecipes as ListRecipes

更新:我已经在最底层添加了之前工作的模式和生成查询,添加_版本似乎破坏了一些东西,我不知道为什么

我不太明白这一点。我得到以下错误
“对于不可为null的字段配方,不能返回null。\u版本。”

这是工作之前,但我使用aws放大了新的可视化数据建模工具,并添加在成分模型。运行放大拉,它似乎打破了我的代码,但不确切地知道为什么

任何帮助都会很好

谢谢

注意:我能够在graphql中很好地运行查询和突变

我有以下代码

//...
import { listRecipes as ListRecipes } from "./graphql/queries";


async function getData() {
    try {
      const recipeData = await API.graphql(graphqlOperation(ListRecipes));
      console.log("listData:", recipeData);
    } catch (err) {
      console.log("error fetching recipes...", err);
    }
  }
graphql模式

type Ingredient
  @model
  @auth(rules: [{ allow: private }])
  @key(name: "byRecipe", fields: ["recipeID"]) {
  id: ID!
  amount: String
  ingredient: String
  recipeID: ID!
}

type Recipe @model @auth(rules: [{ allow: private }]) {
  id: ID!
  userId: ID
  name: String!
  description: String!
  recipe: String!
  comments: [Comment] @connection(name: "RecipeComments")
  original_author: String
  Ingredients: [Ingredient] @connection(keyName: "byRecipe", fields: ["id"])
}

type Comment @model @auth(rules: [{ allow: private }]) {
  id: ID!
  message: String
  createdBy: String
  recipe: Recipe @connection(name: "RecipeComments")
}

type Test {
  id: String!
  name: String!
  price_usd: String!
}

type Query {
  getTest(limit: Int, start: Int): [Test] @function(name: "test-${env}")
}
下面是由amplify mock生成的querys.js文件

export const listRecipes = /* GraphQL */ `
  query ListRecipes(
    $filter: ModelRecipeFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listRecipes(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        userId
        name
        description
        recipe
        original_author
        _version
        _deleted
        _lastChangedAt
        createdAt
        updatedAt
      }
      nextToken
      startedAt
    }
  }
`;
因此,在我使用数据模型ui编辑器添加配料模型之前,我有下面的schmea和querye.js文件正在工作

模式

type Recipe @model {
  id: ID!
  userId: ID
  name: String!
  description: String!
  recipe: String!
  comments: [Comment] @connection(name: "RecipeComments")
}

type Comment
  @model
  @auth(rules: [{ allow: owner, queries: null, ownerField: "createdBy" }]) {
  id: ID!
  message: String
  createdBy: String
  recipe: Recipe @connection(name: "RecipeComments")
}

type Test {
  id: String!
  name: String!
  price_usd: String!
}

type Query {
  getTest(limit: Int, start: Int): [Test] @function(name: "test-${env}")
}
querys.js

export const listRecipes = /* GraphQL */ `
  query ListRecipes(
    $filter: ModelRecipeFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listRecipes(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        userId
        name
        description
        recipe
        createdAt
        updatedAt
      }
      nextToken
    }
  }
`;

DataStore
是否已成为该项目的一部分?IIRC是它用来同步的“私有”字段之一。我认为,虽然您可以使用GraphQL读取数据存储管理的存储层,但如果必须这样做,那么如果您使用的是数据存储,就不应该使用常规GraphQL写入数据。我不知道您有多少个这样的对象,但是您现在可以手动为
\u version
插入一个随机值,并且您应该能够使用常用的数据存储API(
DataStore.query(Recipe)
)列出对象。我在项目中没有使用数据存储。我遵循这段代码来构建我所拥有的。您是否建议迁移到数据存储?就为
\u version
添加随机值而言,仅针对数据存储,我尝试创建了一个变异,其中我向
\u version
添加了一个值,但我的变异不起作用。奇怪的是,在graphql游乐场中,一切正常,但在我的前端,它没有看到recipe对象。在阅读数据存储文档后,它似乎是用于支持脱机数据创建的,现在我不需要它,所以我不认为它是解决我遇到的问题的正确解决方案。只需删除这个(
\u version
)querys.js中的字段?…可能某些现有数据库条目没有定义此字段…只是不询问/查询此字段OK,但在我编写amplify mock时会自动生成查询?