Skip to content

How to implement with Apollo server v3 #81

Open
@daniel-keller

Description

@daniel-keller

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';.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions