在React Native项目中向Amplify API添加模拟时出现错误。

huangapple go评论82阅读模式
英文:

Error when adding mock to Amplify API in React Native project

问题

我目前正在开发一个React Native项目,其中我正在使用AWS Amplify作为我的后端API。我正在尝试为测试目的添加一个模拟的Amplify API,但在使用以下命令时遇到了错误:

amplify mock api

导致出现以下错误消息:

在/***/***/***/***/amplify/backend/api/***/schema.graphql处编辑模式或在/***/***/***/***/amplify/backend/api/***/schema目录中放置.graphql文件
无法启动API模拟。运行清理任务。
原因:错误:在resolvers/Query.syncCouriers.req.vtl的第11行50字符处解析错误
在第12行上解析错误:
...tx.stash.authFilter.or ) #if( $fil
-----------------------^
期望 'ID'、'CONTENT',得到 '||' 

我不确定是什么导致了这个错误,以及如何解决它。我已经检查了我的模式文件,并确保它格式正确。我还尝试在指定的目录中创建一个单独的 .graphql 文件,但错误仍然存在。

package.json:

{
  "name": "ubereatsuser",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "1.17.11",
    "@react-native-community/netinfo": "9.3.5",
    "@react-native-picker/picker": "2.4.8",
    "@react-navigation/bottom-tabs": "^6.3.1",
    "@react-navigation/material-bottom-tabs": "^6.2.1",
    "@react-navigation/material-top-tabs": "^6.2.1",
    "@react-navigation/native": "^6.0.10",
    "@react-navigation/native-stack": "^6.6.1",
    "aws-amplify": "^4.3.20",
    "aws-amplify-react-native": "^6.0.4",
    "expo": "~47.0.0",
    "expo-status-bar": "~1.4.4",
    "graphql": "^14.7.0",
    "react": "18.1.0",
    "react-dom": "18.1.0",
    "react-native": "0.70.8",
    "react-native-maps": "1.3.2",
    "react-native-pager-view": "6.0.1",
    "react-native-paper": "^4.12.0",
    "react-native-safe-area-context": "4.4.1",
    "react-native-screens": "~3.18.0",
    "react-native-tab-view": "^3.1.1",
    "react-native-vector-icons": "^9.1.0",
    "react-native-web": "~0.18.10"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/react": "~18.0.27",
    "@types/react-native": "~0.64.12",
    "typescript": "^4.9.4"
  },
  "private": true
}

schema.graphql:

enum TransportationModes {
  DRIVING
  BICYCLING
}

type Courier @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  sub: String!
  lat: Float
  lng: Float
  transportationMode: TransportationModes!
}

type Basket @model @auth(rules: [{allow: public}]) {
  id: ID!
  BasketDishes: [BasketDish] @hasMany(indexName: "byBasket", fields: ["id"])
  userID: ID! @index(name: "byUser")
  restaurantID: ID! @index(name: "byRestaurant")
}

enum OrderStatus {
  NEW
  COOKING
  READY_FOR_PICKUP
  PICKED_UP
  COMPLETED
  ACCEPTED
}

type OrderDish @model @auth(rules: [{allow: public}]) {
  id: ID!
  quantity: Int!
  Dish: Dish @hasOne
  orderID: ID! @index(name: "byOrder")
}

type Order @model @auth(rules: [{allow: public}]) {
  id: ID!
  userID: ID! @index(name: "byUser")
  Restaurant: Restaurant @hasOne
  total: Float!
  status: OrderStatus!
  OrderDishes: [OrderDish] @hasMany(indexName: "byOrder", fields: ["id"])
  Courier: Courier @hasOne
}

type BasketDish @model @auth(rules: [{allow: public}]) {
  id: ID!
  quantity: Int!
  Dish: Dish @hasOne
  basketID: ID! @index(name: "byBasket")
}

type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  address: String!
  lat: Float!
  lng: Float!
  Orders: [Order] @hasMany(indexName: "byUser", fields: ["id"])
  Baskets: [Basket] @hasMany(indexName: "byUser", fields: ["id"])
  sub: String!
}

type Dish @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  image: String
  description: String
  price: Float!
  restaurantID: ID! @index(name: "byRestaurant")
}

type Restaurant @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  image: String!
  deliveryFee: Float!
  minDeliveryTime: Int!
  maxDeliveryTime: Int!
  rating: Float
  address: String!
  lat: Float!
  lng: Float!
  Dishes: [Dish] @hasMany(indexName: "byRestaurant", fields: ["id"])
  Baskets: [Basket] @hasMany(indexName: "byRestaurant", fields: ["id"])
}

感谢您的帮助!

英文:

I'm currently working on a React Native project where I'm using AWS Amplify for my backend API. I am trying to add a mock to my Amplify API for testing purposes, but I encountered an error using this command:

amplify mock api

resulting in this error message:

Edit your schema at 
/***/***/***/***/amplify/backend/api/***/schema.graphql or place .graphql files in a directory at /***/***/***/***/amplify/backend/api/***/schema
Failed to start API Mocking. Running cleanup tasks.
Reason: Error: Parse error on resolvers/Query.syncCouriers.req.vtl:11:50
Parse error on line 12:
...tx.stash.authFilter.or ) #if( $fil
-----------------------^
Expecting 'ID', 'CONTENT', got '||' 

I'm not sure what exactly is causing this error and how to resolve it. I have already checked my schema file and ensured that it is properly formatted. I also tried creating a separate .graphql file in the specified directory, but the error persists.

package.json:

{
  "name": "ubereatsuser",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "1.17.11",
    "@react-native-community/netinfo": "9.3.5",
    "@react-native-picker/picker": "2.4.8",
    "@react-navigation/bottom-tabs": "^6.3.1",
    "@react-navigation/material-bottom-tabs": "^6.2.1",
    "@react-navigation/material-top-tabs": "^6.2.1",
    "@react-navigation/native": "^6.0.10",
    "@react-navigation/native-stack": "^6.6.1",
    "aws-amplify": "^4.3.20",
    "aws-amplify-react-native": "^6.0.4",
    "expo": "~47.0.0",
    "expo-status-bar": "~1.4.4",
    "graphql": "^14.7.0",
    "react": "18.1.0",
    "react-dom": "18.1.0",
    "react-native": "0.70.8",
    "react-native-maps": "1.3.2",
    "react-native-pager-view": "6.0.1",
    "react-native-paper": "^4.12.0",
    "react-native-safe-area-context": "4.4.1",
    "react-native-screens": "~3.18.0",
    "react-native-tab-view": "^3.1.1",
    "react-native-vector-icons": "^9.1.0",
    "react-native-web": "~0.18.10"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/react": "~18.0.27",
    "@types/react-native": "~0.64.12",
    "typescript": "^4.9.4"
  },
  "private": true
}

schema.graphql:

enum TransportationModes {
  DRIVING
  BICYCLING
}

type Courier @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  sub: String!
  lat: Float
  lng: Float
  transportationMode: TransportationModes!
}

type Basket @model @auth(rules: [{allow: public}]) {
  id: ID!
  BasketDishes: [BasketDish] @hasMany(indexName: "byBasket", fields: ["id"])
  userID: ID! @index(name: "byUser")
  restaurantID: ID! @index(name: "byRestaurant")
}

enum OrderStatus {
  NEW
  COOKING
  READY_FOR_PICKUP
  PICKED_UP
  COMPLETED
  ACCEPTED
}

type OrderDish @model @auth(rules: [{allow: public}]) {
  id: ID!
  quantity: Int!
  Dish: Dish @hasOne
  orderID: ID! @index(name: "byOrder")
}

type Order @model @auth(rules: [{allow: public}]) {
  id: ID!
  userID: ID! @index(name: "byUser")
  Restaurant: Restaurant @hasOne
  total: Float!
  status: OrderStatus!
  OrderDishes: [OrderDish] @hasMany(indexName: "byOrder", fields: ["id"])
  Courier: Courier @hasOne
}

type BasketDish @model @auth(rules: [{allow: public}]) {
  id: ID!
  quantity: Int!
  Dish: Dish @hasOne
  basketID: ID! @index(name: "byBasket")
}

type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  address: String!
  lat: Float!
  lng: Float!
  Orders: [Order] @hasMany(indexName: "byUser", fields: ["id"])
  Baskets: [Basket] @hasMany(indexName: "byUser", fields: ["id"])
  sub: String!
}

type Dish @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  image: String
  description: String
  price: Float!
  restaurantID: ID! @index(name: "byRestaurant")
}

type Restaurant @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  image: String!
  deliveryFee: Float!
  minDeliveryTime: Int!
  maxDeliveryTime: Int!
  rating: Float
  address: String!
  lat: Float!
  lng: Float!
  Dishes: [Dish] @hasMany(indexName: "byRestaurant", fields: ["id"])
  Baskets: [Basket] @hasMany(indexName: "byRestaurant", fields: ["id"])
}
 ````
Thank you for your help!


</details>


# 答案1
**得分**: 5

我通过将我的Amplify版本降级到`" @aws-amplify/cli/@12.0.0`"来解决了这个问题。

首先,我使用`npm uninstall -g @aws-amplify/cli`卸载了当前版本,然后重新安装了较旧的版本`npm install -g @aws-amplify/cli@12.0.0`。

我还遵循了[Amplify页面](https://docs.amplify.aws/cli/usage/mock/)上的建议,建议使用openjdk 16.0.1。仅仅Java版本本身并没有解决问题,但拥有这个版本的JDK和Amplify CLI版本后,我现在能够成功进行模拟。

<details>
<summary>英文:</summary>

I was able to resolve this on my end by downgrading my amplify version to `&quot;@aws-amplify/cli/@12.0.0`.

First I uninstalled my current version with `npm uninstall -g @aws-amplify/cli` and then re-installed an older version `npm install -g @aws-amplify/cli@12.0.0`. 

I had also followed the advice on the [amplify page](https://docs.amplify.aws/cli/usage/mock/) that says to use openjdk 16.0.1. The java version by itself didn&#39;t resolve the issue - but having both this version of jdk and this version of amplify-cli I am no able to mock successfully.

</details>



huangapple
  • 本文由 发表于 2023年7月11日 04:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定