Skip to content

Commit 32601c1

Browse files
committed
add more tests, make fragment removal recursive
1 parent f04abf4 commit 32601c1

3 files changed

Lines changed: 383 additions & 38 deletions

File tree

packages/apollo-language-server/src/project/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export class GraphQLClientProject extends GraphQLProject {
390390
for (const operationName in current) {
391391
const document = current[operationName];
392392

393-
let serviceOnly: DocumentNode = removeDirectiveAnnotatedFields(
393+
let serviceOnly = removeDirectiveAnnotatedFields(
394394
removeDirectives(document, clientOnlyDirectives as string[]),
395395
clientSchemaDirectives as string[]
396396
);

packages/apollo-language-server/src/utilities/__tests__/graphql.test.ts

Lines changed: 224 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ describe("withTypenameFieldAddedWhereNeeded", () => {
7878
});
7979

8080
describe("removeDirectiveAnnotatedFields", () => {
81-
it("should be a function", () => {
82-
expect(typeof removeDirectiveAnnotatedFields).toBe("function");
83-
});
84-
8581
it("should remove fields with matching directives", () => {
8682
expect(
8783
print(
@@ -98,7 +94,7 @@ describe("removeDirectiveAnnotatedFields", () => {
9894
`);
9995
});
10096

101-
it("should remove object fields with matching directives", () => {
97+
it("trim selections sets that are client only", () => {
10298
expect(
10399
print(
104100
removeDirectiveAnnotatedFields(
@@ -121,6 +117,83 @@ describe("removeDirectiveAnnotatedFields", () => {
121117
`);
122118
});
123119

120+
it("should remove fragments when a directive is used on a fragment spread", () => {
121+
expect(
122+
print(
123+
removeDirectiveAnnotatedFields(
124+
parse(`
125+
{
126+
me { name }
127+
...ClientFields @client
128+
}
129+
fragment ClientFields on Query {
130+
hello
131+
}
132+
`),
133+
["client"]
134+
)
135+
)
136+
).toMatchInlineSnapshot(`
137+
"{
138+
me {
139+
name
140+
}
141+
}
142+
"
143+
`);
144+
});
145+
146+
it("should remove fragments when client directive is used inline", () => {
147+
expect(
148+
print(
149+
removeDirectiveAnnotatedFields(
150+
parse(`
151+
{
152+
me { name }
153+
... on Query @client {
154+
hello
155+
}
156+
}
157+
`),
158+
["client"]
159+
)
160+
)
161+
).toMatchInlineSnapshot(`
162+
"{
163+
me {
164+
name
165+
}
166+
}
167+
"
168+
`);
169+
});
170+
171+
it("should remove fragments when the client directive is on the definition", () => {
172+
expect(
173+
print(
174+
removeDirectiveAnnotatedFields(
175+
parse(`
176+
fragment ClientObject on Query @client {
177+
hello
178+
}
179+
{
180+
me { name }
181+
... ClientObject
182+
}
183+
`),
184+
["client"]
185+
)
186+
)
187+
).toMatchInlineSnapshot(`
188+
"{
189+
me {
190+
name
191+
}
192+
}
193+
"
194+
`);
195+
});
196+
124197
it("should remove fragments that become unused when antecendant directives are removed", () => {
125198
expect(
126199
print(
@@ -189,4 +262,150 @@ describe("removeDirectiveAnnotatedFields", () => {
189262
"
190263
`);
191264
});
265+
266+
it("should recursively remove fragments that become unused when antecendant directives are removed", () => {
267+
expect(
268+
print(
269+
removeDirectiveAnnotatedFields(
270+
parse(`
271+
fragment One on Node {
272+
...Two
273+
user {
274+
friends {
275+
name
276+
...Two @client
277+
}
278+
}
279+
}
280+
fragment Two on Node {
281+
id
282+
}
283+
284+
query {
285+
me {
286+
...One
287+
}
288+
}
289+
`),
290+
["client"]
291+
)
292+
)
293+
).toMatchInlineSnapshot(`
294+
"fragment One on Node {
295+
...Two
296+
user {
297+
friends {
298+
name
299+
}
300+
}
301+
}
302+
303+
fragment Two on Node {
304+
id
305+
}
306+
307+
{
308+
me {
309+
...One
310+
}
311+
}
312+
"
313+
`);
314+
});
315+
316+
it("should remove fragment spreads from @client fragment definitions", () => {
317+
expect(
318+
print(
319+
removeDirectiveAnnotatedFields(
320+
parse(`
321+
fragment One on Node @client {
322+
...Two
323+
}
324+
325+
fragment Two on Node {
326+
id
327+
}
328+
329+
query {
330+
me {
331+
name
332+
...One
333+
}
334+
}
335+
`),
336+
["client"]
337+
)
338+
)
339+
).toMatchInlineSnapshot(`
340+
"{
341+
me {
342+
name
343+
}
344+
}
345+
"
346+
`);
347+
});
348+
349+
it("should remove all operations that have no selection set after fragments are removed", () => {
350+
expect(
351+
print(
352+
removeDirectiveAnnotatedFields(
353+
parse(`
354+
fragment One on Node @client {
355+
...Two
356+
}
357+
358+
fragment Two on Node {
359+
id
360+
}
361+
362+
{
363+
name
364+
me {
365+
...One
366+
}
367+
}
368+
`),
369+
["client"]
370+
)
371+
)
372+
).toMatchInlineSnapshot(`
373+
"{
374+
name
375+
}
376+
"
377+
`);
378+
});
379+
380+
it("should not remove fragment definitions that weren't removed by `removeDirectiveAnnotatedFields`", () => {
381+
expect(
382+
print(
383+
removeDirectiveAnnotatedFields(
384+
parse(`
385+
fragment One on Node {
386+
id
387+
}
388+
389+
{
390+
me {
391+
name
392+
}
393+
}
394+
`),
395+
["client"]
396+
)
397+
)
398+
).toMatchInlineSnapshot(`
399+
"fragment One on Node {
400+
id
401+
}
402+
403+
{
404+
me {
405+
name
406+
}
407+
}
408+
"
409+
`);
410+
});
192411
});

0 commit comments

Comments
 (0)