问题
I am Trying to deploy run node js application on heroku, it is deployed successfully but playground doesn't seems to work.
I came around this solution by setting introspection: true
: https://github.com/apollographql/apollo-server/issues/1718 but that also doesn't seems to work.
heroku logs
My code:
package.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}`
)
);
models/index.js
const cities = [{ name: "City 1" }, { name: "City 2" }];
export default {
cities
};
resolvers/cityResolvers.js
export default {
Query: {
cities: (parent, args, { models }) => {
return models.cities;
}
}
};
resolvers/index.js
import cityResolvers from "./cityResolvers";
export default [cityResolvers];
schema/city.js
import { gql } from "apollo-server-express";
export default gql`
extend type Query {
cities: [City]
}
type City {
name: String
}
`;
schema/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];
回答1:
By default, Apollo server turns off introspection and playground when running with NODE_ENV=production
. Since Heroku sets it by default when you use the node buildpack
your playground is disabled.
To circumvent the issue you need to pass to apollo-server 2 options:
const server = new ApolloServer({
introspection: true,
playground: true
});
来源:https://stackoverflow.com/questions/55775415/apollo-server-playground-not-working-on-heroku-working-locally