onError in GraphQL mutations

前端 未结 1 1167
南方客
南方客 2021-01-23 09:58

I was trying onError for graphql mutations and realised that they don\'t work properly:

https://github.com/apollographql/apollo-client/issues/5708

What else can

相关标签:
1条回答
  • 2021-01-23 10:35

    You could use onError callback instead of checking for addingContactError property from the result directly on function body like shown below

    const _onCreateUserRelationError = React.useCallback((error: ApolloError) => {
        console.log('this is the error', error);
        Alert.alert(error.message.includes('already exists') ? 'Contact Already Exists' : 'Unable to Add Contact');
    }, []);
    
    const [
        createUserRelationMutation,
        {
            data: addingContactData,
            loading: addingContactLoading,
            called: isMutationCalled,
        },
    ] = useCreateUserRelationMutation({
        onCompleted: () => {
            Alert.alert('Contact Added');
        },
        onError: _onCreateUserRelationError
    });
    

    Note: Memoize the component using React.memo to avoid unnecessary re-rendering of this component

    0 讨论(0)
提交回复
热议问题