Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call another query onComplete of one query in apollo client using react native

I am getting invalid hook call for nested queriesenter image description here

const fetchNotifications = useNotificationsQuery({
        variables: {
            skip: SKIP,
            take: TAKE,
        },
        async onCompleted(data){
            let ids:Array<string>=[]
            data?.notifications?.forEach((e)=>{
                ids.push(e?.id+"")
            })
            setIds(ids)
            readNotifications()
        }
    });

    const readNotifications =()=> usereadNotifications({
        variables: { notificationIds: ids},
            async onCompleted(data){
                console.log("res"+data)
            }
    })

and usereadNotifications comes from

export const readNotifications = gql` mutation readNotifications($notificationIds: [String]!) {
       readNotifications(notificationIds: $notificationIds) 
} `; 
export const usereadNotifications = (options?: QueryHookOptions) => ( 
     useMutation(readNotifications, options) 
); 
like image 784
Krutika Chotara Avatar asked Jan 19 '26 19:01

Krutika Chotara


1 Answers

Since usereadNotifications uses a hook useMutation, you can't wrap it into a function and try to execute it conditionally as it breaks the rules of hooks

However useMutation returns you a function which allows you to call the function to trigger mutation

So use it like

const fetchNotifications = useNotificationsQuery({
    variables: {
        skip: SKIP,
        take: TAKE,
    },
    async onCompleted(data){
        let ids:Array<string>=[]
        data?.notifications?.forEach((e)=>{
            ids.push(e?.id+"")
        })
        setIds(ids)
        readNotifications()
    }
});

const [readNotifications] = usereadNotifications({
    variables: { notificationIds: ids},
        async onCompleted(data){
            console.log("res"+data)
        }
})
like image 78
Shubham Khatri Avatar answered Jan 21 '26 10:01

Shubham Khatri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!