不能传递变量给useMutation的函数。(vue apollo, composition api, graphql)

问题描述 投票:0回答:0

我正在开发一个前端程序,前端堆栈: Vue3、Vue 路由器、apollo 客户端。 Vue apollo 的 composition api.

在我的组件脚本中: 我有这样的突变。

const USER_MUTATION = gql`
    mutation deleteUser($username: String, $customer_id: String) {
        deleteUser (username: $username, customer_id: $customer_id) {
        customer_id 
    }
  }
`

然后在我的设置函数中,我有这样的代码。

setup() {
    const customer = ref(0)
    const username = ref('')

    const { mutate, onDone, onError } = useMutation(USER_MUTATION)

    const handleSubmit = () => {
        console.log(customer.value);
        console.log(username.value);
        mutate({
            variables: {
                username: username.value,
                customer_id: customer.value 
            }
        })
        .then(() => {
            console.log("success");
        });
    }

    onDone((result1) => {
        console.log(result);
    })

    onError((error) => {
        console.log(error);
    }) 
 
}

我发现我在尝试时无法将变量(用户名和 customer_id)传输到 mutate()。 我得到了错误:

ApolloError: Internal server error
    at new ApolloError (index.ts:89:1)
    at eval (QueryManager.ts:257:1)
    at both (asyncMap.ts:30:1)
    at eval (asyncMap.ts:19:1)
    at new Promise (<anonymous>)
    at Object.then (asyncMap.ts:19:1)
    at Object.eval [as next] (asyncMap.ts:31:1)
    at notifySubscription (module.js:132:1)
    at onNotify (module.js:176:1)
    at SubscriptionObserver.next (module.js:225:1)

我该怎么办? 非常感谢。

我试过运行 useMutation,除非我在 USER_MUTATION 定义中对变量进行硬编码,否则它无法工作。

我希望有人能为我建议一个合适的解决方案。

非常感谢。

graphql vuejs3 apollo vue-composition-api
© www.soinside.com 2019 - 2024. All rights reserved.