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

(populate) - skip visits inside of a FragmentDefinition #1175

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/lazy-suns-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-populate': patch
---

Fix the scenario where we traverse a query but end up in a nested fragment, this maks it so that we can't derive the type for the sub-entity
69 changes: 69 additions & 0 deletions exchanges/populate/src/populateExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,75 @@ describe('interface returned in mutation', () => {
});
});

describe('nested fragment', () => {
const fragment = gql`
fragment TodoFragment on Todo {
id
author {
id
}
}
`;

const queryOp = makeOperation(
'query',
{
key: 1234,
query: gql`
query {
todos {
...TodoFragment
}
}

${fragment}
`,
},
context
);

const mutationOp = makeOperation(
'mutation',
{
key: 5678,
query: gql`
mutation MyMutation {
updateTodo @populate
}
`,
},
context
);

it('should work with nested fragments', () => {
const response = pipe<Operation, any, Operation[]>(
fromArray([queryOp, mutationOp]),
populateExchange({ schema })(exchangeArgs),
toArray
);

expect(print(response[1].query)).toMatchInlineSnapshot(`
"mutation MyMutation {
updateTodo {
...Todo_PopulateFragment_0
}
}

fragment Todo_PopulateFragment_0 on Todo {
...TodoFragment
}

fragment TodoFragment on Todo {
id
author {
id
}
}
"
`);
});
});

describe('nested interfaces', () => {
const queryOp = makeOperation(
'query',
Expand Down
16 changes: 14 additions & 2 deletions exchanges/populate/src/populateExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,19 @@ export const extractSelectionsFromQuery = (
};

const visits: string[] = [];
let insideFragment = false;

traverse(
query,
node => {
if (node.kind === Kind.FRAGMENT_DEFINITION) {
insideFragment = true;
extractedFragments.push(node);
} else if (node.kind === Kind.FIELD && node.selectionSet) {
} else if (
node.kind === Kind.FIELD &&
node.selectionSet &&
!insideFragment
) {
const type = unwrapType(
resolveFields(schema, visits)[node.name.value].type
);
Expand Down Expand Up @@ -219,7 +225,13 @@ export const extractSelectionsFromQuery = (
}
},
node => {
if (node.kind === Kind.FIELD && node.selectionSet) visits.pop();
if (node.kind === Kind.FIELD && node.selectionSet && !insideFragment) {
visits.pop();
}

if (node.kind === Kind.FRAGMENT_DEFINITION) {
insideFragment = false;
}
}
);

Expand Down