解决使用Apollo客户端和代理配置解析Express-GraphQL端点的问题

时间:2017-04-23 15:18:13

标签: node.js proxy graphql apollo

我正在尝试使用Apollo Client连接到Create-React-App中的Express-GraphQL端点,并使用自定义代理配置进行开发。我收到以下错误:enter image description here

Apollo客户端网络接口:

 import {ApolloProvider, ApolloClient, createNetworkInterface} from 'react-apollo';
    import './index.css';

    const client = new ApolloClient({
  networkInterface: createNetworkInterface({
  uri:"http://localhost:8080/graphql",
}),
  connectToDevTools: true
});

Express Server / GraphQL Server

const graphqlHTTP = require('express-graphql');
const app = express();

app.use('/graphql', graphqlHTTP(request =>{
   return {
   schema: schema,
   graphiql: true,
   rootValue: root
} }));
let server;
function runServer(dbUrl, host, port=3001) {
  return new Promise((resolve, reject) => {
    mongoose.Promise = global.Promise;
    mongoose.connect(dbUrl, err => {
      if (err) {
        return reject(err);
      }
      server = app.listen(port, host, () => {
        console.log(`Your app is listening on port ${port}`);
        resolve();
      })

代理 (使用http-proxy-middlewear

const express = require('express');
const proxy = require('http-proxy-middleware');
const app = express();
const runServer = require('./server').runServer;

const app = express();
    // Proxy everything through to Create React App
    app.use(proxy('http://localhost:3000/', {
        logLevel: 'warn', // Keep the logs clean
        ws: true, // Proxy websockets too
        router: {
            // Anything to /api goes to our backend
            'http://localhost:8080/graphql': 'http://localhost:3001/',
        }
    }));
       app.listen(8080);

顶级package.json又名原因 (3个package.json用于客户端,服务器和顶级)

"scripts": {
    "start": "node index.js",
    "heroku-postbuild": "cd client && npm install --only=dev && npm run build",
    "dev": "run-p dev:server dev:client start",
    "dev:client": "cd client && cross-env BROWSER=none npm start -- --color=always | xp http://localhost:3000/ http://localhost:8080/",
    "dev:server": "cd server && npm start",
    "install": "run-s install:server install:client",
    "install:server": "cd server && npm install",
    "install:client": "cd client && npm install"
  },

0 个答案:

没有答案