GraphQL 查询 return GraphiQL 中为 NULL,但数据出现在控制台日志中
GraphQL query return NULL in GraphiQL, but data appears in console log
我有一个 GraphQL API,它应该 return 来自 MySQL 和 PostGres 数据库的数据。在解析器中,我有 console.log 结果并且可以在终端中查看数据。
address: {
type: AddressType,
description: "An Address",
args: {
id: { type: GraphQLInt },
},
resolve: (parent, args) => {
// Make a connection to MySQL
let result;
connection.query(
`SELECT * FROM addresses WHERE id = ${args.id}`,
(err, res, fields) => {
if (err) console.log(err);
console.log("========");
console.log(res);
console.log("+++++++++");
console.log(res[0]);
// console.log(result);
}
);
return result;
},
},
在终端中,我 运行 在 GraphiQL 上查询时可以看到结果:
[nodemon] starting `node schema.js`
Server is running
Connected to PSQL database.
Connected to mySQL database.
========
[
RowDataPacket {
id: 1,
address_type: 'House',
status: 'Inactive',
entity: 'Building',
number_and_street: 'PO BOX 276',
suite_and_apartment: 'PO',
city: 'Ennis',
postal_code: '59729-0276',
country: 'USA',
notes: 'Dolorem quia repellendus et et nobis.',
created_at: 2020-12-18T05:00:00.000Z,
updated_at: 2021-05-21T04:00:00.000Z,
latitude: null,
longitude: null
}
]
+++++++++
RowDataPacket {
id: 1,
address_type: 'House',
status: 'Inactive',
entity: 'Building',
number_and_street: 'PO BOX 276',
suite_and_apartment: 'PO',
city: 'Ennis',
postal_code: '59729-0276',
country: 'USA',
notes: 'Dolorem quia repellendus et et nobis.',
created_at: 2020-12-18T05:00:00.000Z,
updated_at: 2021-05-21T04:00:00.000Z,
latitude: null,
longitude: null
}
但是在 GraphiQL 上,我得到的数据为空。
输入:
{
address(id: 1) {
address_type
}
}
输出:
{
"data": {
"address": null
}
}
我是 GraphQL 的新手。我能在这里错过什么?我试图从终端获取此信息以在 GraphiQL 查询时显示。只是想了解更多。
注意力不集中的经典问题:
您为控制台使用 res
变量。你没有为 result
.
赋值
并且return result
在执行查询之前执行。 (在你有数据的上下文之外)
请参阅文档了解如何使用 async / await
语法。您当前正在使用回调 - 这不是推荐的语法。
不确定,但应该类似于,您应该使用 async/await
,并等待 query
数据的 return。还要确保将值分配给您拥有的变量:
address: {
type: AddressType,
description: "An Address",
args: {
id: { type: GraphQLInt },
},
resolve: async (parent, args) => {
const result = await connection.query('SELECT * FROM addresses WHERE id = ', [args.id]);
return result;
},
},
最终对我有用的是以下内容:
address: {
type: AddressType,
description: "An Address",
args: {
id: { type: GraphQLInt },
},
resolve: async (parent, args) => {
const [rows, fields] = await promisePool.query(
`SELECT * FROM addresses WHERE id = ${args.id}`
);
console.log(rows[0]);
return rows[0];
},
},
我有一个 GraphQL API,它应该 return 来自 MySQL 和 PostGres 数据库的数据。在解析器中,我有 console.log 结果并且可以在终端中查看数据。
address: {
type: AddressType,
description: "An Address",
args: {
id: { type: GraphQLInt },
},
resolve: (parent, args) => {
// Make a connection to MySQL
let result;
connection.query(
`SELECT * FROM addresses WHERE id = ${args.id}`,
(err, res, fields) => {
if (err) console.log(err);
console.log("========");
console.log(res);
console.log("+++++++++");
console.log(res[0]);
// console.log(result);
}
);
return result;
},
},
在终端中,我 运行 在 GraphiQL 上查询时可以看到结果:
[nodemon] starting `node schema.js`
Server is running
Connected to PSQL database.
Connected to mySQL database.
========
[
RowDataPacket {
id: 1,
address_type: 'House',
status: 'Inactive',
entity: 'Building',
number_and_street: 'PO BOX 276',
suite_and_apartment: 'PO',
city: 'Ennis',
postal_code: '59729-0276',
country: 'USA',
notes: 'Dolorem quia repellendus et et nobis.',
created_at: 2020-12-18T05:00:00.000Z,
updated_at: 2021-05-21T04:00:00.000Z,
latitude: null,
longitude: null
}
]
+++++++++
RowDataPacket {
id: 1,
address_type: 'House',
status: 'Inactive',
entity: 'Building',
number_and_street: 'PO BOX 276',
suite_and_apartment: 'PO',
city: 'Ennis',
postal_code: '59729-0276',
country: 'USA',
notes: 'Dolorem quia repellendus et et nobis.',
created_at: 2020-12-18T05:00:00.000Z,
updated_at: 2021-05-21T04:00:00.000Z,
latitude: null,
longitude: null
}
但是在 GraphiQL 上,我得到的数据为空。 输入:
{
address(id: 1) {
address_type
}
}
输出:
{
"data": {
"address": null
}
}
我是 GraphQL 的新手。我能在这里错过什么?我试图从终端获取此信息以在 GraphiQL 查询时显示。只是想了解更多。
注意力不集中的经典问题:
您为控制台使用 res
变量。你没有为 result
.
并且return result
在执行查询之前执行。 (在你有数据的上下文之外)
请参阅文档了解如何使用 async / await
语法。您当前正在使用回调 - 这不是推荐的语法。
不确定,但应该类似于,您应该使用 async/await
,并等待 query
数据的 return。还要确保将值分配给您拥有的变量:
address: {
type: AddressType,
description: "An Address",
args: {
id: { type: GraphQLInt },
},
resolve: async (parent, args) => {
const result = await connection.query('SELECT * FROM addresses WHERE id = ', [args.id]);
return result;
},
},
最终对我有用的是以下内容:
address: {
type: AddressType,
description: "An Address",
args: {
id: { type: GraphQLInt },
},
resolve: async (parent, args) => {
const [rows, fields] = await promisePool.query(
`SELECT * FROM addresses WHERE id = ${args.id}`
);
console.log(rows[0]);
return rows[0];
},
},