【问题标题】:Cannot set property 'name' of undefined (Nodejs Graphql)无法设置未定义的属性“名称”(Nodejs Graphql)
【发布时间】:2019-06-23 13:10:33
【问题描述】:

我正在尝试通过制作一个简单的 Node 应用程序来学习 Graphql,并使用一些虚拟数据进行测试。

当我尝试运行 Node 时,出现以下错误

TypeError: Cannot set property 'name' of undefined
at GraphQLObjectType (/Users/BorisGrunwald/Desktop/programmering/Javascript/Node/graphQLServer/server/node_modules/graphql/type/definition.js:480:15)
at Object.<anonymous> (/Users/BorisGrunwald/Desktop/programmering/Javascript/Node/graphQLServer/server/schema/schema.js:22:23)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/Users/BorisGrunwald/Desktop/programmering/Javascript/Node/graphQLServer/server/app.js:6:20)

这是我项目中仅有的两个文件:

app.js

const express = require('express');
const graphqlHTTP = require('express-graphql');

const app = express();

const schema = require('./schema/schema');

app.use('/graphql',graphqlHTTP({
    schema
}));

app.listen(4000,() => {
    console.log('listening on port 4000');
});

schema.js

const graphql = require('graphql');   

const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;


const dummyData = [
    {name: 'Name of the Wind', genre: 'Fantasy', id:'1'},
    {name: 'The Final Empire', genre: 'Fantasy', id:'2'},
    {name: 'The Long Earth', genre: 'Sci-Fi', id:'3'}
];

const BookType = new GraphQLObjectType({
    name:'Book',
    fields: () => ({
        id: {type : GraphQLString},
        name: {type: GraphQLString},
        genre: {type: GraphQLString}
    })
});

const RootQuery = GraphQLObjectType({
    name:'RootQueryType',
    fields: {
        book:{
            type:'BookType',
            args: {id:{type:GraphQLString}},
            resolve(parent,args) {
                return dummyData.find(book => book.id === args.id)
            }
        }
    }
});


module.exports = new GraphQLSchema({
    query:RootQuery
});

你们中的任何人都能发现错误吗?提前致谢。

【问题讨论】:

    标签: javascript node.js graphql-js


    【解决方案1】:

    在创建 RootQuery 对象时,您只是错过了 GraphQLObjectType 类的“new”关键字。 我已经解决了,请看一下

    
    const graphql = require('graphql');   
    
    const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;
    
    
    const dummyData = [
        {name: 'Name of the Wind', genre: 'Fantasy', id:'1'},
        {name: 'The Final Empire', genre: 'Fantasy', id:'2'},
        {name: 'The Long Earth', genre: 'Sci-Fi', id:'3'}
    ];
    
    const BookType = new GraphQLObjectType({
        name:'Book',
        fields: () => ({
            id: {type : GraphQLString},
            name: {type: GraphQLString},
            genre: {type: GraphQLString}
        })
    });
    
    const RootQuery = new GraphQLObjectType({
        name:'RootQueryType',
        fields: {
            book:{
                type:'BookType',
                args: {id:{type:GraphQLString}},
                resolve(parent,args) {
                    return dummyData.find(book => book.id === args.id)
                }
            }
        }
    });
    
    
    module.exports = new GraphQLSchema({
        query:RootQuery
    });
    

    【讨论】:

    • 非常感谢,就是这样。
    • type:'BookType' 应该是 type: BookType
    猜你喜欢
    • 2019-01-31
    • 2021-08-03
    • 2019-02-09
    • 2018-07-08
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多