Вызовите другой запрос на завершение одного запроса в клиенте apollo, используя реагировать родной - PullRequest
1 голос
/ 04 мая 2020

Я получаю недопустимый перехват вызова для вложенных запросов enter 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)
            }
    })

, а usereadNotifications приходит от

export const readNotifications = gql` mutation readNotifications($notificationIds: [String]!) {
       readNotifications(notificationIds: $notificationIds) 
} `; 
export const usereadNotifications = (options?: QueryHookOptions) => ( 
     useMutation(readNotifications, options) 
); 

1 Ответ

1 голос
/ 04 мая 2020

Поскольку usereadNotifications использует хук useMutation, вы не можете заключить его в функцию и попытаться выполнить ее условно, поскольку это нарушает правила хуков

Однако useMutation возвращает вам функцию, которая позволяет вам вызвать функцию для запуска мутации

Так что используйте его как

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)
        }
})
...