我收到一条错误消息“错误:预期未定义为 GraphQL 架构。”请检查一下这是什么问题 当我移动到 localhost:3000/graphiql 时,它显示上述错误。 也许我做错了,请任何人检查并帮助我(如果可能的话)。
我的服务器.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: ()=> {}
}
};
我的故事.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 );
我的用户.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);
我将graphql降级为15.x然后问题就解决了:
npm install [email protected]
如果你不介意具体版本:
npm install graphql@15
如果您使用“纱线”:
yarn add [email protected]
如果你不介意具体版本:
yarn add graphql@15
遇到同样的问题。 (文档已经过时了?)
无论如何,我们现在必须写类似的东西
const response = await graphql({schema, source: query, rootValue});
而不是
const response = await graphql(schema, query, rootValue);
换句话说,
graphql
现在只接受一个参数,而不是多个位置参数。
只需跳转至
GraphQLArgs
定义即可获取更多详细信息。
虽然这实际上是一种改进,但应该相应地记录下来。
对于 30 分钟前开始使用
graphql-js
并且已经必须处理这样的文档问题的人来说,感觉不太好。
您没有将架构传递到
/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
}
}))