Apollo服务器游乐场无法在Heroku上运行(在本地工作)

时间:2019-04-20 16:18:16

标签: node.js heroku ecmascript-6 apollo-server

我正在尝试在heroku上部署运行节点js应用程序,它已成功部署,但是操场似乎无法正常工作。

我通过设置en_UShttps://github.com/apollographql/apollo-server/issues/1718来解决此问题,但这似乎也不起作用。

heroku日志

heroku logs

我的代码:

package.json

introspection: true

index.js

{
  "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"
  }
}

models / 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}`
  )
);

resolvers / cityResolvers.js

const cities = [{ name: "City 1" }, { name: "City 2" }];

export default {
  cities
};

resolvers / index.js

export default {
  Query: {
    cities: (parent, args, { models }) => {
      return models.cities;
    }
  }
};

schema / city.js

import cityResolvers from "./cityResolvers";

export default [cityResolvers];

schema / index.js

import { gql } from "apollo-server-express";

export default gql`
  extend type Query {
    cities: [City]
  }

  type City {
    name: String
  }
`;

1 个答案:

答案 0 :(得分:1)

默认情况下,当使用NODE_ENV=production运行时,Apollo服务器会关闭自省和游乐场。由于Heroku在使用node buildpack时会默认设置它,因此您的游乐场已禁用。

要避免该问题,您需要传递给apollo-server 2选项:

const server = new ApolloServer({
  introspection: true,
  playground: true
});