Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .changeset/good-spies-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
'@apollo/server': minor
---

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

Apollo Server uses graphql-js behind the scenes when executing graphql queries.
If you would like to use a custom executor, such as
[graphql-jit](https://github.com/zalando-incubator/graphql-jit/tree/main),
you can swap in a `customExecutor`. This can result in significant performance
gains when used in combination with `staticOnly` cache control hints, especially with larger response sizes.

Note that this cannot be used when using the `gateway` option.

```ts
import { compileQuery, isCompiledQuery } from 'graphql-jit';
import { lru } from 'tiny-lru';

const executor = (schema: GraphQLSchema, cacheSize = 2014, compilerOpts = {}) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a schema on GraphQLRequestContext too — can we just use that one instead of having to specify it both to this function and to the server? I guess perhaps we then have to include it in the cache key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting using schema from request instead of passing it into the executor?

I suppose that could work assuming its the same schema object.

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),
});
```
21 changes: 21 additions & 0 deletions docs/source/api/apollo-server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,27 @@ You can also manually call `stop()` in other contexts. Note that `stop()` is asy
</td>
</tr>


<tr>
<td>

###### `customExecutor`

`Object`

</td>
<td>

Apollo Server uses graphql-js behind the scenes when executing graphql queries.
If you would like to use a [custom executor](/apollo-server/performance/custom-executor), such as
[graphql-jit](https://github.com/zalando-incubator/graphql-jit/tree/main),
you can swap in a `customExecutor`.

Note that this cannot be used when using the `gateway` option.

</td>
</tr>

<tr>
<td colspan="2">

Expand Down
3 changes: 2 additions & 1 deletion docs/source/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"Caching": "/performance/caching",
"Cache Backends": "/performance/cache-backends",
"Response Cache Eviction": "/performance/response-cache-eviction",
"Automatic Persisted Queries": "/performance/apq"
"Automatic Persisted Queries": "/performance/apq",
"Using a Custom Executor": "/performance/custom-executor"
},
"Security": {
"Authentication and Authorization": "/security/authentication",
Expand Down
49 changes: 49 additions & 0 deletions docs/source/performance/custom-executor.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Using a Custom Executor
description: How to configure Apollo Server to use a custom executor
---

Apollo Server uses graphql-js behind the scenes when executing graphql queries.
If you would like to use a custom executor, such as
[graphql-jit](https://github.com/zalando-incubator/graphql-jit/tree/main),
you can swap in a `customExecutor`. This can result in significant performance
gains when used in combination with `staticOnly` cache control hints, especially with larger response sizes.

Note that this cannot be used when using the `gateway` option.

![graph showing how Apollo Server performance with increasing payload size with various setups](graphqlhit.png)

## Example using graphql-jit

```ts
import { compileQuery, isCompiledQuery } from 'graphql-jit';
import { lru } from 'tiny-lru';

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),
});
```
Binary file added docs/source/performance/graphqljit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading