Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SubscribeToMore refetching bug #7007

Closed
Jonatthu opened this issue Sep 11, 2020 · 1 comment
Closed

SubscribeToMore refetching bug #7007

Jonatthu opened this issue Sep 11, 2020 · 1 comment

Comments

@Jonatthu
Copy link

Jonatthu commented Sep 11, 2020

I already tried everything on many issues from:
#6340

This is happening in the latest: "@apollo/client": "3.1.3",

	const messagesQuery = useQuery<MSSGetMessages, MSSGetMessagesVariables>(MSS_GET_MESSAGES, {
		notifyOnNetworkStatusChange: true,
		fetchPolicy: 'network-only',
		variables: { profileId: authState.profileId! },
	});

React.useEffect(() => {

	messagesQuery.subscribeToMore<MSSMessagesSubscription>({
		document: MSS_MESSAGES_SUBSCRIPTION,
		updateQuery: (prev, { subscriptionData }) => {
			if (!subscriptionData.data) return prev;

			const newMessage = subscriptionData.data.onCreateDateChatMessage;
			const chats = new List(prev.dateChats?.items?.slice());
			const targetChatIndex = prev.dateChats?.items?.findIndex(
				(x) => x?.id === newMessage?.dateChatId
			)!;
			const targetChat = chats.ElementAt(targetChatIndex);
			chats.RemoveAt(targetChatIndex);

			if (targetChat) {
				const newTargetChat: MSSGetMessages_dateChats_items = {
					...targetChat,
					messages: {
						...targetChat.messages,
						items: [
							...targetChat.messages?.items!,
							{
								id: newMessage?.id!,
								message: newMessage?.message!,
							},
						],
					},
				};

				return {
					...prev,
					dateChats: {
						...prev.dateChats,
						items: [newTargetChat, ...chats.ToArray()],
					},
				};
			} else {
				// Chat does not exist reload.
				return { dateChats: null };
			}
		},
	});
}, []);

If I comment out

							{
								id: newMessage?.id!,
								message: newMessage?.message!,
							},

The query does not refetch again since is the same data.

So the real question is:

How can I modify that object in order to not trigger a refetch in that query.

@Jonatthu Jonatthu changed the title SubscribeToMore reloading bug SubscribeToMore refetching bug Sep 11, 2020
@Jonatthu
Copy link
Author

Removing
notifyOnNetworkStatusChange: true,
fetchPolicy: 'network-only',

Solves my issue

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant