Apollo-server-express 返回 "cannot Get /" 而不是 Graphql 游乐场
Apollo-server-express returning "cannot Get /" instead of the Graphql playground
所以我的 server.js 和 schema.js 都有这段代码来自于 apollo doc 教程 https://www.apollographql.com/docs/tutorial/introduction/
server.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}`);
})
schema.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
我做的唯一调整是选择 import/exporting 模块的 ES 模块方法而不是文档中使用的 commonJS 方法,我认为这应该不是问题,它编译没有错误但不是让 graphql playground 查看我的模式,我得到一个 cannot Get /
我还没有完成解析器,但在这一点上,我相信我应该已经看到了运行中的操场。根据文档
使用applyMiddleware
时,如果不明确指定路径,默认为/graphql
。
这意味着您将在 localhost:${PORT}/graphql
而不是 localhost:${PORT}
访问您的端点和 GraphQL Playground 界面(这是您根据错误消息所做的)。
所以我的 server.js 和 schema.js 都有这段代码来自于 apollo doc 教程 https://www.apollographql.com/docs/tutorial/introduction/
server.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}`);
})
schema.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
我做的唯一调整是选择 import/exporting 模块的 ES 模块方法而不是文档中使用的 commonJS 方法,我认为这应该不是问题,它编译没有错误但不是让 graphql playground 查看我的模式,我得到一个 cannot Get /
我还没有完成解析器,但在这一点上,我相信我应该已经看到了运行中的操场。根据文档
使用applyMiddleware
时,如果不明确指定路径,默认为/graphql
。
这意味着您将在 localhost:${PORT}/graphql
而不是 localhost:${PORT}
访问您的端点和 GraphQL Playground 界面(这是您根据错误消息所做的)。