停留在 aws amplify API.graphql(...) 调用以运行查询,查询在 graphql 操场上工作

And*_*son 6 javascript async-await reactjs graphql aws-amplify

更新:我已经在最底部添加了模式并生成了以前工作过的查询,似乎添加 _version 似乎破坏了某些东西,我不知道为什么。

我无法完全弄清楚这一点。我收到以下错误 "Cannot return null for non-nullable field Recipe._version."

它以前可以工作,但我使用了 aws 放大了新的可视化数据建模工具并添加到了成分模型中。Ran amplify pull 似乎破坏了我的代码,但不确定为什么。

任何帮助都会很棒。

谢谢

注意:我能够在 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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

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}")
}
Run Code Online (Sandbox Code Playgroud)

这是由 amplify mock 生成的 queries.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
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

因此,在我使用数据模型 ui 编辑器添加成分模型之前,我有以下正在运行的 schmea 和 querie.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}")
}
Run Code Online (Sandbox Code Playgroud)

查询.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
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)