Skip to content

Commit

Permalink
include/skip on fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Apr 23, 2024
1 parent ce0f551 commit b9a274a
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/react-urql/src/hooks/useFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,17 @@ const maskFragment = <Data>(
const maskedData = {};
let isDataComplete = true;
selectionSet.selections.forEach(selection => {
const hasIncludeOrSkip =
selection.directives &&
selection.directives.some(
x => x.name.value === 'include' || x.name.value === 'skip'
);

if (selection.kind === Kind.FIELD) {
const fieldAlias = selection.alias
? selection.alias.value
: selection.name.value;

const hasIncludeOrSkip =
selection.directives &&
selection.directives.some(
x => x.name.value === 'include' || x.name.value === 'skip'
);

if (selection.selectionSet) {
if (data[fieldAlias] === undefined) {
if (hasIncludeOrSkip) return;
Expand Down Expand Up @@ -266,15 +266,15 @@ const maskFragment = <Data>(
}
maskedData[selection.name.value] = data[selection.name.value];
} else if (selection.kind === Kind.INLINE_FRAGMENT) {
if (isHeuristicFragmentMatch(selection, data, fragments)) {
if (!isHeuristicFragmentMatch(selection, data, fragments)) {
return;
}

const result = maskFragment(data, selection.selectionSet, fragments);
// TODO: how do we handle inline-fragments with a skip/include directive?
if (!result.fulfilled) {
if (!result.fulfilled && !hasIncludeOrSkip) {
isDataComplete = false;
}

Object.assign(maskedData, result.data);
} else if (selection.kind === Kind.FRAGMENT_SPREAD) {
const fragment = fragments[selection.name.value];
Expand All @@ -286,13 +286,12 @@ const maskFragment = <Data>(
return;
}

if (!fragment || isHeuristicFragmentMatch(fragment, data, fragments)) {
if (!fragment || !isHeuristicFragmentMatch(fragment, data, fragments)) {
return;
}

const result = maskFragment(data, fragment.selectionSet, fragments);
// TODO: how do we handle inline-fragments with a skip/include directive?
if (!result.fulfilled) {
if (!result.fulfilled && !hasIncludeOrSkip) {
isDataComplete = false;
}
Object.assign(maskedData, result.data);
Expand Down

0 comments on commit b9a274a

Please sign in to comment.