Skip to content

Commit e2292e7

Browse files
author
Andras Toth
committed
fix(chore): accept variables for mutations via POST
1 parent ddb11dd commit e2292e7

File tree

7 files changed

+126
-4
lines changed

7 files changed

+126
-4
lines changed

src/express/express.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ export default function middleware({ graphiql = true, schema = required() } = {}
3131
return sendError(response, boom);
3232
}
3333

34-
return graphql(schema, query, request, variables)
34+
let parsedVariables = variables;
35+
try {
36+
parsedVariables = JSON.parse(variables);
37+
} catch (err) {
38+
// ignore
39+
}
40+
41+
return graphql(schema, query, request, parsedVariables)
3542
.then((result) => {
3643
if (result.errors) {
3744
const message = result.errors.map((error) => error.message).join('\n');

src/express/express.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,33 @@ describe('graffiti express', () => {
176176

177177
mw(request, response);
178178
});
179+
180+
it('should accept variables for mutations via POST', function postTest(done) {
181+
const result = { updateData: '123' };
182+
const mw = mwFactory({
183+
schema: this.schema
184+
});
185+
186+
const request = {
187+
method: 'POST',
188+
path: '/graphql',
189+
body: {
190+
query: `mutation mutate($data: String!) {
191+
updateData(data: $data)
192+
}`,
193+
variables: '{ "data": "123" }'
194+
},
195+
accepts: (type) => type === 'json'
196+
};
197+
198+
const response = {
199+
json: ({ data }) => {
200+
expect(data).to.be.eql(result);
201+
done();
202+
}
203+
};
204+
205+
mw(request, response);
206+
});
179207
});
180208
});

src/hapi/hapi.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ const plugin = {
2929
return reply(methodNotAllowed('GraphQL mutation only allowed in POST request.'));
3030
}
3131

32-
return graphql(schema, query, request, variables)
32+
let parsedVariables = variables;
33+
try {
34+
parsedVariables = JSON.parse(variables);
35+
} catch (err) {
36+
// ignore
37+
}
38+
39+
return graphql(schema, query, request, parsedVariables)
3340
.then((result) => {
3441
if (result.errors) {
3542
const message = result.errors.map((error) => error.message).join('\n');

src/hapi/hapi.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ describe('graffiti hapi', () => {
9494
});
9595
});
9696

97+
it('should accept variables for mutations via POST', function postTest(done) {
98+
const server = new Server();
99+
server.connection({ port: 3000 });
100+
101+
const result = { updateData: '123' };
102+
103+
server.register({
104+
register: hapi,
105+
options: {
106+
schema: this.schema
107+
}
108+
}, (err) => {
109+
if (err) {
110+
return done(err);
111+
}
112+
113+
server.inject({
114+
method: 'POST',
115+
url: '/graphql',
116+
payload: {
117+
query: `mutation mutate($data: String!) {
118+
updateData(data: $data)
119+
}`,
120+
variables: '{ "data": "123" }'
121+
}
122+
}, ({ payload }) => {
123+
expect(JSON.parse(payload).data).to.eql(result);
124+
done();
125+
});
126+
});
127+
});
128+
97129
it('should return with GraphiQL on GET when it is enabled', function getTest(done) {
98130
const server = new Server();
99131
server.connection({ port: 3000 });

src/koa/koa.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ export default function middleware({ graphiql = true, schema = required() } = {}
2828
return this.body;
2929
}
3030

31-
this.body = yield graphql(schema, query, this, variables);
31+
let parsedVariables = variables;
32+
try {
33+
parsedVariables = JSON.parse(variables);
34+
} catch (err) {
35+
// ignore
36+
}
37+
38+
this.body = yield graphql(schema, query, this, parsedVariables);
3239
return this.body;
3340
}
3441

src/koa/koa.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ describe('graffiti koa', () => {
5757
expect(res.data).to.eql(result);
5858
});
5959

60+
it('should accept variables for mutations via POST', function *postTest() {
61+
const mwFactory = koa;
62+
63+
const mw = mwFactory({
64+
schema: this.schema
65+
});
66+
67+
const request = {
68+
method: 'POST',
69+
path: '/graphql',
70+
payload: {
71+
query: `mutation mutate($data: String!) {
72+
updateData(data: $data)
73+
}`,
74+
variables: '{ "data": "123" }'
75+
}
76+
};
77+
78+
const result = { updateData: '123' };
79+
const res = yield mw.call(request);
80+
81+
expect(res.data).to.eql(result);
82+
});
83+
6084
it('should return with GraphiQL on GET when it is enabled', function *getTest() {
6185
const mwFactory = koa;
6286

src/test-setup.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import chaiSubset from 'chai-subset';
55
import {
66
GraphQLSchema,
77
GraphQLObjectType,
8-
GraphQLInt
8+
GraphQLInt,
9+
GraphQLString
910
} from 'graphql';
1011

1112
before(() => {
@@ -25,6 +26,22 @@ beforeEach(function beforeEachTest() {
2526
resolve: () => 1
2627
}
2728
}
29+
}),
30+
mutation: new GraphQLObjectType({
31+
name: 'RootMutationType',
32+
fields: {
33+
updateData: {
34+
type: GraphQLString,
35+
args: {
36+
data: {
37+
name: 'data',
38+
type: GraphQLString
39+
}
40+
},
41+
description: 'Returns the data provided',
42+
resolve: (obj, { data }) => data
43+
}
44+
}
2845
})
2946
});
3047
});

0 commit comments

Comments
 (0)