【发布时间】:2018-12-20 20:17:32
【问题描述】:
最初,我尝试使用无服务器 Lambda 函数来处理我的 API 的架构拼接,但我开始转向 Elastic Beanstalk 服务器,以避免需要获取每个请求的初始架构。
即便如此,对我的主 API 服务器的请求从其中一个子 API 服务器获取结果的时间可能是我的子服务器的十倍。我不确定是什么让请求如此长,但似乎有什么东西阻止了请求的快速解决。
这是我的父 API 代码:
import * as express from 'express';
import { introspectSchema, makeRemoteExecutableSchema, mergeSchemas } from 'graphql-tools';
import { ApolloServer } from 'apollo-server-express';
import { HttpLink } from 'apollo-link-http';
import fetch from 'node-fetch';
async function run () {
const createRemoteSchema = async (uri: string) => {
const link = new HttpLink({ uri, fetch });
const schema = await introspectSchema(link);
return makeRemoteExecutableSchema({
schema,
link
});
};
const remoteSchema = await createRemoteSchema(process.env.REMOTE_URL);
const schema = mergeSchemas({
schemas: [remoteSchema]
});
const app = express();
const server = new ApolloServer({
schema,
tracing: true,
cacheControl: true,
engine: false
});
server.applyMiddleware({ app });
app.listen({ port: 3006 });
};
run();
知道为什么这么慢吗?
更新:
对于任何试图在本地环境中拼接模式的人,我通过直接获取 127.0.0.1 而不是通过 localhost 获得了显着的速度提升。
http://localhost:3002/graphql > http://127.0.0.1:3002/graphql
这对我来说根本不是阿波罗的问题。
【问题讨论】:
-
在我的测试中,最大的时间消耗是 makeRemoteExecutableSchema,在我的 8 核机器上使用本地模式文件需要 400-600 毫秒。
标签: graphql apollo graphql-js apollo-server