chore(get-support): support for GET method for hapi

This commit is contained in:
Hagai Cohen 2016-10-20 21:46:52 +03:00
parent 7abcee8398
commit aa251f0663
2 changed files with 44 additions and 17 deletions

View file

@ -98,7 +98,7 @@ app.use('/graphql', graphqlConnect({ schema: myGraphQLSchema }));
http.createServer(app).listen(PORT); http.createServer(app).listen(PORT);
``` ```
### Hapi ### Hapi ( POST / GET )
Now with the Hapi plugins `graphqlHapi` and `graphiqlHapi` you can pass a route object that includes options to be applied to the route. The example below enables CORS on the `/graphql` route. Now with the Hapi plugins `graphqlHapi` and `graphiqlHapi` you can pass a route object that includes options to be applied to the route. The example below enables CORS on the `/graphql` route.

View file

@ -20,7 +20,8 @@ export interface HapiPluginOptions {
} }
const graphqlHapi: IRegister = function(server: Server, options: HapiPluginOptions, next) { const graphqlHapi: IRegister = function(server: Server, options: HapiPluginOptions, next) {
server.method('verifyPayload', verifyPayload); server.method('assignIsBatch', assignIsBatch);
server.method('assignBuffer', assignBuffer);
server.method('getGraphQLParams', getGraphQLParams); server.method('getGraphQLParams', getGraphQLParams);
server.method('getGraphQLOptions', getGraphQLOptions); server.method('getGraphQLOptions', getGraphQLOptions);
server.method('processQuery', processQuery); server.method('processQuery', processQuery);
@ -29,12 +30,17 @@ const graphqlHapi: IRegister = function(server: Server, options: HapiPluginOptio
plugins: { plugins: {
graphql: isOptionsFunction(options.graphqlOptions) ? options.graphqlOptions : () => options.graphqlOptions, graphql: isOptionsFunction(options.graphqlOptions) ? options.graphqlOptions : () => options.graphqlOptions,
}, },
pre: [{ pre: [
{
assign: 'buffer',
method: 'assignBuffer(method, payload, query)',
},
{
assign: 'isBatch', assign: 'isBatch',
method: 'verifyPayload(payload)', method: 'assignIsBatch(method, pre.buffer)',
}, { }, {
assign: 'graphqlParams', assign: 'graphqlParams',
method: 'getGraphQLParams(payload, pre.isBatch)', method: 'getGraphQLParams(pre.buffer, pre.isBatch)',
}, { }, {
assign: 'graphqlOptions', assign: 'graphqlOptions',
method: 'getGraphQLOptions', method: 'getGraphQLOptions',
@ -45,7 +51,7 @@ const graphqlHapi: IRegister = function(server: Server, options: HapiPluginOptio
}); });
server.route({ server.route({
method: 'POST', method: ['GET', 'POST'],
path: options.path || '/graphql', path: options.path || '/graphql',
config, config,
handler: function(request, reply) { handler: function(request, reply) {
@ -71,23 +77,44 @@ graphqlHapi.attributes = {
version: '0.0.1', version: '0.0.1',
}; };
function verifyPayload(payload, reply) { function assignBuffer(method, payload, query, reply) {
switch ( method ) {
case 'get':
if (!query) {
return reply(createErr(500, 'GET query missing.'));
}
return reply(query);
case 'post':
if (!payload) { if (!payload) {
return reply(createErr(500, 'POST body missing.')); return reply(createErr(500, 'POST body missing.'));
} }
return reply(payload);
// TODO: do something different here if the body is an array. default:
// Throw an error if body isn't either array or object. return reply(createErr(405, 'Apollo Server supports only GET/POST requests.'));
reply(payload && Array.isArray(payload)); }
} }
function getGraphQLParams(payload, isBatch, reply) { function assignIsBatch(method, buffer, reply) {
// TODO: do something different here if the body is an array.
// Throw an error if body isn't either array or object.
switch ( method ) {
case 'get':
return reply(false);
case 'post':
return reply(Array.isArray(buffer));
default:
throw new Error(`Invalid case reached, method is ${method}`);
}
}
function getGraphQLParams(buffer, isBatch, reply) {
if (!isBatch) { if (!isBatch) {
payload = [payload]; buffer = [buffer];
} }
const params = []; const params = [];
for (let query of payload) { for (let query of buffer) {
let variables = query.variables; let variables = query.variables;
if (variables && typeof variables === 'string') { if (variables && typeof variables === 'string') {
try { try {