-
Notifications
You must be signed in to change notification settings - Fork 2k
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
andrewmcgivery
wants to merge
8
commits into
apollographql:main
Choose a base branch
from
andrewmcgivery:feature/customExecutor2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9824def
Added ability to use a custom executor such as graphql-jit
andrewmcgivery 9dd7ce8
Added docs
andrewmcgivery 966da88
Add custom executor doc to sidebar
andrewmcgivery 7eb267f
Added changeset
andrewmcgivery 2ce1aef
Fix eslint issues
andrewmcgivery c3a32d0
Remove GraphQLExecutionResult in favor of type from graphql package
andrewmcgivery 24e9946
Remove references to staticOnly since that PR died on impact
andrewmcgivery c6c3145
Update example to get schema from request context instead of passing in
andrewmcgivery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}) => { | ||
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), | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.