Skip to content

Commit 8efac44

Browse files
committed
refactor: improve query inlining to handle nested field merging correctly 🔄
This refactors the fragment inlining logic to properly handle merging fields with the same name but different selection sets, especially when fields have arguments. - Rewrote the field merging algorithm to properly handle nested selections - Added handling for GraphQL arguments to prevent duplicates and ensure correct merging - Improved type safety with type guards and better TypeScript practices - Added normalization for value nodes to create consistent keys - Updated test to use snapshot testing instead of comparing strings - Fixed a bug where fields with the same name but different arguments were incorrectly merged
1 parent 3e36381 commit 8efac44

File tree

2 files changed

+192
-112
lines changed

2 files changed

+192
-112
lines changed

src/lib/inline-relay-query.test.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const relayQueryText = `query NavigationQuery {
1010
account {
1111
id
1212
account_type
13+
people(where: {is_active: {_eq: true}}) {
14+
id
15+
email
16+
}
1317
}
1418
...PermissionsProvider_user
1519
}
@@ -21,30 +25,35 @@ fragment PermissionsProvider_user on users {
2125
account {
2226
id
2327
timesheets_protected
24-
}
25-
permissions
26-
}`;
27-
28-
const expectedInlinedQuery = `query NavigationQuery {
29-
current_user {
30-
id
31-
permissions
32-
account {
28+
people(where: {is_active: {_eq: true}}) {
3329
id
34-
account_type
35-
timesheets_protected
30+
name
3631
}
37-
email
3832
}
39-
}
40-
`;
33+
permissions
34+
}`;
4135

4236
describe('inlineRelayQueryFromString', () => {
4337
it('inlines fragments in a Relay query string', () => {
4438
const inlined = inlineRelayQueryFromString(relayQueryText);
45-
// Remove extra whitespace/newlines for comparison.
46-
const normalized = inlined.replace(/\s+/g, ' ').trim();
47-
const normalizedExpected = expectedInlinedQuery.replace(/\s+/g, ' ').trim();
48-
expect(normalized).toBe(normalizedExpected);
39+
expect(inlined).toMatchInlineSnapshot(`
40+
"query NavigationQuery {
41+
current_user {
42+
id
43+
permissions
44+
account {
45+
id
46+
account_type
47+
people(where: {is_active: {_eq: true}}) {
48+
id
49+
email
50+
name
51+
}
52+
timesheets_protected
53+
}
54+
email
55+
}
56+
}"
57+
`);
4958
});
5059
});

0 commit comments

Comments
 (0)