Query complexity in a federated API

Hello there,

We’re writing GraphQL API for every new service moving forward and have chosen to go with federated architecture. It’s working amazingly well and now we’re moving to federation 2.0 for the public version of our gateway. (Before it was internal only where we were still learning stuff)

Now, I’d like to limit query complexity and have complexity calculation done and request be rejected if it costs more than some amount.

How do I go about doing that? Do the individual subgraph reject queries (that doesn’t seem good), does the gateway know which costs what? And how would it? Is there a special directive that we can use to indicate cost and gateway would know how much?

How are you guys doing it? My subgraphs are in golang and gateway is the federation 2 version with nodejs.

Thanks in advance.

I was hoping to be able to just define a directive with cost to each field and default to 0, and gateway would be able to calculate query complexity from there

Hello,

We have implemented exactly such a complexity evaluation mechanism in our gateway. We have implemented an apollo plugin that looks at every request and calculates a complexity score. We have used the graphql-query-complexity library for the actual complexity calculation.

Hope that helps.

Etienne

Hi,
Thanks for the reply, I tried the same, however on my query complexity calculation, I don’t have see my custom directives on my schema.

I have a @complexity directive on my subgraphs, and now when it’s time to calculate, getComplexity doesn’t see @complexity, I tried to log schema.getDirectives() and I only see @include, @skip, @deprecated and @specifiedBy.