[英]GraphQL Error Handling with ApolloClient and Apollo-Server-Express
[英]Apollo-server-express returning “cannot Get /” instead of the Graphql playground
所以我按照阿波羅文檔教程https://www.apollographql.com/docs/tutorial/introduction/為我的 server.js 和 schema.js 提供了這段代碼
服務器.js
import apollo from "apollo-server-express";
const { ApolloServer } = apollo;
import express from "express";
import typeDefs from "./schema.js";
const app = express();
const server = new ApolloServer({
typeDefs,
playground: true,
});
server.applyMiddleware({ app });
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`operating on port ${PORT}`);
})
架構.js
import pkg from "apollo-server-express"
const { gql } = pkg
// defining schema
const typeDefs = gql`
type Launch {
id: ID!
site: String
mission: Mission
rocket: Rocket
isBooked: Boolean!
}
type Rocket {
id: ID!
name: String
type: String
}
type User {
id: ID!
email: String!
trips: [Launch]!
}
type Mission {
name: String
missionPatch(size: PatchSize): String
}
enum PatchSize {
SMALL
LARGE
}
type Query {
launches: [Launch]!
launch(id: ID!): Launch
me: User
}
type Mutation {
bookTrips(launchIds: [ID]!): TripUpdateResponse!
cancelTrip(launchId: ID!): TripUpdateResponse!
login(email: String): String # login token
}
type TripUpdateResponse {
success: Boolean!
message: String
launches: [Launch]
}
`
export default typeDefs
我所做的唯一調整是選擇 ES 模塊導入/導出模塊的方法,而不是文檔中使用的 commonJS 方式,我認為這應該不是問題,它編譯沒有錯誤,但不是讓 graphql 操場看到我的架構,我得到一個cannot Get /
我還沒有完成解析器,但在這一點上,我相信我應該已經看到 Playground 的運行了。 根據文檔
使用applyMiddleware
,如果沒有明確指定路徑,則默認為/graphql
。
這意味着您將在localhost:${PORT}/graphql
訪問端點和 GraphQL Playground 接口,而不是localhost:${PORT}
(這是您根據錯誤消息執行的操作)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.