簡體   English   中英

我在快遞中遇到 graphql 解析器錯誤

[英]I got error with graphql resolver in express

我目前正在嘗試使用 graphql 但我收到此錯誤(無法為不可為空的字段 Person.name 返回 null。)我已嘗試修復它,但我無法幫助我

應用程序.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

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;

解析器.js

import { people, getById } from '../fakedb';

const resolvers = {
    Query {
        people: () => people,
        person: (_, { id }) => getById(id)
    } 
export default Query;

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];
};

我檢查了我的 db.js 很多次,但我認為它沒有問題我認為錯誤來自其他地方但我找不到它有人可以幫助我嗎? 謝謝

您的代碼似乎沒有任何問題,但是您是否正在對 fakedb people 數組中存在的 id 進行查詢? 請分享您通過 id 傳遞的查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM