【发布时间】:2018-11-07 19:25:15
【问题描述】:
我想知道如何改进将 graphql 连接到 api 的响应时间。我决定使用 Redis。我不知道我该怎么做。
我已经建立了我的 graphql 服务器:
import express from 'express';
import {
graphqlExpress,
graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
import { schema } from './src/schema';
import cors from 'cors';
import redis from 'redis';
const PORT = 4000;
const server = express();
const client = redis.createClient();
client.on('error', function (err) {
console.log('error' + err)
});
server.use('*', cors({ origin: 'http://localhost:3000' }));
server.use('/graphql', bodyParser.json(), graphqlExpress({
schema, context: { client }
}));
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql'
}));
server.listen(PORT, () =>
console.log(`GraphQL Server is now running on http://localhost:${PORT}`)
);
我这里已经导入了redis。我的 graphql 服务器通过 cors 连接到首页,所以我可以从 api 渲染字符串。在解析器中,我通过 Node.js 连接到 api(如有必要,我将添加它。),我在这里有一些自定义解析器。来自 api 的响应时间(也影响页面上的渲染)太慢了——大约 10~15 秒。
【问题讨论】: