Appsync更新和删除突变不起作用

时间:2020-03-26 22:57:05

标签: reactjs amazon-web-services graphql aws-amplify aws-appsync

我正在为我的APP使用AppSync。这是我的Schema.graphql文件的快速浏览

type Item @model
  @auth(rules: [
    { allow: public, provider: apiKey, operations: [read] },
    { allow: groups, groups: ["admin"] }
  ]) {
  id: ID!
  name: String!
  soldUnits: String!
  soldAs: [Float!]
  price: Float!
  isAvailable: Boolean!
  availableAmount: Float!
  category: String!
  isVeg: Boolean!
  offer: [String!]
  about: String
  storage: String
  benifits: String
  otherInfo: String
  rating: Int
  keywords: [String!]
  reviews: [String]
  images: [String]
  updatedBy: String
  addedBy: String
  qty: Int
  inventroy: Inventory @connection(name: "ItemInventory")
}

type User @model {
  id: ID!
  firstName: String!
  lastName: String!
  about: String
  phoneNumber: String
  email: String
  countryCode: String
  addresses: [Address] @connection
  orders: [Order] @connection(name: "OwnOrder")
}

type CartItem {
    id: ID!
    count: Int!
    name: String!
    price: Float
}

type Address @model {
  id: ID!
  lat: Float!
  long: Float!
  fullAddress: String!
  apartment: String!
  street: String
  area: String!
  district: String!
  city: String
  state: String!
  zip: Int!
  country: String!
  phoneNumber: String!
}

type InventoryAddress @model {
  id: ID!
  address: Address @connection
  inventroy: Inventory! @connection(name: "InventoryAddress")
}

type Order @model {
  id: ID!
  orderdate: AWSDate!
  ordertime: AWSTime!
  deliverySlot: String!
  deliveryDate: AWSDate!
  deliveryInput: String!
  deliveryPhoneNumber: String!
  deliveryPhoneCountryCode: String!
  deliveryAddress: String!
  deliveredTime: AWSDate
  deliveredDate: AWSDate
  deliveryCharges: Float!
  totalAmount: Float!
  paidAmount: Float!
  discount: Float!
  unpaidAmount: Float!
  status: String!
  paymentMethod: String!
  paymentId: String!
  itemsCount: Int!
  items: [CartItem]
  deliverAssignedTo: Patron @connection(name: "PatronOrder")
  owner: User @connection(name: "OwnOrder")
  inventroyDetails: Inventory @connection(name: "InventoryOrder")
}

type Patron @model {
  id: ID!
  age: Int!
  firstName: String!
  lastName: String!
  about: String
  workTime: String
  rating: Int!
  reviews: [String]
  addressProof: String!
  role: String
  workArea: Address @connection
  address: Address @connection
  deliveries: [Order] @connection(name: "PatronOrder")
  inventroyDetails: Inventory! @connection(name: "PatronInventory")
  addedItems: [Item] @connection
}

type Inventory @model {
  id: ID!
  address: InventoryAddress @connection(name: "InventoryAddress")
  patron: [Patron] @connection(name: "PatronInventory")
  items: [Item] @connection(name: "ItemInventory")
  orders: [Order] @connection(name: "InventoryOrder")
  manager: Patron @connection
  admins: [Patron] @connection
}

这是我在AppSync中生成的GraphQL模式。 我可以查询数据并创建突变,但是更新和删除突变无效。

mutation UpdateAddressInput {
  updateAddress(input:{
    id:"af5cd7e6-8213-48b6-aa8e-9d2d6999742c",
   area:"New Delhi"
  }){
    id,
    area
  }
}

这是一个示例查询,如您在下面看到的返回数据所示,它没有反映在dynamoDB中

{
  "data": {
    "updateAddress": {
      "id": "af5cd7e6-8213-48b6-aa8e-9d2d6999742c",
      "area": "Electronic City"
    }
  }
}

2 个答案:

答案 0 :(得分:1)

您将在dynamoDB表中看到一个自动生成的字段“ _version”。请参阅_version的值以获取要更新的ID区域字段。获取地址时,您将获得一个字段“ _version”。 尝试这样的事情

mutation UpdateAddressInput {  
    updateAddress(input:{
     id:"af5cd7e6-8213-48b6-aa8e-9d2d6999742c",
     area:"New Delhi",

     _version:variable name // _version value in dynamoDB table for                                
                            //  the desired row.

    }){
    id, 
    area   
  }
}

答案 1 :(得分:0)

即使使用AWS Amplify,我也更喜欢转到AppSync控制台并选择查询和突变,这样您就可以看到正在发生的事情以及在幕后进行突变所需要的东西。

下面是从AppSync控制台提取的更新突变示例,而不是使用Amplify的codegen

input UpdateRuralAddressInput {
    id: ID!
    latitude: String
    longitude: String
    stateCity: String
    status: Int
    _version: Int
}

请注意,必须使用字段版本进行更新。

我只是指出要帮助其他人解决此问题,这就是我在Lambdas中使用Amplify + Appsync客户端的方法

因此,在应用任何更新变异之前,请执行“通过ID获取” query,并相应地更新来自请求正文的模型。这样,不仅版本字段将保持正常,其他方面也将保持正常。

Lambda处理程序函数中的更新示例。 注意:您需要删除属性__typename

    const updateRuralAddressRequest = JSON.parse(event.body);
    
    const client = await appsyncClient.hydrated();

    const ruralAddressQuery = await client.query({ query: gql(queryRuralAddress), variables: { id: updateRuralAddressRequest.id } });
    const ruralAddressFromDb = ruralAddressQuery.data.getRuralAddress;


    const updateRuralAddressInput = { ...ruralAddressFromDb, ...updateRuralAddressRequest };
    delete updateRuralAddressInput.__typename;

    const data = await client.mutate({ mutation: gql(updateRuralAddressMutation), variables: { updateRuralAddressInput } });

请注意,我的查询和更新突变都必须包含version字段。

export const queryRuralAddress = `
  query GetRuralAddressByID($id: ID!) {
    getRuralAddress(id: $id) {
      id,
      latitude,
      longitude,
      stateCity,
      status,
      _version
    }
  }
`;

export const updateRuralAddressMutation = `
    mutation UpdateRuralAddress($updateRuralAddressInput:UpdateRuralAddressInput!) {
      updateRuralAddress(input: $updateRuralAddressInput) {
        id
        latitude
        longitude
        stateCity
        status,
        _version
      }
    }`;