I am new to Apollo. Issue i am facing is that On initial (Mount/Render) network call is made and on prop change it isn't.
Following is simplified structure of my application.
<Component (has filters in state)>
<QueryComponent filters = {...filters} (passed to query)>
<Table onChange/>
</QueryComponent>
<Component/>
QueryComponent Code
export const QueryComponent = ({ callback, filter }) => {
// Data transformation from filter to vars(expected by Query)
return (
<Query
handleLoading
query={query}
returnPartialData
variables={vars}
fetchPolicy="network-only"
callback={callback}
getData={function}
entityType="xx"
/>
);
};
Query returns ApolloQuery
<ApolloQuery
returnPartialData={returnPartialData}
partialRefetch={partialRefetch}
{...rest}
>
{
({
data,
loading,
}) => {
if (loading) {
return handleLoading
? (
<Loading />
)
: callback({
loading,
});
}
const queryData = data && getData(data);
const hasValidData = !!queryData && !!Object
.values(queryData)
.filter((val) => !!val)
.length;
if (!hasValidData) {
return passThruMissingData
? callback({
loading,
...queryData,
})
: (
<EntityNotFound
type={entityType}
/>
);
}
let strippedData = { ...queryData };
const isValueAnArray = Object.values(strippedData)[0] instanceof Array;
if (isValueAnArray) {
strippedData = transform(
strippedData,
(result, value, key) => {
value.forEach(deepStripInvalid);
// eslint-disable-next-line no-param-reassign
result[key] = value;
},
);
} else {
deepStripInvalid(strippedData);
}
return callback({
...strippedData,
});
}
}
</ApolloQuery>
and QueryComponent
has a Wrapper that has Query as ApolloQuery
form react-apollo
and it returns loader when loading is true.
In <Table/>
component I have handler that updates the filters in Component which flows down into <QueryComponent />
On initial render Filters are passed down and i can see that network call has been made and loading state was true for a second and then change to false. When I interact with table onChange is called which updates the filters and they are passed to Query, but loading state return false and there is no network call.
I have tried to set fetchPolicy="network-only"
and to fetchPolicy="no-cache"
as well and there is still no network call.
Note: Currently there is no data on backend so initial query returns an empty array.
Behaviour I am expecting: When filters are changed query is called again and there should be a network call.
Note2: If I forcefully unmount and remount the <QueryComponent>
then network request is made, but i would prefer to use Apollo's loading state to handle that.
react-apollo: "version": "2.5.8"
apollo: "version": "2.21.0"
Edited to include more details.
QueryComponent
code? It seems it is likely only making fetches incomponentDidMount
versus also doing so incomponentDidUpdate
, so OFC remounting it fetches data. – Drew ReeseQuery
andApolloQuery
in another page where data is actually returned from the query and it behaves properly i.e loading istrue
and thenfalse
. Also i was logging the loading above if(loading) inApolloQuery
. It means that on each change i am reaching there but somehow ApolloQuery is not making a network call. I have edited and updated the question. – noumanniazi