尽管突变包含在 gql
标记中,但我还是收到以下错误:
Invariant Violation: Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql
此问题仅由下面的突变代码引起,我有一个有效的查询。
代码:
<script>
import gql from 'graphql-tag'
export default {
apollo: {
createTask: {
mutation: gql`
mutation Task(
$taskName: String!
$taskDesc: String!
) {
setSession(
taskName: $taskName
taskDesc: $taskDesc
) {
id
taskName
}
}
`,
variables() {
return {
taskName: this.res.data.task_name,
taskDesc: this.res.data.task_description,
}
},
},
},
data() {
return {
createTask: '',
}
},
}
<script>
最佳答案
Smart queries使用 apollo
对象声明并在组件安装时运行。如果您希望以这种方式运行查询,那么您的代码将如下所示:
export default {
apollo: {
someQuery: gql`...`,
}
}
但是,在处理突变时,您通常不希望在挂载时运行它们。相反,您希望执行突变以响应某些用户操作,例如单击按钮。所以上面的语法不适用于突变。相反,您需要直接在您的方法之一内调用 this.$apollo.mutate
:
export default {
methods: {
createTask() {
this.$apollo.mutate({
mutation: gql`...`,
variables: {...},
// other options
}).then(result => {
// do something with the result
})
}
}
}
关于graphql - 不变违规 : Expecting a parsed GraphQL document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62520976/