【问题标题】:Apollo-server-express returning "cannot Get /" instead of the Graphql playgroundApollo-server-express 返回“无法获取 /”而不是 Graphql 操场
【发布时间】:2020-12-17 09:00:51
【问题描述】:

所以我的 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

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

我还没有完成解析器,但在这一点上,我相信我应该已经看到操场在行动了。根据文档

【问题讨论】:

    标签: node.js express graphql apollo-server


    【解决方案1】:

    使用applyMiddleware时,如果不明确指定路径,默认为/graphql

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

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 2022-11-09
      • 2019-02-13
      • 2019-09-01
      • 2019-09-14
      • 2019-12-19
      • 2019-07-19
      • 2021-04-20
      相关资源
      最近更新 更多