[英]GraphQL / Apollo: “No Schemas available”. NodeJS
I`m trying to work with GraphQL/Apollo, and my "Documentation Explorer" loading infinitely and doesnt show anything, and i can't make any queries. 我正在尝试使用GraphQL / Apollo,并且我的“文档资源管理器”无限加载且未显示任何内容,并且我无法进行任何查询。
After few minutes I getting an typeError "Failed to fetch"
. 几分钟后,我收到一个typeError
"Failed to fetch"
。
Here's my graphql/index.js
file: 这是我的
graphql/index.js
文件:
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const User = require('../models/user.model');
const typeDefs = `
type Query {
users: [User]
}
type User {
id: ID!
name: String
email: String
password: String
}
`;
const resolvers = {
Query: {
users() {
return User.find({});
}
}
}
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
module.exports = (app) => {
app.use('/graphql', () => { }, graphqlExpress({ schema }));
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
};
Console and DevTools both clear. 控制台和DevTools都清除。 Can someone explain, what`s wrong?
有人可以解释,怎么了? Thank you !
谢谢 !
It's a little unclear what you were trying to accomplish, but you've added a middleware to your /graphql
route that does nothing: 尚不清楚您要完成什么,但是您已经在
/graphql
路由中添加了一个不执行任何操作的中间件:
app.use('/graphql', () => { }, graphqlExpress({ schema }))
The function you've inserted gets called anytime something hits the /graphql
route, and because your function doesn't call next
or end the response, the next middleware ( graphqlExpress
) is never called and the request just hangs. 每当有什么东西碰到
/graphql
路由时,都会调用您插入的函数,并且由于您的函数不会调用next
或结束响应,因此永远不会调用下一个中间件( graphqlExpress
),并且请求只会挂起。
The other problem is that graphqlExpress
requires bodyParser middleware to run before it's called. 另一个问题是
graphqlExpress
要求bodyParser中间件在被调用之前运行。 That means you can do either: 这意味着您可以执行以下任一操作:
const bodyParser = require('body-parser')
// Option A -- body parser will run prior to all routes after this point
app.use(bodyParser.json())
// Option B -- body parser will only run for the graphql route
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }))
If you fail to include bodyParser, graphqlExpress will typically complain and tell you as much as long you actually reach it in the first place. 如果您没有包含bodyParser,graphqlExpress通常会抱怨并告诉您与实际达到目标一样多的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.