-
-
Notifications
You must be signed in to change notification settings - Fork 676
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
Specifying query complexity in decorators does not take effect #417
Comments
Cannot reproduce - please checkout the examples: If you use NestJS or |
I'll try with the example. FYI, I didn't use NestJS or After trying with that example and still cannot get complexity working, I'll comment about the triage. Thx for the fast comment. |
Well... I'm seeing that when using I copy-and-pasted your example, only slightly changing:
With only 2 changes above, example code works correctly. It produces complexity point of 6 for graphql query query {
# Count of 1 will multiply 1 to child complexity.
recipes(count: 1) {
title # This will produce complexity of 1.
ratingsCount # This will produce complexity of 5, defined as, at FieldResolver.
}
# (1 + 5) * 1 = 6, total complexity.
} But after applying /// index.ts Line 11-13
const schema = await buildSchema({
resolvers: [RecipeResolver],
}); with /// index.ts Line 11-13
const schema = lexicographicSortSchema(await buildSchema({
resolvers: [RecipeResolver],
})); same graphql query will product complexity point of 3. Can this be confirmed? |
It can't affect query complexity nor the schema build itself.
I can confirm that as it strip off all additional properties of You can raise an issue in |
As a workaround, below code snippet will produce the almost same effect with const schema = buildSchemaSync({
resolvers,
});
// Manually sort lexicographically.
const sortedTypeMap: any = {};
const sortedQueryTypeFields: any = {};
const sortedMutationTypeFields: any = {};
for (const typeMapKey of Object.keys(schema.getTypeMap()).sort()) {
sortedTypeMap[typeMapKey] = schema.getTypeMap()[typeMapKey];
}
(schema as any)._typeMap = sortedTypeMap;
for (const queryTypeFieldKey of Object.keys(schema.getQueryType()!.getFields()).sort()) {
sortedQueryTypeFields[queryTypeFieldKey] = schema.getQueryType()!.getFields()[queryTypeFieldKey];
}
(schema as any)._queryType._fields = sortedQueryTypeFields;
for (const mutationTypeFieldKey of Object.keys(schema.getMutationType()!.getFields()).sort()) {
sortedMutationTypeFields[mutationTypeFieldKey] = schema.getMutationType()!.getFields()[mutationTypeFieldKey];
}
(schema as any)._mutationType._fields = sortedMutationTypeFields;
(schema as any)._implementations.Node.sort((a: any, b: any) => a.name < b.name ? -1 : 1); Anyway, I might as well raise an issue at graphql-query-complexity repo about using Btw, for |
It should happen only when you use compilation to ES5 using babel or sth. Can you reproduce the issue in a new example repository? |
Sorry for checking late. I'll try with |
Describe the bug
TypeGraphQL documentation describes how to apply query complexity analysis based on
graphql-query-complexity
package. When actually applying it by specifying complexity for fields, whether with a simple number or with a function that takes field arguments and child complexity, both do not take effect, resulting in incorrect query complexity.To Reproduce
I think any TypeGraphQL schema with non-default('default complexity' here means the
defaultComplexity
ingraphql-query-complexity
'ssimpleEstimator
) complexity as a number or function would reproduce this bug, but for to be sure, minimal reproducible code(I can think of) is at this Repo. Clone this repo and build withtsc -b ./src
, run withnode ./dist/app.js
will run the apollo server.Now querying
should return complexity of 8, since
id
field is 1,name
field is 5(bycomplexity: 5
at its@Field
decorator) andage
field is 1, and lastly,human
field by itself is 1. However, running this query will print outUsed complexity: 4
instead ofUsed complexity: 8
.Expected behavior
Expect query complexity field to be accurately computed, wherever decorator the complexity is defined at as long as its valid one mentioned in docs, whatever type(number or function) it is described in.
Enviorment (please complete the following information):
Additional context
While debugging into TypeGraphQL source code to find the cause, I think setting
complextiy
field at GraphQL type build time is not safe to use (at least) withgraphql-query-complexity
package. As I can understand, TypeGraphQL seems to build its own metadata with user-defined type definitions, then callgraphql-js
'sGraphQLSchema
constructor with that TypeGraphQL-built type information(metadata?). (Guessed fromschema-generator.ts
file.) Butgraphql-js
does not take complexity into its consideration, hence not setting the fieldcomplexity
to its finally built graphql type object.(Guessed from constructor ofGraphQLScalarType
(here),GraphQLObjectType
(here) atgraphql-js
's 'src/type/definition.js
file.) Butgraphql-query-complexity
tries to get complexity from already-built-by-graphql-js
-type (here), which does not have information about complexity TypeGraphQL has set, because it's been lost while building final graphql type objects bygraphql-js
. I don't know my guessing is correct or not, but if it is correct, by current method, complexity may not be set at all, resulting in all complexity falls back tosimpleEstimator
'sdefaultComplexity
.If I'm getting something wrong, please feel free to point them out!
The text was updated successfully, but these errors were encountered: