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 all 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
45 changes: 45 additions & 0 deletions .changeset/good-spies-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
'@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 This can result in improved performance.

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 = (cacheSize = 2014, compilerOpts = {}) => {
const cache = lru(cacheSize);
return async ({ contextValue, document, operationName, request, queryHash, schema }) => {
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(),
});
```
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
46 changes: 46 additions & 0 deletions docs/source/performance/custom-executor.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
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 improved performance.

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

## Example using graphql-jit

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

const executor = (cacheSize = 2014, compilerOpts = {}) => {
const cache = lru(cacheSize);
return async ({ contextValue, document, operationName, request, queryHash, schema }) => {
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(),
});
```
Loading