Skip to content

Commit 970d4e4

Browse files
authored
fix: apply directives skip/include for mutation (#172)
1 parent 5ab79b4 commit 970d4e4

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/__tests__/directives.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@ const testSchema = makeExecutableSchema({
1111
typeDefs: `
1212
schema {
1313
query: TestType
14+
mutation: Mut
1415
}
1516
type TestType {
1617
a: String
1718
b: String
1819
}
20+
type Mut {
21+
c: String
22+
d: String
23+
}
1924
`,
2025
resolvers: {
2126
TestType: {
2227
a: () => "a",
2328
b: () => "b"
29+
},
30+
Mut: {
31+
c: () => "c",
32+
d: () => "d"
2433
}
2534
}
2635
});
@@ -85,6 +94,66 @@ describe("Execute: handles directives", () => {
8594
});
8695
});
8796

97+
describe("mutations", () => {
98+
test("skip directive works on mutation", () => {
99+
const result = executeTestQuery(`
100+
mutation {
101+
c @skip(if: true)
102+
d
103+
}
104+
`);
105+
106+
expect(result).toEqual({
107+
data: { d: "d" }
108+
});
109+
});
110+
111+
test("include directive works on mutation", () => {
112+
const result = executeTestQuery(`
113+
mutation {
114+
c
115+
d @include(if: false)
116+
}
117+
`);
118+
119+
expect(result).toEqual({
120+
data: { c: "c" }
121+
});
122+
});
123+
124+
test("skip directive works on mutation - with variables", () => {
125+
const result = executeTestQuery(
126+
`
127+
mutation($if: Boolean!) {
128+
c @skip(if: $if)
129+
d
130+
}
131+
`,
132+
{ if: true }
133+
);
134+
135+
expect(result).toEqual({
136+
data: { d: "d" }
137+
});
138+
});
139+
140+
test("include directive works on mutation - with variables", () => {
141+
const result = executeTestQuery(
142+
`
143+
mutation($if: Boolean!) {
144+
c
145+
d @include(if: $if)
146+
}
147+
`,
148+
{ if: false }
149+
);
150+
151+
expect(result).toEqual({
152+
data: { c: "c" }
153+
});
154+
});
155+
});
156+
88157
describe("works on fragment spreads", () => {
89158
test("if false omits fragment spread", () => {
90159
const result = executeTestQuery(`

src/execution.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,18 @@ function compileDeferredField(
598598

599599
function compileDeferredFieldsSerially(context: CompilationContext): string {
600600
let body = "";
601-
context.deferred.forEach((deferredField) => {
601+
context.deferred.forEach((deferredField, index) => {
602602
const { name, fieldName, parentType } = deferredField;
603603
const resolverName = getResolverName(parentType.name, fieldName);
604604
const mutationHandler = getHoistedFunctionName(
605605
context,
606606
`${name}${resolverName}Mutation`
607607
);
608-
body += `${GLOBAL_EXECUTION_CONTEXT}.queue.push(${mutationHandler});\n`;
608+
body += `
609+
if (${SAFETY_CHECK_PREFIX}${index}) {
610+
${GLOBAL_EXECUTION_CONTEXT}.queue.push(${mutationHandler});
611+
}
612+
`;
609613
const appendix = `
610614
if (${GLOBAL_PROMISE_COUNTER} === 0) {
611615
${GLOBAL_RESOLVE}(${GLOBAL_EXECUTION_CONTEXT});

0 commit comments

Comments
 (0)