当我通过 graphiql 发送查询时,graphql JS returns null
graphql JS returns null when i send a query via graphiql
我想我已经彻底检查了这段代码,但找不到错误请帮忙!!
当我查询 id
时,我得到一个 null return
还有,vscode告诉我我parent中的resolve函数没有用。我在这里做错了什么??
快递---
const express = require('express');
const graphqlHTTP = require('express-graphql');
const schema = require('./schema/schema')
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql:true
}));
app.listen(1991, ()=> {
console.log('1991 live');
});
架构--
const graphql = require('graphql');
const _= require('lodash');
const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLInt,
GraphQLSchema } = graphql;
var drugs = [
{name: 'Paracetamol', price: 200, qty: 20, drugid: 1},
{name: 'Amoxicilin', price: 700, qty: 10, drugid: 2}
];
var categories = [
{name: 'Painkiller', id: 1},
{name: 'Anti-Biotic', id: 2}
];
const DrugType = new GraphQLObjectType({
name: 'Drug',
fields: () => ({
name: {type: GraphQLString},
price: {type: GraphQLInt},
qty: {type: GraphQLInt},
drugid: {type: GraphQLID}
})
});
const DrugCategory = new GraphQLObjectType({
name: 'Category',
fields: () => ({
name: {type: GraphQLString},
id: {type: GraphQLID}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
drug:{
type: DrugType,
args:{id:{type: GraphQLID}},
resolve(parent, args){
return _.find(drugs, {drugid:args.id});
}
},
category:{
type: DrugCategory,
args:{id: {type: GraphQLID}},
resolve(parent,args){
return _.find(categories, {id:args.id});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
这是我在 graphiql 中查询时得到的结果
查询--
{
drug(id: 1){
name
}
}
结果--
{
"data": {
"drug": null
}
}
您传递给 lodash 的 find
方法的对象是 { id: args.id }
—— 这意味着您正在寻找具有 id
属性 的对象匹配 args.id
。但是,drugs
数组中的 none 个对象具有 id
属性。更新您的数组或更改您的搜索条件(即 { drugid: args.id }
)。
此外,id
参数的类型是 GraphQLID
,它被视为字符串。这意味着 find
正在将字符串参数与数字 属性 值进行比较。要么将参数的类型更改为 GraphQLInt
、 或 将 args.id
换成 Number.parseInt
、 或 更改 drugId
值到字符串。
我想我已经彻底检查了这段代码,但找不到错误请帮忙!! 当我查询 id
时,我得到一个 null return还有,vscode告诉我我parent中的resolve函数没有用。我在这里做错了什么??
快递---
const express = require('express');
const graphqlHTTP = require('express-graphql');
const schema = require('./schema/schema')
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql:true
}));
app.listen(1991, ()=> {
console.log('1991 live');
});
架构--
const graphql = require('graphql');
const _= require('lodash');
const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLInt,
GraphQLSchema } = graphql;
var drugs = [
{name: 'Paracetamol', price: 200, qty: 20, drugid: 1},
{name: 'Amoxicilin', price: 700, qty: 10, drugid: 2}
];
var categories = [
{name: 'Painkiller', id: 1},
{name: 'Anti-Biotic', id: 2}
];
const DrugType = new GraphQLObjectType({
name: 'Drug',
fields: () => ({
name: {type: GraphQLString},
price: {type: GraphQLInt},
qty: {type: GraphQLInt},
drugid: {type: GraphQLID}
})
});
const DrugCategory = new GraphQLObjectType({
name: 'Category',
fields: () => ({
name: {type: GraphQLString},
id: {type: GraphQLID}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
drug:{
type: DrugType,
args:{id:{type: GraphQLID}},
resolve(parent, args){
return _.find(drugs, {drugid:args.id});
}
},
category:{
type: DrugCategory,
args:{id: {type: GraphQLID}},
resolve(parent,args){
return _.find(categories, {id:args.id});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
这是我在 graphiql 中查询时得到的结果
查询--
{
drug(id: 1){
name
}
}
结果--
{
"data": {
"drug": null
}
}
您传递给 lodash 的 find
方法的对象是 { id: args.id }
—— 这意味着您正在寻找具有 id
属性 的对象匹配 args.id
。但是,drugs
数组中的 none 个对象具有 id
属性。更新您的数组或更改您的搜索条件(即 { drugid: args.id }
)。
此外,id
参数的类型是 GraphQLID
,它被视为字符串。这意味着 find
正在将字符串参数与数字 属性 值进行比较。要么将参数的类型更改为 GraphQLInt
、 或 将 args.id
换成 Number.parseInt
、 或 更改 drugId
值到字符串。