I've hit a roadblock setting up HapiJS with the ApolloStack.
I must have missed something.
'use strict'
const Path = require('path')
const Hapi = require('hapi')
const { ApolloHAPI, GraphiQLHAPI } = require('apollo-server')
const { host, port } = require('./configs')
const { apolloOptions, graphiQLOptions } = require('./options/apollo')
const server = new Hapi.Server({
connections: {
routes: {
files: {
relativeTo: Path.join(__dirname, 'statics')
}
},
router: {
stripTrailingSlash: true
}
}
})
server.connection({ host, port: process.env.PORT || port })
// Plugins
const plugins = [
{
register: new ApolloHAPI(),
options: apolloOptions,
routes: { prefix: '/graphql' }
},
{
register: new GraphiQLHAPI(),
options: graphiQLOptions,
routes: { prefix: '/graphiql' }
},
]
server.register([...plugins], err => {
if (err) throw err
server.start(err => {
if (err) throw err
server.log('info', `Server running at ${server.info.uri}`) // eslint-disable-line
})
})
const {
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
GraphQLInt,
GraphQLList,
GraphQLFloat,
GraphQLEnumType,
GraphQLNonNull,
GraphQLInterfaceType
} = require('graphql')
const TodoType = new GraphQLEnumType({
name: 'TodoType',
description: 'A Type of the todo',
values: {
HOME: { value: 'home' },
PLAY: { value: 'play' },
WORK: { value: 'work' }
}
})
const Todo = new GraphQLObjectType({
name: 'Todo',
description: 'Represent the todos',
fields: () => ({
_id: { type: GraphQLString },
title: { type: GraphQLString },
importance: { type: GraphQLInt },
completed: { type: GraphQLInt },
type: { type: TodoType },
date: { type: GraphQLFloat },
})
})
const TodoModel = require('../models/todos')
const Query = new GraphQLObjectType({
name: 'MySchemaQuery',
description: "Root of the My Schema",
fields: () => ({
todos: {
type: new GraphQLList(Todo),
description: "List of todos",
args: {
id: { type: GraphQLString },
active: { type: GraphQLInt },
importance: { type: GraphQLInt },
type: { type: GraphQLString },
},
resolve: (source, { id, active, importance, type }) => {
if (id) {
return TodoModel.getById(id)
} else if (active) {
return TodoModel.activeTodos(active)
} else if (importance) {
return TodoModel.ofImportance(importance)
} else if (type) {
return TodoModel.ofType(type)
} else {
return TodoModel.allTodos();
}
}
},
todo: {
type: Todo,
description: "A todo",
args: {
id: { type: new GraphQLNonNull(GraphQLString) }
},
resolve: (source, { id }) =>
TodoModel.getById(id)
},
latestTodos: {
type: Todo,
description: "Latest 5 todos added",
resolve: () =>
TodoModel.orderBy('id')
.limit(10)
.max('_id')
.run()
.then(todos => todos)
}
})
})
const Mutation = new GraphQLObjectType({
name: "MySchemaMutations",
fields: {
createTodo: {
type: Todo,
description: "Create a new todo",
args: {
title: { type: new GraphQLNonNull(GraphQLString) },
importance: { type: GraphQLInt },
type: { type: new GraphQLNonNull(TodoType) }
},
resolve: (source, todo) => TodoModel.saveTodo(todo)
}
}
})
const Schema = new GraphQLSchema({
query: Query,
mutation: Mutation
});
module.exports = Schema