Hi there, I’ve been using Apollo for a while, but still haven’t figured out the below problem…
The query I’m executing is the below in multiple components with different source id.
export const GET_SOURCE_DETAILS_CONDITIONAL: TypedDocumentNode<{
sourceDetails: sourceDetailExtended[];
}> = gql`
${SOURCE_DETAIL_FIELDS}
query GET_SOURCE_DETAILS_CONDITIONAL($where: sourceDetails_bool_exp!) {
sourceDetails(where: $where) {
...SourceDetailFields
}
}
`;
const { data } = useQuery(GET_SOURCE_DETAILS_CONDITIONAL, {
variables: {
where: {
sourceId: {
_eq: source.id,
},
},
},
});
And after I execute an insert mutation, call cache.modify, I get the problem where the added items gets attached to all the queries (GET_SOURCE_DETAILS_CONDITIONAL).
update(cache, { data }) {
let insertedSoureDetails = (data.insert_sourceDetails?.returning ||
[]) as sourceDetail[];
if (data.insert_sources_one) {
insertedSoureDetails.concat(data.insert_sources_one);
}
cache.modify({
fields: {
sourceDetails(existing = []) {
const newSourceRefs = insertedSoureDetails.map(sd => {
return cache.writeFragment({
data: sd,
fragment: SOURCE_DETAIL_FIELDS,
fragmentName: "SourceDetailFields",
});
});
return [...existing, ...newSourceRefs];
},
},
});
},
above is the logic I use to update cache and below is the fragment for the query
export const SOURCE_DETAIL_CORE_FIELDS = gql`
fragment SourceDetailCoreFields on sourceDetails {
id
sourceId
companyId
name
}
`;
export const SOURCE_DETAIL_FIELDS = gql`
${SOURCE_DETAIL_CORE_FIELDS}
fragment SourceDetailFields on sourceDetails {
...SourceDetailCoreFields
source {
id
name
isSourceCompany
companyId
}
company {
id
name
}
}
`;
At the moment I have to do this in order to filter incorrectly added data
const sourceDetails = (data?.sourceDetails || []).filter(
sd => sd.sourceId == source.id
);
However, as you can expect, once the where filter gets complicated, it’ll be prone to too many mistakes and is surly an unnecessary step.
I tried implementing keyargs, keyfields in the typePolicies in various ways but none seem to help with my situation…
If anyone can help and shed some light on it, I’d appreciate it dearly.