0

嗨,我在我的应用程序中使用 Apollo GraphQL 服务器、mongodb、nodejs。我有架构和解析器以及 movies.js

schema.js

const typeDefs = `
    type Movie {
         _id: Int!
        name: String!
    }
    type Query {
        mv: [Movie]
    }
`;
module.exports = typeDefs;

解析器.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;

电影.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;

应用程序.js

var graph = require("./routes/movie");
app.use("/movie", movie);

当我尝试访问它http://localhost/movie时,我收到此错误 GET 查询丢失。

有谁知道我做错了什么?

4

1 回答 1

2

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

对于GET端点,您可以通过将查询作为(URL 转义)查询参数传递来做到这一点:

http://localhost/movie?query=...

(记录在这里:http ://dev.apollodata.com/tools/apollo-server/requests.html#getRequests )

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

http://localhost:3000/movie?query=%7B%20mv%20%7B%20name%20%7D%20%7D

但我建议设置一个POST端点,以便您可以发送POST请求

此外,您将不正确的属性名称传递给graphqlExpress,它应该是这样的:

router.get(
  "/",
  bodyParser.json(),
  graphqlExpress({
    schema : executableSchema
  })
);
于 2017-09-14T12:33:08.343 回答