node.js - 尝试获取特定的单个记录时,GraphiQL 返回空值
问题描述
我正在尝试构建一个基本的 GraphQL 模式。我有一个在端口 3000 上运行的 JSON 服务器,其中包含我想使用 GraphiQL 检索的数据。
以下是我为此编写的代码:
const CustomerType = new GraphQLObjectType({
name:'Customer',
fields:() => ({
id: { type:GraphQLString },
name: { type:GraphQLString }
})
});
const RootQuery = new GraphQLObjectType({
name : 'RootQueryType',
fields:{
customer : {
type : CustomerType,
args:{
id:{type:GraphQLString}
},
resolve(parentValue,args){
return axios.get('http://localhost:3000/customers/?id='+args.id)
.then(res => res.data);
}
}
}
});
这就是我写查询的方式。
customer(id : "123"){
id,
name
}
这是正在生成的输出。
{
"data": {
"customer": {
"id": null,
"name": null,
}
}
}
但是,当我尝试使用 console.log 验证是否从 data.json 文件中获取正确的数据时,生成的输出包含正确的非空值。有人可以帮我解决这个问题吗?此外,当我尝试使用 GraphQLList 一次获取所有客户的列表时,也没有发生错误,如下所示:
customers:{
type: new GraphQLList(CustomerType),
resolve(parentValue,args){
return axios.get('http://localhost:3000/customers/')
.then(res => res.data);
}
解决方案
推荐阅读
- bash - 查找子字符串中数字最大的命令输出
- java - 无法反序列化“org.json.JSONObject”的实例
- c - 如何修复:操作码未正确读取
- java - Javac 禁止调用特定的 JDK 方法
- php - 如何使用我的搜索按钮显示数据库行?
- javascript - 如何通过stringify使Date对象toString在javascript中使用常规toString而不是toStringISO?
- java - 在 DataFrame 中的分组数据中查找比率
- python-3.x - 如何在 Python 数据框中找到最近的坐标
- c# - 使用扫描仪页面后崩溃
- stream - 如何在方案中制作无限的 x 和 sin(x) 流?