NodeJS TypeError:GraphQLObjectType不是构造函数

xkrw2x1b  于 2023-06-05  发布在  Node.js
关注(0)|答案(2)|浏览(181)

schema.js

const { graphql } = require("graphql");
const _ = require('lodash');

const { GraphQLSchema, GraphQLObjectType, GraphQLString } = graphql;

var books = [
    {name: 'Wind',genre:'Fantasy',id:'1'},
    {name: 'Final',genre:'Fantasy',id:'2'},
    {name: 'Long',genre:'Sci-Fi',id:'3'},
];

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

const BookType = new GraphQLObjectType({ ^
TypeError:GraphQLObjectType不是构造函数

oalqel3c

oalqel3c1#

你的graphql import语句应该是:

const graphql = require("graphql");
xoefb8l8

xoefb8l82#

我相信你的错误是试图将字段作为函数传递,而不是类型定义中的对象。
试试这个

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

相关问题