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

How to implement with Apollo server v3 #81

Open
daniel-keller opened this issue Oct 4, 2022 · 1 comment
Open

How to implement with Apollo server v3 #81

daniel-keller opened this issue Oct 4, 2022 · 1 comment

Comments

@daniel-keller
Copy link

daniel-keller commented Oct 4, 2022

I spent several hours finding the best way to implement graphql-query-complexity with Apollo server v3 (server and serverless)
and I just wanted to post the answer in case anyone else needs it.

The trouble I ran into is that apollo server doesn't give its validationRules access to the request object so calculating query complexity based on request parameters is not possible. In apollo validationRules are not guaranteed to run on every request.

The solution is to use the plugin api's didResolveOperation hook to manually trigger a complexity calculation further along the request pipeline as explained here.

If you are using express (apollo-server-express or apollo-server-lambda or apollo-server-cloud-functions) you could probably do this as express middleware as well.

Feel free to close this Issue.

EDIT: You will need to modify the code example to work with the latest version of graphql-query-complexity (fieldConfigEstimator is deprecated). If you are using Apollo and building your schema using gql() you may also need to convert your schema into a GraphQLSchema. Graphql includes a utility to do this import { buildASTSchema } from 'graphql';.

@shadowbrush
Copy link

Thank you, @daniel-keller, that was helpful a few years later. To add to this: use fieldExtensionsEstimator as replacement for fieldConfigEstimator. Works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants