简体   繁体   中英

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

App.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;

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

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? 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? Please share the query you are passing with id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM