Skip to content
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
231 changes: 231 additions & 0 deletions internals-js/src/__tests__/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,216 @@ describe('fragments optimization', () => {
}
}
`);

const optimizedWithAutoRefragmetize = withoutFragments.optimize(operation.selectionSet.fragments!, {autoFragmetize: true} );
// Note that we expect onU to *not* be recreated because, by default, optimize only
// add add back a fragment if it is used at least twice (otherwise, the fragment just
// make the query bigger).
expect(optimizedWithAutoRefragmetize.toString()).toMatchString(`
fragment OnT1 on T1 {
a
b
}

fragment OnT2 on T2 {
x
y
}

fragment OnI on I {
b
}

fragment OnU on U {
...OnI
...OnT1
...OnT2
}

{
t {
...OnU
u {
...OnU
}
}
}
`);
});

test('Reuse fragment if possible else auto re-fragment', () => {
const schema = parseSchema(`
type Query {
t: T1
}

type T1 {
a: Int
b: Int
c: String
d: T2
}

type T2 {
x: String
y: String
u: String
t: String
s: String
r: String
q: String
p: String
o: String
z: Int
v: String
w: T1
}

`);

const operation = parseOperation(schema, `

fragment OnSubT2 on T2 {
x
y
u
t
s
r
q
p
}

query {
t {
d {
...OnSubT2
z
w {
d {
...OnSubT2
v
}
}
}
}

duplicate: t {
d {
...OnSubT2
z
w {
d {
...OnSubT2
v
}
}
}
}
}
`);
const expandedOperationForQP = operation.expandAllFragments();
const resultantOperationWithoutFragments = expandedOperationForQP.optimize(operation.selectionSet.fragments!, {autoFragmetize: false});
expect(resultantOperationWithoutFragments.toString()).toMatchString(`
{
t {
d {
x
y
u
t
s
r
q
p
z
w {
d {
x
y
u
t
s
r
q
p
v
}
}
}
}
duplicate: t {
d {
x
y
u
t
s
r
q
p
z
w {
d {
x
y
u
t
s
r
q
p
v
}
}
}
}
}
`);
const optimized = expandedOperationForQP.optimize(operation.selectionSet.fragments!, {autoFragmetize: true});
// Note that since auto refragment is set to true we were able to fragment the composite types
// Also reuse the query fragment "OnSubT2"
// and thus effective re-fragment and reduce the number of lines
expect(optimized.toString()).toMatchString(`
fragment OnSubT2 on T2 {
x
y
u
t
s
r
q
p
}

fragment T206a5e7e420bda1858b8f4a7b50df310b2444b2df0c2539a574eb73b2a47e331d on T2 {
...OnSubT2
v
}

fragment T244924a138e3ac3bc1d623451cad7b7b82d2b8f580227f63436a4715fc85f4f37 on T2 {
...OnSubT2
z
w {
d {
...T206a5e7e420bda1858b8f4a7b50df310b2444b2df0c2539a574eb73b2a47e331d
}
}
}

{
t {
d {
...T244924a138e3ac3bc1d623451cad7b7b82d2b8f580227f63436a4715fc85f4f37
}
}
duplicate: t {
d {
...T244924a138e3ac3bc1d623451cad7b7b82d2b8f580227f63436a4715fc85f4f37
}
}
}
`);
});

test('handles fragments with nested selections', () => {
Expand Down Expand Up @@ -227,6 +437,27 @@ describe('fragments optimization', () => {
}
}

{
t1a {
...OnT1
t2 {
y
}
}
t2a {
...OnT1
}
}
`);
// In the following test with AutoFragmetize as true, we dont achieve extra benefits so the output operation remains the same as above.
const optimizedWithAutoFragmetize = withoutFragments.optimize(operation.selectionSet.fragments!, {autoFragmetize: true});
expect(optimizedWithAutoFragmetize.toString()).toMatchString(`
fragment OnT1 on T1 {
t2 {
x
}
}

{
t1a {
...OnT1
Expand Down
Loading