Apollo-server-express 返回“无法获取/”而不是 Graphql 游乐场

Jud*_*ihi 3 node.js express graphql apollo-server

所以我按照阿波罗文档教程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}`);
})
Run Code Online (Sandbox Code Playgroud)

架构.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
Run Code Online (Sandbox Code Playgroud)

我所做的唯一调整是选择导入/导出模块的 ES 模块方法,而不是文档中使用的 commonJS 方式,我认为这不应该是问题,它编译时没有错误,但不是让 graphql 游乐场看到我的架构,我得到一个cannot Get /

我还没有完成解析器,但此时,我相信我应该已经看到游乐场正在运行。根据文档

Dan*_*den 5

使用时applyMiddleware,如果没有显式指定路径,则/graphql默认为路径。

这意味着您将访问您的端点和 GraphQL Playground 接口localhost:${PORT}/graphql,而不是localhost:${PORT}(这是您根据错误消息正在执行的操作)。