1

My app's front-end is Reactjs and backend is Node js. I used express server and graphql-express server. I deployed my app successfully. But it starts with data is loading and then nothing shows. But if I run locally npm start then the heroku app is start working.

This is the error I get it in browser

enter image description here

This is my server's package json file. If i run in terminal npm run dev. It opens my react js app.

    {
  "name": "backend",
  "version": "1.0.0",
  "description": "personish vol 2",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "server": "node server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },
  "author": "Alak",
  "license": "ISC",
  "dependencies": {
    "concurrently": "^5.0.2",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-graphql": "^0.9.0",
    "graphql": "^14.5.8",
    "pg": "^7.15.1",
    "pg-hstore": "^2.3.3",
    "sequelize": "^5.21.3"
  }
}

This is my react's package json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "engines": {
    "node": "13.3.0",
    "npm": "6.13.0"
  },
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "apollo-boost": "^0.4.7",
    "axios": "^0.19.0",
    "firebase": "^7.6.1",
    "graphql": "^14.5.8",
    "pg": "^7.15.1",
    "react": "^16.12.0",
    "react-apollo": "^3.1.3",
    "react-dom": "^16.12.0",
    "react-dropzone": "^10.2.1",
    "react-redux": "^7.1.3",
    "react-redux-firebase": "^3.0.5",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "redux": "^4.0.4",
    "redux-firestore": "^0.11.0",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

This is my express and graphql-express server.

const express = require("express");
const app = express();

const graphqlHTTP = require("express-graphql");
const schema = require("./schema");
const cors = require("cors"); 
const path = require("path");

app.use(cors());

app.use(
  "/graphql",
  graphqlHTTP({
    schema,
    pretty: true,
    graphiql: true
  })
);
app.use(express.static(path.join(__dirname, "build")));

app.get("/*", (req, res) => {
  res.sendFile(path.join(__dirname, "build", "index.html"));
});

const port = process.env.PORT || 8081;
app.listen(port, () =>
  console.log(`✅  Example app listening on port ${port}!`)
);

This is my react's app file. Where I use Apollo boost to connect with react app and node js app. I think the error comes from here, dont know how to fix it

import React from "react";
import { ApolloClient, HttpLink, InMemoryCache } from "apollo-boost";
import { ApolloProvider } from "react-apollo";
const client = new ApolloClient({
  link: new HttpLink({
    uri: "http://localhost:8081/graphql"
  }),
  cache: new InMemoryCache()
});

This is my heroku app

https://databaseapp2020.herokuapp.com/

Krisna
  • 2,854
  • 2
  • 24
  • 66
  • Your ‘ERR_CONNECTION_REFUSED’ shows that you are trying to connect to ‘localhost:8081’. – Ratul Sharker Jan 06 '20 at 14:18
  • I know the problem, but dont know the solution. should I use the public ip instead of local host? for example: https://192.168.0.0:8081/graphql – Krisna Jan 06 '20 at 14:38

1 Answers1

0

your heroku dyno/app would have a name, you would use that to hit your graphql from say a graphiql playground on your laptop, or your react-app... if your heroku app was actually named databaseapp2020 and running then you would use the following assuming your cors was setup properly in the dyno environment variables

https://databaseapp2020.herokuapp.com/graphql

Greg Belyea
  • 858
  • 1
  • 5
  • 15
  • i'm not sure it's a code problem, do the heroku logs tell you that your app is running ok? if so then, you should replace uri: "http://localhost:8081/graphql" with uri: "https://databaseapp2020.herokuapp.com/graphql"... but if you can't hit the link i posted above then your app is not running, and if it won't allow you access with a cors error then you need to grant proper privileges – Greg Belyea Jan 07 '20 at 08:27
  • when the app is running and cors is setup, https://databaseapp2020.herokuapp.com/graphql would take you to the graphiql playground screen – Greg Belyea Jan 07 '20 at 08:32