如何 运行 nodejs 与 apollo graphql 服务器
How to run nodejs with apollo graphql server
您好,我在我的应用程序中使用 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 query missing。
有谁知道我做错了什么?
/movie
被声明为 GraphQL 端点,因此您必须向它发送 (GraphQL) 查询。
使用 GET
端点,您可以通过将查询作为(URL-转义)查询参数传递来实现:
http://localhost/movie?query=...
(此处记录:http://dev.apollodata.com/tools/apollo-server/requests.html#getRequests)
对于post查询{ mv { name } }
,URL会变成这样:
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
})
);
您好,我在我的应用程序中使用 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 query missing。
有谁知道我做错了什么?
/movie
被声明为 GraphQL 端点,因此您必须向它发送 (GraphQL) 查询。
使用 GET
端点,您可以通过将查询作为(URL-转义)查询参数传递来实现:
http://localhost/movie?query=...
(此处记录:http://dev.apollodata.com/tools/apollo-server/requests.html#getRequests)
对于post查询{ mv { name } }
,URL会变成这样:
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
})
);