gpt4 book ai didi

node.js - Apollo-server-express 返回 "cannot Get/"而不是 Graphql playground

转载 作者:行者123 更新时间:2023-12-05 02:50:33 26 4
gpt4 key购买 nike

所以我从 apollo 文档教程 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 playground查看我的模式,我得到一个 cannot Get/

我还没有完成解析器,但在这一点上,我相信我应该已经看到了 playground 的运行情况。根据文档

最佳答案

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

这意味着您将在 localhost:${PORT}/graphql 访问您的端点和 GraphQL Playground 界面,而不是 localhost:${PORT}(这是根据错误信息你在做什么)。

关于node.js - Apollo-server-express 返回 "cannot Get/"而不是 Graphql playground,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63633577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com