简体   繁体   English

我在快递中遇到 graphql 解析器错误

[英]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.

相关问题 如何在 JavaScript 中调试 GraphQL 解析器? - How do I debug a GraphQL resolver in JavaScript? 使用 graphql-sequelize 解析器(Node.js、express-graphql)限制递归 GraphQL 模式查询的深度 - Limit Depth on Recursive GraphQL Schema Query using graphql-sequelize resolver (Node.js, express-graphql) 尝试连接到 prismic 时出现“'[链接解析器错误] 未知类型\n'” - Trying to connect to prismic got " '[Link resolver error] Unknown type\n' " 如何在GraphQL解析器中访问请求对象(使用Apollo-Server-Express) - How to access the request object inside a GraphQL resolver (using Apollo-Server-Express) 在任何父解析器中使用子 graphql 解析器 - 解析器链接 - use child graphql resolver inside any parent resolver - resolver chaining 为什么会出现错误“必须提供查询字符串”。express-graphql? - Why I'm getting the error “Must provide query string.” express-graphql? 用于授权检查的GraphQL解析器中间件 - GraphQL resolver middleware for auth check Apollo GraphQL - 未触发解析器 - Apollo GraphQL - resolver not being triggered 使用GraphQL在单个字段上设置解析器 - Set resolver on individual field with GraphQL 如何查询firestore()中的graphQL解析器? - How to query firestore() for graphQL resolver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM