Question: Optional config param for the hapi register method
developer-rakeshpaul opened this issue · 2 comments
developer-rakeshpaul commented
Overview of the Issue
Team is it possible to provide an optional config param to the register method of the hapi plugin. This will help users with ability to configure authentication mode and other params easily. For instance if we change the register method for the hapi plugin like below
const plugin = {
register: (server, { graphiql = true, context = {}, config = {}, schema = required() } = {}, next) => {
const handler = (request, reply) => {
const data = request.payload || request.query || {};
const { query, variables } = data;
if (accepts(request, 'html') && graphiql) {
return reply(renderGraphiQL({ query, variables }));
}
if (query && query.includes('mutation') && isGet(request)) {
return reply(methodNotAllowed('GraphQL mutation only allowed in POST request.'));
}
let parsedVariables = variables;
try {
parsedVariables = JSON.parse(variables);
} catch (err) {
// ignore
}
return graphql(schema, query, { request }, context, parsedVariables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
reply(badRequest(message));
return;
}
reply(result);
})
.catch((err) => {
reply(badRequest(err));
});
};
server.route({
method: 'POST',
path: '/graphql',
config,
handler
});
if (graphiql) {
server.route({
method: 'GET',
path: '/graphql',
config,
handler
});
}
next();
}
};
Then users can easily provide authentication and security by providing the auth mode in the config like below without going for the resolve hooks
{
register: graffiti.hapi,
options: {
schema,
context: {}, // custom context
config: {
description: 'graphQL endpoints',
auth: 'jwt',
},
},
}
Please help if there are any better ways to solve this.
Kind Regards,
Paul
tothandras commented
Hey @developer-rakeshpaul,
I don't really use hapi, but it looks alright. Can you open a PR? 😉
developer-rakeshpaul commented
Sure @tothandras. Will do it today itself..