Skip to content

Instantly share code, notes, and snippets.

@chasevida
Created February 18, 2016 03:42
Show Gist options
  • Save chasevida/f4f2e33f18cfa5b1619f to your computer and use it in GitHub Desktop.
Save chasevida/f4f2e33f18cfa5b1619f to your computer and use it in GitHub Desktop.
Hapi GraphQL simple example
var Hapi = require('hapi');
var GraphQL = require('./lib');
var GQL = require('graphql');
var GraphQLSchema = GQL.GraphQLSchema;
var GraphQLObjectType = GQL.GraphQLObjectType;
var GraphQLInt = GQL.GraphQLInt;
var server = new Hapi.Server();
server.connection({
port: 3000
});
var Counter = new GraphQLObjectType({
name: 'Counter',
fields: () => ({
counter: {
type: GraphQLInt
}
})
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
viewer: {
type: Counter,
resolve: () => 42
}
})
});
var TestSchema = new GraphQLSchema({
query: Query
});
server.register({
register: GraphQL,
options: {
query: {
schema: TestSchema,
rootValue: {},
pretty: false,
graphiql: true
},
// OR
//
// query: (request) => {{
// schema: TestSchema,
// rootValue: {},
// pretty: false,
// graphiql: true
// }),
route: {
path: '/graphql',
config: {}
}
}
}, () =>
server.start(() =>
console.log('Server running at:', server.info.uri)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment