我有一个node / express / Apollo应用程序,可为前端应用程序提供GraphQL服务。我在应用程序中还拥有REST API,可以为遗留应用程序提供服务。我想将REST API调用转发到GraphQL端点。例如:
来自
GET /api/roles
到
POST /graphql
{ *body* }
我尝试过这样:
// 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`);
});
// route / role.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);
});
它不起作用,并显示错误“ Cannot POST / graphql”。知道怎么做吗?