简体   繁体   中英

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);

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.

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

Dec, 2021 Update:

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

npm install graphql@15.8.0

If you don't mind the specific version :

npm install graphql@15

If you use "yarn" :

yarn add graphql@15.8.0

If you don't mind the specific version :

yarn add graphql@15

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 graphql@15.8.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM