DIP*_*MAN 6 amazon-web-services reactjs graphql aws-appsync aws-amplify
我正在为我的 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
}
Run Code Online (Sandbox Code Playgroud)
这是我在 AppSync 中生成的 GraphQL 架构。我能够查询数据并创建变异,但更新和删除变异不起作用。
mutation UpdateAddressInput {
updateAddress(input:{
id:"af5cd7e6-8213-48b6-aa8e-9d2d6999742c",
area:"New Delhi"
}){
id,
area
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个示例查询,您可以在下面看到返回数据,它没有反映在 dynamoDB 中
{
"data": {
"updateAddress": {
"id": "af5cd7e6-8213-48b6-aa8e-9d2d6999742c",
"area": "Electronic City"
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使使用 AWS Amplify,我也更喜欢转到 AppSync 控制台并选择查询和变更,这样您就可以看到正在发生的事情以及在幕后进行变更所需的内容。
下面是从 AppSync 控制台中提取的更新突变示例 - 而不是使用codegen来自 Amplify。
input UpdateRuralAddressInput {
id: ID!
latitude: String
longitude: String
stateCity: String
status: Int
_version: Int
}
Run Code Online (Sandbox Code Playgroud)
请注意,需要字段版本才能进行更新。
我只是指出它来帮助其他人解决这个问题,这就是我在Lambdas中使用 Amplify + Appsync 客户端的方式
因此,在应用任何更新更改之前,执行“get by 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 } });
Run Code Online (Sandbox Code Playgroud)
请注意,我的查询和更新更改都必须包含该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
}
}`;
Run Code Online (Sandbox Code Playgroud)
您\xe2\x80\x99 将在 dynamoDB 表中看到自动生成的字段 \xe2\x80\x9c_version\xe2\x80\x9d。查看要更新区域字段的ID的_version值。当您获取地址时,您将得到一个字段“_version”。\n尝试这样的事情
\n\nmutation UpdateAddressInput { \n updateAddress(input:{\n id:"af5cd7e6-8213-48b6-aa8e-9d2d6999742c",\n area:"New Delhi",\n\n _version:variable name // _version value in dynamoDB table for \n // the desired row.\n\n }){\n id, \n area \n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2389 次 |
| 最近记录: |