Express REST API路由器转发到Apollo GraphQL endpoinr

Express REST API路由器转发到Apollo GraphQL endpoinr,express,graphql,apollo-server,Express,Graphql,Apollo Server,我有一个node/express/Apollo应用程序为前端应用程序提供GraphQL服务。我还在应用程序中使用RESTAPI端点,为遗留应用程序提供服务。我想将RESTAPI调用转发到GraphQL端点。例如: 从 到 我试着这样做: //app.js import express from 'express'; import { ApolloServer } from 'apollo-server-express'; import routes from './routes'; const

我有一个node/express/Apollo应用程序为前端应用程序提供GraphQL服务。我还在应用程序中使用RESTAPI端点,为遗留应用程序提供服务。我想将RESTAPI调用转发到GraphQL端点。例如:

我试着这样做:

//app.js

import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import routes from './routes';

const port = process.env.PORT || 8088;
const app = express();

app.use('/api/roles', routes.role);

const server = new ApolloServer({
     ......  
  },
});

server.applyMiddleware({ app, path: '/graphql' });

app.listen({ port: port }, () => {
  console.log(`Apollo Server on http://localhost:${port}/graphql`);
});
//路由/角色.js

import { Router } from 'express';

const router = Router();

router.get('/', (req, res, next) => {
  req.url = '/graphql';
  req.originalUrl = '/graphql';
  req.method = 'POST';
  req.body = `
      {
          findRoles {
              data {
                  roleId
                  name
              }
          }
      }`;

  return router.handle(req, res, next);
});
它不工作,并给出错误“cannotpost/graphql”。知道怎么做吗

import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import routes from './routes';

const port = process.env.PORT || 8088;
const app = express();

app.use('/api/roles', routes.role);

const server = new ApolloServer({
     ......  
  },
});

server.applyMiddleware({ app, path: '/graphql' });

app.listen({ port: port }, () => {
  console.log(`Apollo Server on http://localhost:${port}/graphql`);
});
import { Router } from 'express';

const router = Router();

router.get('/', (req, res, next) => {
  req.url = '/graphql';
  req.originalUrl = '/graphql';
  req.method = 'POST';
  req.body = `
      {
          findRoles {
              data {
                  roleId
                  name
              }
          }
      }`;

  return router.handle(req, res, next);
});