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

perf(gqwery): remove filter for entries #248

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 11 additions & 36 deletions packages/gqwery/src/cache/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ export const makePluck = (typePolicies: GraphQLTypePolicy) => {
store.set(key, next);
}

const entityReferences = Object.entries(currentEntity)
.filter(
([_, value]) =>
isEntity(value) ||
(Array.isArray(value) && value.some(isEntity)),
)
.flatMap(([key, value]) => {
const entityReferences = Object.entries(currentEntity).flatMap(
([key, value]) => {
if (Array.isArray(value)) {
return value.map((entity, idx) => ({
parent: value,
Expand All @@ -94,7 +89,8 @@ export const makePluck = (typePolicies: GraphQLTypePolicy) => {
parentProperty: key,
entity: value,
};
});
},
);

return recur([...rest, ...entityReferences], entityKey ?? key);
},
Expand All @@ -113,51 +109,30 @@ export const makePluck = (typePolicies: GraphQLTypePolicy) => {
return acc;
}

const [field, value] = rootField;
const [property, value] = rootField;

// Found an array of entities
if (Array.isArray(value) && value.some(isEntity)) {
// TODO: Unsure here, do union types allow a mix of scalars and entities?
// I think it does
acc[field] = value.map(value =>
// Union types
acc[property] = value.map(value =>
isEntity(value)
? storeEntityReference(value, store)
: value,
);

if (!rest.length) {
return acc;
}

return recur(rest, acc);
}

// Scalar fields at the root don't have an entity reference so just set it
// Root fields are visited before nested fields
if (!isEntity(value) && field !== "__typename") {
acc[field] = value;

return recur(rest, acc);
}

// With prefetched data, if there is no type information then ignore and continue
if (
!isEntity(value) &&
data[field] !== undefined &&
value &&
typeof value === "object" &&
rest.length
) {
acc[field] = value;
// Properties which have values which are not entities
if (!isEntity(value)) {
acc[property] = value;

return recur(rest, acc);
}

const entityReference = storeEntityReference(value, store);

if (data[field]) {
acc[field] = entityReference;
}
acc[property] = entityReference;

return recur(rest, acc);
},
Expand Down