I've following component, which has a date variable. On each re render, date variable is getting updated. Now the the problem is as I've assigned date to a query variable, graphql fetches again and again infinitely. I have debugged the code and found apollo is Observing on date variable, when it is receiving a new value, it is to re.
import React from 'react';
import { getISODate } from '../../dateUtils';
import { useQuery } from '@apollo/react-hooks';
import { GET_EXPENSE_STATUS } from '../../queries';
import get from 'lodash/get';
const ExpenseStatus = (props) => {
const date = getISODate(); // returns current date as ISO String Format
const { loading, error, data } = useQuery(GET_EXPENSE_STATUS, {
variables: {
date
}
});
if (error) return <p>Error :(</p>;
return(
<div>
{get(data, 'expenseStatus.value')}
</div>
);
};
I also tried with useLazyQuery. But no luck.
const date = getISODate(); // returns current date in ISO String
const [loadExpenseStatus, { loading, error, data }] = useLazyQuery(GET_EXPENSE_STATUS, {
variables: {
date
}
});
useEffect(() => {
if(!called) {
loadExpenseStatus();
}
}, []);
So, Is there any way, I can skip this Observer? I just want to receive the fetch call once.