Node.js 如何使用apollo graphql服务器运行nodejs

Node.js 如何使用apollo graphql服务器运行nodejs,node.js,express,apollo,apollo-client,apollo-server,Node.js,Express,Apollo,Apollo Client,Apollo Server,您好,我在应用程序中使用Apollo GraphQL服务器、mongodb和nodejs。我有schema、resolvers和movies.js schema.js const typeDefs = ` type Movie { _id: Int! name: String! } type Query { mv: [Movie] } `; module.exports = typeDefs; resolvers

您好,我在应用程序中使用Apollo GraphQL服务器、mongodb和nodejs。我有schema、resolvers和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);
当我试图访问它,然后我得到了这个错误得到查询失踪


有人知道我做错了什么吗

/movie
被声明为GraphQL端点,因此您必须向其发送(GraphQL)查询

使用
GET
endpoints,可以通过将查询作为(URL转义)查询参数传递:

http://localhost/movie?query=...
(此处记录:)

要发布查询
{mv{name}}
,URL将变为:

http://localhost:3000/movie?query=%7B%20mv%20%7B%20name%20%7D%20%7D
但我建议您设置一个
POST
端点,以便发送

此外,您向
graphqlExpress
传递的属性名称不正确,应该是:

router.get(
  "/",
  bodyParser.json(),
  graphqlExpress({
    schema : executableSchema
  })
);

/movie
被声明为GraphQL端点,因此您必须向其发送(GraphQL)查询

使用
GET
endpoints,可以通过将查询作为(URL转义)查询参数传递:

http://localhost/movie?query=...
(此处记录:)

要发布查询
{mv{name}}
,URL将变为:

http://localhost:3000/movie?query=%7B%20mv%20%7B%20name%20%7D%20%7D
但我建议您设置一个
POST
端点,以便发送

此外,您向
graphqlExpress
传递的属性名称不正确,应该是:

router.get(
  "/",
  bodyParser.json(),
  graphqlExpress({
    schema : executableSchema
  })
);

我正在使用这个{mv[{name}]}并得到
{“errors”:[{“message”:“语法错误GraphQL请求(1:9)预期名称,找到[\n\n1:query{mv[{name}]}\n^\n”,“locations:[{“line”:1,“column”:9}]}
请参见
{“errors”:[{“message GraphQL请求(1:1)语法错误无法解析意外字符\%”\“\n\n1:%7B%20mv%20%7B%20name%20%7%E2%80%8C%E2%80%8BD%20%7D\n^\n”,“位置:[{“行”:1,“列”:1}]}
@Nancy查看我的更新答案。我发布的URL中有一些隐藏字符不应该在那里,我还发现了另一个问题。谢谢。mv是数组类型。它应该是{mv[{name}}]?我试图记录解析程序
db.collection(“movie”).find({}).toArray(函数(err,result){if(err)throw err;console.log(result);return JSON.stringify(result);db.close();});
它打印
[{u id:59ba279e0a605b4a3bf93e76,name:'Iron Man',year:2017}]
但它向客户端发送
{“数据”:{“mv”:null}
我的解析器异步或等待出错?@Nancy尝试返回
结果
,而不调用
JSON.stringify()
。另外,
db.close()
将永远不会被调用,因为您会在它之前返回。哦,而且您将回调与解析程序中的承诺混合在一起,这也不是一件好事。我将更新我的答案。我正在使用此{mv[{name}}}并获得
{errors:[{“message”:“语法错误图形SQL请求(1:9)预期名称,找到[\n\n1:query{mv[{name}]}}}}\n^\n”,“位置”“:[{“行”:1,“列”:9}]}]}
请参见
{“错误”:[{“消息”:“语法错误图形SQL请求(1:1)无法分析意外字符\“%\”\n\n1:%7B%20mv%20%7B%20name%20%7%E2%80%8C%E2%80%8BD%20%7D\n\n”,“位置”:[^“行”:1,“列”:1}]}”
@Nancy查看我的更新答案。我发布的URL中有一些隐藏字符不应该存在,我还发现了另一个问题。谢谢。mv是数组类型。它应该是{mv[{name}]}吗?我试图记录解析程序的结果
db.collection(“movie”).find({}).toArray(函数(err,result){if(err)throw err;console.log(result);return JSON.stringify(result);db.close();})
它打印
[{{u id:59ba279e0a605b4a3bf93e76,name:'Iron Man',year:2017}]
但它发送给客户端
{data:{“mv null}
我的解析程序async或await有什么问题吗?@Nancy尝试返回
结果
,但不调用它上的
JSON.stringify()
。此外,
db.close()
将永远不会被调用,因为你在它之前返回。哦,你在解析程序中混合了回调和承诺,这也不是一件好事。我将更新我的答案。