简体   繁体   English

Apollo useLazyQuery 在完成时监听

[英]Apollo useLazyQuery listen on completion

I am using graphql with Apollo for a signup page.我将 graphql 与 Apollo 一起用于注册页面。 onSubmit I want to send a query to confirm if the username is unique (currently counting users, I know I should check for exists, but not the scope of this question) onSubmit我想发送一个查询来确认用户名是否唯一(目前正在计算用户,我知道我应该检查是否存在,但不是这个问题的 scope)

I have several problems doing that.我这样做有几个问题。 Using useQuery sends the Query instantly - I cant choose to send the query on submit.使用useQuery会立即发送查询 - 我不能选择在提交时发送查询。 Using useLazyQuery sends the Query delayed on calling user_count .使用useLazyQuery会在调用user_count时发送延迟查询。 -> Problem: It is neither a promise nor does onComplete work (a function that exists in useQuery and executes once the query finished) -> 问题:它既不是 promise 也不是onComplete工作(一个 function 存在于useQuery并在查询完成后执行)

How does useLazyQuery work? useLazyQuery是如何工作的? It executes the query.它执行查询。 The values are accessible through data_user which gets updated once the query finishes.这些值可以通过data_user访问,一旦查询完成就会更新。 data_user is initally undefined . data_userundefined的。

As useLazyQuery does not return a promise, I decided to create my own promise-like listener.由于useLazyQuery不返回 promise,我决定创建自己的类似 Promise 的侦听器。 For what I know the recommended way to do it is a Proxy .据我所知,推荐的方法是Proxy The problem with that is: Trying to set target= data_user throws问题是:尝试设置target= data_user抛出

Cannot create proxy with a non-object as target or handler

I could solve it with a setCallback function that looks every couple MS for a change, but that feels dirty.我可以用setCallback function 来解决它,它看起来每对 MS 都有变化,但感觉很脏。


export default function Signup() {
    const [state, setState] = useState([])
    const [user_count, {loading, data: data_user}] = useLazyQuery(USER_COUNT);

    const handleSubmit = event => {
        event.preventDefault();

        const proxy = new Proxy(data_user, {
            set: function (target, prop, value) {
                if (prop === 'userCount') {
                    console.log("Query finished")
                    const user_exists = data_user.userCount !== 0;
                    console.log(`User exists: ${user_exists}`)
                }
            }
        })

        user_count({
            variables: {
                name: state.value
            }
        });

I was able to workaround this problem by using useApolloClient directly:我可以通过直接使用useApolloClient来解决这个问题:

const client = useApolloClient();

const handleSubmit = event => {
        event.preventDefault();

        client
            .query({
                query: USER_COUNT,
                variables: {
                    name: state.value
                }
            })
            .then(response => {
                const user_exists = response.data.userCount !== 0;
                console.log(`User already exists: ${user_exists}`)
            });
    }
    ```

You can use the Window Abort controller to cancel the request您可以使用 Window Abort controller 取消请求

Learn more here: https://evilmartians.com/chronicles/aborting-queries-and-mutations-in-react-apollo在此处了解更多信息: https://evilmartians.com/chronicles/aborting-queries-and-mutations-in-react-apollo

at the end of article check the complete example hope it will help you out在文章末尾检查完整的示例希望它对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM