您好我在我的应用程序中使用Apollo GraphQL服务器,mongodb,nodejs。我有架构和解析器和movies.js
schema.js
const typeDefs = `
type Movie {
_id: Int!
name: String!
}
type Query {
mv: [Movie]
}
`;
module.exports = typeDefs;
resolvers.js
const mongoDB = require("../mongoose/connect");
const resolvers = {
Query: {
mv: async (root, args, context) => {
return await mongoDB.connectDB(async err => {
if (err) throw err;
const db = mongoDB.getDB();
db
.collection("movie")
.find({})
.toArray(function(err, result) {
if (err) throw err;
return JSON.stringify(result);
db.close();
});
});
}
}
};
module.exports = resolvers;
movie.js
var express = require("express");
var bodyParser = require("body-parser");
const { graphqlExpress } = require("apollo-server-express");
const { makeExecutableSchema } = require("graphql-tools");
const createResolvers = require("../graphql/resolvers");
const typeDefs = require("../graphql/schema");
const resolvers = require("../graphql/resolvers");
var router = express.Router();
const executableSchema = makeExecutableSchema({
typeDefs,
resolvers
});
router.get(
"/",
bodyParser.json(),
graphqlExpress({
executableSchema
})
);
module.exports = router;
app.js
var graph = require("./routes/movie");
app.use("/movie", movie);
当我尝试访问http://localhost/movie时,我收到此错误GET查询丢失。
有谁知道我做错了什么?
答案 0 :(得分:2)
/movie
被声明为GraphQL端点,因此您必须向其发送(GraphQL)查询。
使用GET
个端点,您可以将查询作为(URL转义的)查询参数传递:
http://localhost/movie?query=...
(记录在此:http://dev.apollodata.com/tools/apollo-server/requests.html#getRequests)
要发布查询{ mv { name } }
,网址将变为:
http://localhost:3000/movie?query=%7B%20mv%20%7B%20name%20%7D%20%7D
但我建议您设置一个POST
端点,以便发送POST
requests。
此外,您将错误的属性名称传递给graphqlExpress
,它应该是:
router.get(
"/",
bodyParser.json(),
graphqlExpress({
schema : executableSchema
})
);