apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Added ability to use a custom executor such as graphql-jit

Open andrewmcgivery opened this issue 1 year ago • 2 comments

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.

Screenshot 2024-11-04 at 2 20 19 PM

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),
});

andrewmcgivery avatar Nov 05 '24 21:11 andrewmcgivery

🚫 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

svc-apollo-docs avatar Nov 05 '24 21:11 svc-apollo-docs

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.

codesandbox-ci[bot] avatar Nov 05 '24 21:11 codesandbox-ci[bot]