node.js - Apollo 服务器游乐场无法在 heroku 上运行(在本地工作)
问题描述
我正在尝试在 heroku 上部署运行节点 js 应用程序,它已成功部署,但 Playground 似乎不起作用。
我通过设置来解决这个解决方案introspection: true
:https ://github.com/apollographql/apollo-server/issues/1718但这似乎也不起作用。
heroku 日志
我的代码:
包.json
{
"name": "travelindiaserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf build && mkdir build",
"build-babel": "babel -d ./build ./src -s",
"build": "npm run clean && npm run build-babel",
"start": "npm run build && node ./build/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^2.4.8",
"babel-preset-env": "^1.7.0",
"express": "^4.16.4",
"graphql": "^14.2.1"
},
"devDependencies": {
"babel-cli": "^6.26.0"
}
}
index.js
import express from "express";
import { ApolloServer } from "apollo-server-express";
import typeDefs from "./schema";
import resolvers from "./resolvers";
import models from "./models";
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
context: { models },
introspection: true
});
server.applyMiddleware({ app });
const PORT = process.env.PORT || 5000;
app.listen(PORT, () =>
console.log(
` Server ready at http://localhost:${PORT}${server.graphqlPath}`
)
);
模型/index.js
const cities = [{ name: "City 1" }, { name: "City 2" }];
export default {
cities
};
解析器/cityResolvers.js
export default {
Query: {
cities: (parent, args, { models }) => {
return models.cities;
}
}
};
解析器/index.js
import cityResolvers from "./cityResolvers";
export default [cityResolvers];
架构/city.js
import { gql } from "apollo-server-express";
export default gql`
extend type Query {
cities: [City]
}
type City {
name: String
}
`;
架构/index.js
import { gql } from "apollo-server-express";
import citySchema from "./city";
const linkSchema = gql`
type Query {
_: Boolean
}
type Mutation {
_: Boolean
}
type Subscription {
_: Boolean
}
`;
export default [linkSchema, citySchema];
解决方案
默认情况下,Apollo 服务器在运行NODE_ENV=production
. 由于 Heroku 在您使用 Playground 时默认设置它node buildpack
是禁用的。
为了规避您需要传递给 apollo-server 2 选项的问题:
const server = new ApolloServer({
introspection: true,
playground: true
});
推荐阅读
- python - 熊猫根据时间戳合并
- python - 如何在熊猫中按天和月分组?
- swiftui - 以非模态方式呈现视图
- bash - 如何在 bash 脚本中的远程服务器上使用 SSH 运行多个命令?
- c++ - Visual Studio 测试资源管理器找不到我的 Boost 测试
- r - 查找没有包的 1:n 数字的所有唯一组合
- react-native - 尝试升级 React Native,得到令人困惑的信息
- spring - 一堆更改后如何解决 UnsatisfiedDependencyException
- gradle - 如何为 Netbeans 启用 gradle?
- hyperledger-fabric - 我们能否在生产中从基于 kafta 的订购服务更改为基于 raft 的订购,反之亦然在 hyperledger 结构中?