Skip to content

Commit

Permalink
fix(chore): accept variables for mutations via POST
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Toth committed Feb 15, 2016
1 parent ddb11dd commit e2292e7
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/express/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ export default function middleware({ graphiql = true, schema = required() } = {}
return sendError(response, boom);
}

return graphql(schema, query, request, variables)
let parsedVariables = variables;
try {
parsedVariables = JSON.parse(variables);
} catch (err) {
// ignore
}

return graphql(schema, query, request, parsedVariables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
Expand Down
28 changes: 28 additions & 0 deletions src/express/express.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,33 @@ describe('graffiti express', () => {

mw(request, response);
});

it('should accept variables for mutations via POST', function postTest(done) {
const result = { updateData: '123' };
const mw = mwFactory({
schema: this.schema
});

const request = {
method: 'POST',
path: '/graphql',
body: {
query: `mutation mutate($data: String!) {
updateData(data: $data)
}`,
variables: '{ "data": "123" }'
},
accepts: (type) => type === 'json'
};

const response = {
json: ({ data }) => {
expect(data).to.be.eql(result);
done();
}
};

mw(request, response);
});
});
});
9 changes: 8 additions & 1 deletion src/hapi/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const plugin = {
return reply(methodNotAllowed('GraphQL mutation only allowed in POST request.'));
}

return graphql(schema, query, request, variables)
let parsedVariables = variables;
try {
parsedVariables = JSON.parse(variables);
} catch (err) {
// ignore
}

return graphql(schema, query, request, parsedVariables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
Expand Down
32 changes: 32 additions & 0 deletions src/hapi/hapi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ describe('graffiti hapi', () => {
});
});

it('should accept variables for mutations via POST', function postTest(done) {
const server = new Server();
server.connection({ port: 3000 });

const result = { updateData: '123' };

server.register({
register: hapi,
options: {
schema: this.schema
}
}, (err) => {
if (err) {
return done(err);
}

server.inject({
method: 'POST',
url: '/graphql',
payload: {
query: `mutation mutate($data: String!) {
updateData(data: $data)
}`,
variables: '{ "data": "123" }'
}
}, ({ payload }) => {
expect(JSON.parse(payload).data).to.eql(result);
done();
});
});
});

it('should return with GraphiQL on GET when it is enabled', function getTest(done) {
const server = new Server();
server.connection({ port: 3000 });
Expand Down
9 changes: 8 additions & 1 deletion src/koa/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export default function middleware({ graphiql = true, schema = required() } = {}
return this.body;
}

this.body = yield graphql(schema, query, this, variables);
let parsedVariables = variables;
try {
parsedVariables = JSON.parse(variables);
} catch (err) {
// ignore
}

this.body = yield graphql(schema, query, this, parsedVariables);
return this.body;
}

Expand Down
24 changes: 24 additions & 0 deletions src/koa/koa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ describe('graffiti koa', () => {
expect(res.data).to.eql(result);
});

it('should accept variables for mutations via POST', function *postTest() {
const mwFactory = koa;

const mw = mwFactory({
schema: this.schema
});

const request = {
method: 'POST',
path: '/graphql',
payload: {
query: `mutation mutate($data: String!) {
updateData(data: $data)
}`,
variables: '{ "data": "123" }'
}
};

const result = { updateData: '123' };
const res = yield mw.call(request);

expect(res.data).to.eql(result);
});

it('should return with GraphiQL on GET when it is enabled', function *getTest() {
const mwFactory = koa;

Expand Down
19 changes: 18 additions & 1 deletion src/test-setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import chaiSubset from 'chai-subset';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt
GraphQLInt,
GraphQLString
} from 'graphql';

before(() => {
Expand All @@ -25,6 +26,22 @@ beforeEach(function beforeEachTest() {
resolve: () => 1
}
}
}),
mutation: new GraphQLObjectType({
name: 'RootMutationType',
fields: {
updateData: {
type: GraphQLString,
args: {
data: {
name: 'data',
type: GraphQLString
}
},
description: 'Returns the data provided',
resolve: (obj, { data }) => data
}
}
})
});
});
Expand Down

0 comments on commit e2292e7

Please sign in to comment.