如何在 laravel lighthouse 和 vue apollo 中创建嵌套关系
How to create nested relationships in laravel lighthouse and vue apollo
我在 vue apollo 中有以下变化
mutation (
$title: String!
$image: String!
$author: String!
$description: String!
$link: String!
$featured: Boolean
$category: { connect: Int! }
)
{
createBook(
input: {
title: $title
image: $image
author: $author
description: $description
link: $link
featured: $featured
category: $category
}
) {
id
title
author
}
}
我只需要在这个对象 {connect: $categoryId} 中引入 categoryId。
我还在 laravel lighthouse 上问了关于 GitHub 的问题,但不幸的是,答案对我帮助不大。
mutation (
$categoryId: Int!
) {
createBook(
input: {
category: {
connect: $categoryId
}
) {}
}
如果我的问题不是很清楚,我可以在 Github here
上提供完整的回购协议
更新问题在这里
AddBook.gql
mutation(
$title: String!
$image: String!
$author: String!
$description: String!
$link: String!
$featured: Boolean
$category: Int!
) {
createBook(
input: {
title: $title
image: $image
author: $author
description: $description
link: $link
featured: $featured
category: { connect: $categoryId } // $categoryId undefined
}
) {
id
title
author
}
}
我在 AddBook.vue
中使用这个突变
<div class="form-group">
<ApolloQuery
:query="require('@/graphql/queries/Categories.gql')"
class="move-down"
>
<template slot-scope="{ result: { data, loading } }">
<!-- Some content -->
<div v-if="loading">Loading...</div>
<select v-else v-model="categoryId">
<option
v-for="category of data.categories"
:key="category.id"
:value="category.id"
>
{{ category.name }}
</option>
</select>
</template>
</ApolloQuery>
方法中
addBook() {
this.$apollo
.mutate({
// Query
mutation: addBook,
// Parameters
variables: {
$title: this.title,
$image: this.image,
$author: this.author,
$description: this.description,
$link: this.link,
$featured: this.featured,
$categoryId: this.categoryId, // here is the problem
},
})
.then((data) => {
// Result
console.log(data);
})
.catch((error) => {
// Error
console.error(error);
});
},
提前致谢
变量应如下所示:
addBook() {
this.$apollo
.mutate({
// Query
mutation: addBook,
// Parameters
variables: {
title: this.title,
image: this.image,
author: this.author,
description: this.description,
link: this.link,
featured: this.featured,
categoryId: this.categoryId, // here is the problem
},
})
.then((data) => {
// Result
console.log(data);
})
.catch((error) => {
// Error
console.error(error);
});
}
我在 vue apollo 中有以下变化
mutation (
$title: String!
$image: String!
$author: String!
$description: String!
$link: String!
$featured: Boolean
$category: { connect: Int! }
)
{
createBook(
input: {
title: $title
image: $image
author: $author
description: $description
link: $link
featured: $featured
category: $category
}
) {
id
title
author
}
}
我只需要在这个对象 {connect: $categoryId} 中引入 categoryId。
我还在 laravel lighthouse 上问了关于 GitHub 的问题,但不幸的是,答案对我帮助不大。
mutation (
$categoryId: Int!
) {
createBook(
input: {
category: {
connect: $categoryId
}
) {}
}
如果我的问题不是很清楚,我可以在 Github here
上提供完整的回购协议更新问题在这里
AddBook.gql
mutation(
$title: String!
$image: String!
$author: String!
$description: String!
$link: String!
$featured: Boolean
$category: Int!
) {
createBook(
input: {
title: $title
image: $image
author: $author
description: $description
link: $link
featured: $featured
category: { connect: $categoryId } // $categoryId undefined
}
) {
id
title
author
}
}
我在 AddBook.vue
中使用这个突变 <div class="form-group">
<ApolloQuery
:query="require('@/graphql/queries/Categories.gql')"
class="move-down"
>
<template slot-scope="{ result: { data, loading } }">
<!-- Some content -->
<div v-if="loading">Loading...</div>
<select v-else v-model="categoryId">
<option
v-for="category of data.categories"
:key="category.id"
:value="category.id"
>
{{ category.name }}
</option>
</select>
</template>
</ApolloQuery>
方法中
addBook() {
this.$apollo
.mutate({
// Query
mutation: addBook,
// Parameters
variables: {
$title: this.title,
$image: this.image,
$author: this.author,
$description: this.description,
$link: this.link,
$featured: this.featured,
$categoryId: this.categoryId, // here is the problem
},
})
.then((data) => {
// Result
console.log(data);
})
.catch((error) => {
// Error
console.error(error);
});
},
提前致谢
变量应如下所示:
addBook() {
this.$apollo
.mutate({
// Query
mutation: addBook,
// Parameters
variables: {
title: this.title,
image: this.image,
author: this.author,
description: this.description,
link: this.link,
featured: this.featured,
categoryId: this.categoryId, // here is the problem
},
})
.then((data) => {
// Result
console.log(data);
})
.catch((error) => {
// Error
console.error(error);
});
}