Ali Usman
Ali Usman

Reputation: 13

TypeError: GraphQLObjectType is not a constructor

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 is not a constructor

Upvotes: 1

Views: 2059

Answers (2)

hrHVN
hrHVN

Reputation: 1

I believe your error is that you try to pass fields as a function an not an object in your Type definition.

Try this instead.

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

Upvotes: 0

Cody Geisler
Cody Geisler

Reputation: 8617

Your graphql import statement should be:

const graphql = require("graphql");

Upvotes: 3

Related Questions