Hi I am using Apollo GraphQL server, mongodb, nodejs in my application. I have schema and resolvers and 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);
When I try to access it http://localhost/movie then I am getting this error GET query missing.
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.
With GET
endpoints, you'd do that by passing the query as a (URL-escaped) query parameter:
http://localhost/movie?query=...
(documented here: http://dev.apollodata.com/tools/apollo-server/requests.html#getRequests )
To post the query { mv { name } }
, the URL would become this:
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 .
Additionally, you're passing an incorrect property name to graphqlExpress
, it should be this:
router.get(
"/",
bodyParser.json(),
graphqlExpress({
schema : executableSchema
})
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.