Я настраиваю сервер apollo graphql в nodejs. Ниже приведен исходный код. Я могу запустить сервер, и он начнет прослушивать порт 6000. Но я получил This site can’t be reached, когда открыл URL-адрес (http://localhost:6000/graphiql) в браузере. Интересно, что не так с моим кодом.
const express = require('express');
const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
// Some fake data
const books = [
{
title: "Harry Potter and the Sorcerer's stone",
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
// The GraphQL schema in string form
const typeDefs = `
type Query { books: [Book] }
type Book { title: String, author: String }
`;
// The resolvers
const resolvers = {
Query: { books: () => books },
};
// Put together a schema
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
// Initialize the app
const app = express();
// The GraphQL endpoint
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
// GraphiQL, a visual editor for queries
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
// Start the server
app.listen(6000, () => {
console.info('Go to http://localhost:6000/graphiql to run queries!');
});


Это проблема с портом, порт 6000 является одним из портов, который не считается безопасным для большинства браузеров, хотя вы можете свернуть его на терминале. Например, это должно работать:
curl -X POST http://localhost:6000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ books { title } }"}'
Но запускать его в браузере нельзя, так как порт ограничен. Вы можете увидеть список запрещенных портов на Хром и Fire Fox, другие браузеры также должны следовать этим правилам.
Если вы измените свой порт на 4000 (например, или любой другой неограниченный порт), все должно работать нормально.