[英]How to run nodejs with apollo graphql server
Hi I am using Apollo GraphQL server, mongodb, nodejs in my application. 嗨,我在我的应用程序中使用Apollo GraphQL服务器,mongodb,nodejs。 I have schema and resolvers and movies.js
我有架构和解析器以及movie.js
schema.js schema.js
const typeDefs = `
type Movie {
_id: Int!
name: String!
}
type Query {
mv: [Movie]
}
`;
module.exports = typeDefs;
resolvers.js 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 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 app.js
var graph = require("./routes/movie");
app.use("/movie", movie);
When I try to access it http://localhost/movie then I am getting this error GET query missing. 当我尝试访问它http:// localhost / movie时,出现此错误GET查询丢失。
Does anyone know what I am doing wrong ? 有人知道我在做什么错吗?
/movie
is declared as a GraphQL endpoint, so you have to send a (GraphQL) query to it. /movie
被声明为GraphQL端点,因此您必须向其发送(GraphQL)查询。
With GET
endpoints, you'd do that by passing the query as a (URL-escaped) query parameter: 使用
GET
端点,您可以通过将查询作为(转义的URL)查询参数传递来实现:
http://localhost/movie?query=...
(documented here: http://dev.apollodata.com/tools/apollo-server/requests.html#getRequests ) (在此处记录: http : //dev.apollodata.com/tools/apollo-server/requests.html#getRequests )
To post the query { mv { name } }
, the URL would become this: 要发布查询
{ mv { name } }
,URL将变为:
http://localhost:3000/movie?query=%7B%20mv%20%7B%20name%20%7D%20%7D
But I would suggest setting up a POST
endpoint so you can send POST
requests . 但是我建议设置一个
POST
端点,以便您可以发送POST
请求 。
Additionally, you're passing an incorrect property name to graphqlExpress
, it should be this: 此外,您将不正确的属性名称传递给
graphqlExpress
,应该是这样的:
router.get(
"/",
bodyParser.json(),
graphqlExpress({
schema : executableSchema
})
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.