1.0.7 • Published 8 years ago
hapi-graysql v1.0.7
GraysQL Hapi Plugin
Use GraysQL with HapiJS.
Why this Plugin ?
You can write reusable plugins that exposes GraphQL queries, and join the full schema before creating a GraphQLHTTP Server.
Install
npm install --save hapi-graysql
Hapi GraphQL Server Example
For the full Hapi GraphQL Server Example Check the examples
folder:
import Hapi from 'hapi';
import HapiGraphQL from 'hapi-graphql';
import HapiGraysQL from 'hapi-graysql';
import Path from 'path';
import DB from './graphql/db';
export default async function Server() {
try {
const SCHEMAPATH = Path.join(__dirname, 'graphql', 'schema')
const server = new Hapi.Server();
server.connection({
port: 8080
});
//Main Plugins
await server.register({
register: HapiGraysQL,
options: {
extensions: ['load-from-dir'],
options: {
DB: DB
}
}
});
// load-from-dir extension exposes load method
// Load the main schema
await server.GQL.load(PATHS.schema);
// Now every plugin loads its own schema.
/* await server.register({
register: MYPLUGIN-THATUSES-HAPI-GRAYSQL,
options: {
option1: 'example opt'
}
}); */
// Build up the GraphQL Full Schema
const schema = server.GQL.generateSchema();
// Register HapiGraphQL Plugin
// Enabling graphiql let us test the Server.
await server.register({
register: HapiGraphQL,
options: {
query: {
schema: schema,
graphiql: true
},
route: {
path: '/graphql',
config: {}
}
}
});
await server.start();
console.log( `Server started at ${ server.info.uri }` );
} catch(e) {
console.error(e);
}
}
Options
The options
key accepts the following:
extensions
: An optional array of strings containing a validGraysQL
extension name see more here.options
: Any number of custom keys. This keys will be passed to GraysQL Constructor see more here.