我是nodejs和qraphql的新手,我正在尝试对graphql进行简单的查询 . 在对MySQL数据库进行查询之后,我的问题是我不知道如何正确地将数据发送到graphql以查看结果 .
我必须尝试使用解决方案,但它不起作用 . 我使用json从mySQL返回结果,但它不起作用 . 为什么我在Graphiql中看不到结果?我该如何回归?
这是我进行查询并返回结果的函数:
let getPlayer = (args) => {
let id = args.id;
console.log("id: " + id);
let myPromise = new Promise((resolve, reject) => {
const connection = mysql.createConnection(config.ddbb_connection);
connection.connect();
connection.query("select json_object('id', id) as player from tbl003_player where id = " + id,
function (error, results, fields) {
if (!error){
if (results.length > 0){
resolve(results[0]);
console.log("resultado: " + results[0]);
}else{
reject(new Error("No se ha encontrado ninguna jugadora con el ID: " + id));
}
}else{
reject(new Error("Se ha producido un error de acceso a BBDD"));
}
});
connection.end();
});
console.log("Salgo de la consulta");
myPromise.then((resolve) => {
console.log("resolve: " + JSON.stringify(resolve));
return resolve;
},(error) => {
console.log(error);
return error;
});
};
Edit I:
如果我为JSON.parse更改JSON.stringify,我在控制台中遇到此错误:
(node:31898) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse ()
at myPromise.then (/home/josecarlos/Workspace/graph-ql/primer-server-express/routes-api.js:68:21)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:31898) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing insideof an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:31898) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Edit II:
我已经修复了错误,当我返回JSON.parse(解析)但它不起作用:(我仍然在GraphiQL中收到此错误:
{
"data": {
"player": null
}
}
我管理myPromise的代码现在就是这个......
myPromise.then((resolve) => {
console.log("resolve: " + JSON.stringify(resolve));
data = JSON.parse(resolve);
return data;
},(error) => {
console.log(error);
return error;
}).catch(() => {
console.log("Entro dentro del catch");
});
Edit III:
我们用JSON.stringify(resolve)得到了这个字符串{"player":"{" id \ ": 11}"} . 我想我只需要回复{\ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _我怎样才能做到这一点?知道如何将json返回到GraphiQL吗?
Edit IV:
我修改了我的代码只返回带有返回解析的json(结果[0] .player)并且它不起作用!
这是我的实际代码:
let getPlayer = (args) => {
let id = args.id;
console.log("id: " + id);
let myPromise = new Promise((resolve, reject) => {
const connection = mysql.createConnection(config.ddbb_connection);
connection.connect();
connection.query("select json_object('id', id) as player from tbl003_player where id = " + id,
function (error, results, fields) {
if (!error){
if (results.length > 0){
resolve(results[0].player);
console.log("resultado: " + results[0].player);
}else{
reject(new Error("No se ha encontrado ninguna jugadora con el ID: " + id));
}
}else{
reject(new Error("Se ha producido un error de acceso a BBDD"));
}
});
connection.end();
});
console.log("Salgo de la consulta");
myPromise.then((resolve) => {
console.log("resolve: " + JSON.stringify(resolve));
return JSON.parse(resolve);
},(error) => {
console.log(error);
return error;
}).catch(() => {
console.log("Entro dentro del catch");
});
};