![](/img/trans.png)
[英]How to upload file with GraphQL and Apollo Upload Server in NodeJS?
[英]How to run nodejs with apollo graphql server
嗨,我在我的應用程序中使用Apollo GraphQL服務器,mongodb,nodejs。 我有架構和解析器以及movie.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查詢丟失。
有人知道我在做什么錯嗎?
/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
})
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.