Package: graphql-complexity
graphql-complexityPython library to compute the complexity of a GraphQL operationUsageThe library uses the query complexity algorithm to compute the complexity of a GraphQL operation. The algorithm is
based on the number of fields requested in the operation and the depth of the query.fromgraphql_complexityimport(get_complexity,SimpleEstimator)query="""query SomeQuery {user {idname}}"""complexity=get_complexity(query,estimator=SimpleEstimator(complexity=1,multiplier=1))ifcomplexity>10:raiseException("Query is too complex")EstimatorsIn order to get the complexity of a query, an estimator needs to be defined. The main responsibility of
the estimator is to give each node an integer value representing its complexity and (optionally) a
multiplier that reflects the complexity in relation to the depth of the query.There are two built-in estimators, plus the capability to create any new estimator by
implementing theComplexityEstimatorinterface.Estimate fields complexity based on constants for complexity and multiplierThis estimator assigns aconstantcomplexity value to each field and multiplies
it by anotherconstantwhich is propagated along the depth of the query.fromgraphql_complexityimportSimpleEstimatorestimator=SimpleEstimator(complexity=2,multiplier=1)Given the following query:query {
    user {
        name
        email
    }
}As the complexity and multiplier are constant, the complexity of the fields will be:FieldComplexityuser1name2 * (2 * 1)email2 * (2 * 1)And the total complexity will be6.Define fields complexity using schema directivesAssigns a complexity value to each field and multiplies it by the depth of the query.
It also supports the@complexitydirective to assign a custom complexity value to a field.This approach requires to provide the schema to the estimator.fromgraphql_complexityimportDirectivesEstimatorschema="""directive @complexity(value: Int!) on FIELD_DEFINITIONtype Query {oneField: String @complexity(value: 5)otherField: String @complexity(value: 1)withoutDirective: String}"""estimator=DirectivesEstimator(schema)Given the schema from above and the following query:query {
    oneField
    otherField
    withoutDirective
}The complexity of the fields will be:FieldComplexityCommentoneField5otherField1withoutDirective1The default complexity for fields without directive is1, this can be modified by parameters.And the total complexity will be7.Create a custom estimatorThis option allows to define a custom estimator to compute the complexity of a field using theComplexityEstimatorinterface. For example:fromgraphql_complexityimportComplexityEstimatorclassCustomEstimator(ComplexityEstimator):defget_field_complexity(self,node,key,parent,path,ancestors)->int:ifnode.name.value=="specificField":return100return1defget_field_multiplier(self,node,key,parent,path,ancestors)->int:return1Supported libraries (based on GraphQL-core)The library is compatible with the following GraphQL libraries:StrawberryThe library is compatible withstrawberry-graphql.
To use the library with strawberry-graphql, you need to install the library with thestrawberry-graphqlextra.poetryinstall--extrasstrawberry-graphqlTo use the library withstrawberry-graphql, you need to use thebuild_complexity_extensionmethod to build
the complexity extension and add it to the schema. This method receives an estimator and returns a complexity
extension that can be added to the schema.importstrawberryfromgraphql_complexity.extensions.strawberry_graphqlimportbuild_complexity_extension@strawberry.typeclassQuery:@strawberry.field()defhello_world(self)->str:return"Hello world!"extension=build_complexity_extension()schema=strawberry.Schema(query=Query,extensions=[extension])schema.execute_sync("query { helloWorld }")Thebuild_complexity_extensionmethod accepts an estimator as optional argument giving the possibility to use one
of the built-in estimators or a custom estimator.CreditsEstimators idea was heavily inspired bygraphql-query-complexity.