[英]I got error with graphql resolver in express
i'am trying to use graphql currently but i got this error (Cannot return null for non-nullable field Person.name.) I have tried to fix it but i couldn't plz help me我目前正在尝试使用 graphql 但我收到此错误(无法为不可为空的字段 Person.name 返回 null。)我已尝试修复它,但我无法帮助我
App.js应用程序.js
import resolvers from './graphql/resolvers';
const app = express();
const Router = express.Router();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: resolvers,
graphiql: true
}));
export default app;
schema.js schema.js
import { buildSchema } from 'graphql';
const schema = buildSchema(`
type Person {
name: String!
age: Int!
gender: String!
}
type Query {
people: [Person]!
person(id: Int!): Person
}
`);
export default schema;
resolvers.js解析器.js
import { people, getById } from '../fakedb';
const resolvers = {
Query {
people: () => people,
person: (_, { id }) => getById(id)
}
export default Query;
fakedb.js fakedb.js
export const people = [
{
id: 1,
name: "Andy",
age: 23,
gender: "male"
},
{
id: 2,
name: "Nam",
age: 26,
gender: "female"
},
{
id: 3,
name: "Jang",
age: 21,
gender: "male"
},
{
id: 4,
name: "Duk",
age: 23,
gender: "male"
}
];
export const getById = id => {
const filteredPeople = people.filter(person => person.id === id);
return filteredPeople[0];
};
I checked my db.js a lot of times, but i don't think it has a problem I am thinking the error comes from other place but i can't find it Would anyone help me?我检查了我的 db.js 很多次,但我认为它没有问题我认为错误来自其他地方但我找不到它有人可以帮助我吗? Thank you谢谢
Your code doesn't seem to have any issues, but are you running a query for the ids which are present in the fakedb people array?您的代码似乎没有任何问题,但是您是否正在对 fakedb people 数组中存在的 id 进行查询? Please share the query you are passing with id.请分享您通过 id 传递的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.