我正在将GraphQL
与Hapi
一起使用,并运行错误
代码如下:
const hapi = require('hapi');
const mongoose = require('mongoose');
const { graphqlHapi, graphiqlHapi } = require('apollo-server-hapi');
const schema = require('./graphql/schema');
const Painting = require('./models/Painting');
/* swagger section */
const Inert = require('inert');
const Vision = require('vision');
const HapiSwagger = require('hapi-swagger');
const Pack = require('./package');
const server = hapi.server({
port: 5000,
host: 'localhost'
});
mongoose.connect('mongodb:// dbuser:dbpassword@ds151612.mlab.com:51612 / example_graphql_api',{useNewUrlParser:true});
mongoose.connection.once('open', () => {
console.log('connected to database');
});
const init = async () => {
await server.register([
Inert,
Vision,
{
plugin: require(HapiSwagger),
options: {
info: {
title: 'Paintings API Documentation',
version: Pack.version
}
}
}
]);
await server.register({
plugin: require(graphiqlHapi),
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql'
},
route: {
cors: true
}
}
});
await server.register({
plugin: require(graphqlHapi),
options: {
path: '/graphql',
graphqlOptions: {
schema
},
route: {
cors: true
}
}
});
server.route([
{
method: 'GET',
path: '/api/v1/paintings',
options: {
description: 'Get all the paintings',
tags: ['api', 'v1', 'painting']
},
handler: (req, reply) => {
return Painting.find();
}
},
{
method: 'POST',
path: '/api/v1/paintings',
options: {
description: 'Get a specific painting by ID.',
tags: ['api', 'v1', 'painting']
},
handler: (req, reply) => {
const { name, url, technique } = req.payload;
const painting = new Painting({
name,
url,
technique
});
return painting.save();
}
}
]);
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on('unHandledRejection', (err) => {
if (err) {
console.log(err);
process.exit(1);
}
});
init();
错误:
(节点:28684)UnhandledPromiseRejectionWarning:AssertionError [ERR_ASSERTION]:路径必须为字符串
(节点:28684)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1)
(节点:28684)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。