错误:预期未定义为GraphQL模式

时间:2018-11-07 15:06:42

标签: reactjs graphql

我收到一条错误消息,内容为“错误:预期未定义为GraphQL模式。”请检查这是什么问题 当我移至localhost:3000 / graphiql时,它显示了上述错误。 也许我在做一些错误,请任何人检查并尽可能帮助我。

我的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}`);
})

我的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]
}


`;

我的Resolvers.js

exports.resolvers= {

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

};

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

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

1 个答案:

答案 0 :(得分:2)

您没有将架构传递给/graphql端点的配置。属性名称在javascript中区分大小写。更改此:

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

对此:

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

或将变量设为小写,然后执行:

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