Skip to content

Commit 0ac2709

Browse files
committed
fix(graphql): omit variables and operation name if empty. Fixes #243
1 parent ea19b01 commit 0ac2709

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/dsl/graphql.spec.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ describe("GraphQLInteraction", () => {
2626
});
2727
});
2828
describe("when no operation is provided", () => {
29-
it("should marshal to null", () => {
29+
it("should not be present in unmarshaled body", () => {
3030
interaction.uponReceiving("a request");
3131
interaction.withQuery("{ hello }");
3232

3333
const json: any = interaction.json();
34-
expect(json.request.body.operationName).to.eq(null);
34+
expect(json.request.body).to.not.have.property("operationName");
3535
});
3636
});
3737
describe("when given an invalid operation", () => {
@@ -48,13 +48,23 @@ describe("GraphQLInteraction", () => {
4848
interaction.withOperation("query");
4949
interaction.withQuery("{ hello }");
5050
interaction.withVariables({
51-
foo: "bar",
51+
foo: "bar"
5252
});
5353

5454
const json: any = interaction.json();
5555
expect(json.request.body.variables).to.deep.eq({ foo: "bar" });
5656
});
5757
});
58+
describe("when no variables are provided", () => {
59+
it("should not add the variables property to the payload", () => {
60+
interaction.uponReceiving("a request");
61+
interaction.withOperation("query");
62+
interaction.withQuery("{ hello }");
63+
64+
const json: any = interaction.json();
65+
expect(json.request.body).to.not.have.property("variables");
66+
});
67+
});
5868
});
5969

6070
describe("#withQuery", () => {
@@ -63,13 +73,15 @@ describe("GraphQLInteraction", () => {
6373
interaction.withOperation("query");
6474
interaction.withQuery("{ hello }");
6575
interaction.withVariables({
66-
foo: "bar",
76+
foo: "bar"
6777
});
6878
});
6979

7080
describe("when given an invalid query", () => {
7181
it("should fail with an error", () => {
72-
expect(() => interaction.withQuery("{ not properly terminated")).to.throw(Error);
82+
expect(() =>
83+
interaction.withQuery("{ not properly terminated")
84+
).to.throw(Error);
7385
});
7486
});
7587

@@ -101,7 +113,7 @@ describe("GraphQLInteraction", () => {
101113
}
102114
}`);
103115
interaction.withVariables({
104-
name: "bar",
116+
name: "bar"
105117
});
106118
const json: any = interaction.json();
107119

@@ -110,9 +122,7 @@ describe("GraphQLInteraction", () => {
110122
const lotsOfWhitespace = `{ Hello(id: \$id) { name } }`;
111123
expect(r.test(lotsOfWhitespace)).to.eq(true);
112124
});
113-
114125
});
115126
});
116127
});
117-
118128
});

src/dsl/graphql.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { Interaction, InteractionState } from "../dsl/interaction";
77
import { regex } from "./matchers";
8-
import { isNil, extend } from "lodash";
8+
import { isNil, extend, omitBy, isEmpty } from "lodash";
99
import gql from "graphql-tag";
1010

1111
export interface GraphQLVariables { [name: string]: any; }
@@ -81,12 +81,12 @@ export class GraphQLInteraction extends Interaction {
8181
throw new Error("You must provide a description for the query.");
8282
}
8383

84-
this.state.request = extend({
85-
body: {
84+
this.state.request = extend({
85+
body: omitBy({
8686
operationName: this.operation || null,
8787
query: regex({ generate: this.query, matcher: escapeGraphQlQuery(this.query) }),
8888
variables: this.variables,
89-
},
89+
}, isEmpty),
9090
headers: { "content-type": "application/json" },
9191
method: "POST",
9292
}, this.state.request);

0 commit comments

Comments
 (0)