Skip to content

Commit

Permalink
chore(get-support): support for GET method for hapi
Browse files Browse the repository at this point in the history
  • Loading branch information
DxCx committed Oct 20, 2016
1 parent a112122 commit dd7fa7f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ app.use('/graphql', apolloConnect({ schema: myGraphQLSchema }));
http.createServer(app).listen(PORT);
```

### Hapi
### Hapi ( POST / GET )

Now with the Hapi plugins `apolloHapi` 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.

Expand Down
54 changes: 40 additions & 14 deletions src/integrations/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as GraphiQL from '../modules/renderGraphiQL';
import { runQuery } from '../core/runQuery';
import ApolloOptions from './apolloOptions';


export interface IRegister {
(server: Server, options: any, next: any): void;
attributes?: any;
Expand All @@ -22,7 +21,8 @@ export interface HapiPluginOptions {
}

const apolloHapi: 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('getApolloOptions', getApolloOptions);
server.method('processQuery', processQuery);
Expand All @@ -31,12 +31,17 @@ const apolloHapi: IRegister = function(server: Server, options: HapiPluginOption
plugins: {
graphql: isOptionsFunction(options.apolloOptions) ? options.apolloOptions : () => options.apolloOptions,
},
pre: [{
pre: [
{
assign: 'buffer',
method: 'assignBuffer(method, payload, query)',
},
{
assign: 'isBatch',
method: 'verifyPayload(payload)',
method: 'assignIsBatch(method, pre.buffer)',
}, {
assign: 'graphqlParams',
method: 'getGraphQLParams(payload, pre.isBatch)',
method: 'getGraphQLParams(pre.buffer, pre.isBatch)',
}, {
assign: 'apolloOptions',
method: 'getApolloOptions',
Expand All @@ -47,7 +52,7 @@ const apolloHapi: IRegister = function(server: Server, options: HapiPluginOption
});

server.route({
method: 'POST',
method: ['GET', 'POST'],
path: options.path || '/graphql',
config,
handler: function(request, reply) {
Expand All @@ -73,23 +78,44 @@ apolloHapi.attributes = {
version: '0.0.1',
};

function verifyPayload(payload, reply) {
if (!payload) {
return reply(createErr(500, 'POST body missing.'));
}
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) {
return reply(createErr(500, 'POST body missing.'));
}
return reply(payload);
default:
return reply(createErr(405, 'Apollo Server supports only GET/POST requests.'));
}
}

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.
reply(payload && Array.isArray(payload));

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(payload, isBatch, reply) {
function getGraphQLParams(buffer, isBatch, reply) {
if (!isBatch) {
payload = [payload];
buffer = [buffer];
}

const params = [];
for (let query of payload) {
for (let query of buffer) {
let variables = query.variables;
if (variables && typeof variables === 'string') {
try {
Expand Down

0 comments on commit dd7fa7f

Please sign in to comment.