Added ability to use a custom executor such as graphql-jit
Added ability to use a custom executor such as graphql-jit. This is a notable performance improvement.
Why?
These changes are to optimize general execution time and especially execution time for larger payloads.
Here is a comparison of the response time with a increasing response size. You can see that compared to the Apollo baseline, using Apollo + graphql-jit + staticOnly cache control mode results in a more than twice as fast response time when getting to larger response sizes. Even with smaller response sizes, there is a notable difference in response time.
How?
Version 4 of Apollo Server removed the executor argument. This was due to it being a "legacy" option that used to be used for the Gateway. However, in the process of removing it, it also removed the option to drop in something like graphql-JIT.
GraphQL JIT is a drop in replacement for graphql-js when executing. It is pretty significantly faster when looking at benchmarks.
This change introduces an argument called customExecutor which allows it to be dropped in. E.g.:
const executor = (schema: GraphQLSchema, cacheSize = 2014, compilerOpts = {}) => {
const cache = lru(cacheSize);
return async ({ contextValue, document, operationName, request, queryHash }) => {
const prefix = operationName || 'NotParametrized';
const cacheKey = `${prefix}-${queryHash}`;
let compiledQuery = cache.get(cacheKey);
if (!compiledQuery) {
const compilationResult = compileQuery(schema, document, operationName || undefined, compilerOpts);
if (isCompiledQuery(compilationResult)) {
compiledQuery = compilationResult;
cache.set(cacheKey, compiledQuery);
} else {
// ...is ExecutionResult
return compilationResult;
}
}
return compiledQuery.query(undefined, contextValue, request.variables || {});
};
};
const schema = buildSubgraphSchema([{ typeDefs, resolvers }]);
const server = new ApolloServer<BaseContext>({
schema,
customExecutor: executor(schema),
});
🚫 Docs Preview Denied
You must have approval from an Apollo team member to request a docs preview. If you are a team member, please comment !docs preview.
File Changes
0 new, 1 changed, 1 removed
* (developer-tools)/apollo-server/(latest)/api/apollo-server.mdx
- (developer-tools)/apollo-server/(latest)/performance/custom-executor.mdx
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.