私はApolloとVueでGraphQLを使用し始めているので、 "ばかげた"質問かもしれませんが、どうやって行うのか分かりません。GraphQL vue apolloのクエリと突然変異同じビュー
どのように私は、同じビュー内で、クエリは一つのオブジェクトを取得し、変異
を使用して、それを更新するために行うことができますのは、私はシンプルなスキーマ
type Product {
id: ID!
title: String!
description: String
}
そしてVUEコンポーネント
を持っているとしましょう<script>
// GraphQL query
const ProductQuery = gql `
query($id: ID){
Product(id: $id)
{
id
title
description
}
}
`;
const UpdateProductQuery = gql `
mutation updateProduct($id: ID!, $title: String!, $description: String) {
updateProduct(
id: $id,
title: $title,
description: $description,
) {
id
}
}
`;
export default {
data() {
return {
Product: {},
};
},
apollo: {
Product: {
query: ProductQuery,
variables() {
id: 1234,
};
},
},
methods: {
updateProduct() {
this.$apollo.mutate({
mutation: UpdateProductQuery,
variables: {
id: this.Product.id,
title: this.Product.title,
description: this.Product.description,
},
})
}
}
};
</script>
ここでテンプレート部分をどのように書きますか? Productオブジェクトを入力内のvモデルにリンクすることはできますか?
<template>
<section>
<input v-model="product.title"></input>
<input v-model="product.description"></input>
<button @click="updateProduct">Update</button>
</section>
</template>
ありがとうございました。
ご返信いただきありがとうございます。はい、私は実際に小文字に変更しました。しかし、私のupdateProduct()メソッドでは、 'this.product'に新しい値がありません。私が何か間違っていると確信していない場合 – Jerome
@Jerome、私は私の答えを更新しました。私は、「反応性パラメータ」の使用が鍵だと思います。 – MattDionis