Created
February 18, 2016 03:42
-
-
Save chasevida/f4f2e33f18cfa5b1619f to your computer and use it in GitHub Desktop.
Hapi GraphQL simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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