javascriptreactjsnode.jsgraphql

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

  • 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
      }
    }))