'Error: Expected undefined to be a GraphQL schema

I am getting an error which says " Error: Expected undefined to be a GraphQL schema." Please check what issue is this when I move to localhost:3000/graphiql it shows the above error. Maybe i am doing some mistake please anyone check and help me if possible.

My Server.js

const express = require ('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');


//importing the Schema
const Story = require('./models/Story');
const User = require('./models/Story');


//Bring in GraphQl-Express middleware
const { graphiqlExpress, graphqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');

const { resolvers } = require('./resolvers');
const { typeDefs } = require('./schema');

//create schema
const Schema = makeExecutableSchema({
    typeDefs,
    resolvers
})


require('dotenv').config({ path: 'variables.env' });
// connecting mongoose to database
mongoose
.connect(process.env.MONGO_URI)
.then(()=> console.log('DB Connected'))
.catch(err => console.log(err));

//initializing express
const app = express();

//create GraphiQl application
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql'}))

//connect schemas with GraphQl
app.use('/graphql',bodyParser.json(), graphqlExpress({
    Schema,
    context:{
        Story,
        User
    }
}))

const PORT = process.env.PORT || 3000;

app.listen(PORT, ()=> {
    console.log(`Server Running on PORT ${PORT}`);
})

my Schema.js

exports.typeDefs = `


type Story {
    name: String!
    category: String!
    description: String!
    instructions: String!
    createDate: String
    likes: Int
    username: String
}



type User {
    username: String! @unique
    password: String!
    email: String!
    joinDate: String
    favorites: [Story]
}

type Query {
    getAllStories: [Story]
}


`;

My Resolvers.js

exports.resolvers= {

    Query:{
        getAllStories: ()=> {}
    }

};

My Story.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const StorySchema = new Schema({
    name: {
        type: String,
        required:true,
    },
    category:{
        type:String,
        required:true
    },
    description:{
        type:String,
        required:true
    },
    instructions:{
        type:String,
        required:true
    },
    createdDate:{
        type:Date,
        default: Date.now
    },
    likes:{
        type:Number,
        default:0
    },
    username:{
        type:String
    }

})

module.exports = mongoose.model('Story', StorySchema );

My User.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const UserSchema = new Schema({
    username:{
        type:String,
        required:true,
        unique:true
    },
    password:{
        type:String,
        required:true
    },
    email:{
        type:String,
        required:true
    },
    joinDate:{
        type:Date,
        default:Date.now
    },
    favorites:{
        type:[Schema.Types.ObjectId],
        refs:'Story'
    }
})

module.exports = mongoose.model('User', UserSchema);


Solution 1:[1]

You're not passing in a schema to your configuration for the /graphql endpoint. Property names are case-sensitive in javascript. Change this:

app.use('/graphql',bodyParser.json(), graphqlExpress({
  Schema,
  context:{
    Story,
    User
  }
}))

to this:

app.use('/graphql',bodyParser.json(), graphqlExpress({
  schema: Schema,
  context:{
    Story,
    User
  }
}))

or make your variable lowercase and then do:

app.use('/graphql',bodyParser.json(), graphqlExpress({
  schema,
  context:{
    Story,
    User
  }
}))

Solution 2:[2]

Feb, 2022 Update:

I downgraded graphql to 15.x then the problem was solved:

npm install [email protected]

If you don't mind the specific version:

npm install graphql@15

If you use "yarn":

yarn add [email protected]

If you don't mind the specific version:

yarn add graphql@15

Solution 3:[3]

Encountered the same problem. (the docs are outdated??)

Anyway, we now have to write something like

const response = await graphql({schema, source: query, rootValue});

instead of

const response = await graphql(schema, query, rootValue);

In other words, graphql now accepts only one parameter instead of multiple positional parameters.

Just jump to the GraphQLArgs definition to get more details.

While this is infact an improvement it should be documented accordingly.

As someone who started using graphql-js like 30 minutes ago and already have to deal with documentation issues like this just doesn't feel good.

Solution 4:[4]

I faced this with latest version of graphql and @nestjs/graphql :"9.1.2" version.

Downgraded the version to 15.8.0 and it's started working.

npm i [email protected]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Rearden
Solution 2
Solution 3 Page not found
Solution 4 Barnendu