Skip to content

Commit

Permalink
feat(complexity): add support only for fieldExtensionsEstimator
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Oct 6, 2019
1 parent 65ae057 commit 1cd84f7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Expand Up @@ -5,8 +5,7 @@
### Features
- **Breaking Change**: emit in schema only types actually used by provided resolvers classes (#415)
- **Breaking Change**: update `graphql-js` peer dependency to `^14.5.4`
- update `graphql-query-complexity` dependency to `^0.4.0`

- **Breaking Change**: update `graphql-query-complexity` dependency to `^0.4.0` and drop support for `fieldConfigEstimator` (use `fieldExtensionsEstimator` instead)
### Fixes
- refactor union types function syntax handling to prevent possible errors with circular refs

Expand All @@ -16,7 +15,6 @@
- update deps to newest minor versions (`tslib`, `semver`, `graphql-query-complexity` and `glob`)
- support nested array types (`@Field(type => [[Int]])`) (#393)
- deprecate the direct array syntax for union types

### Fixes
- fix errors on circular refs in union types (#364) by adding the function syntax (`() => TClassTypes`)

Expand Down
4 changes: 2 additions & 2 deletions docs/complexity.md
Expand Up @@ -63,8 +63,8 @@ async function bootstrap() {
// numeric value that is being returned by an estimator is used as the field complexity.
// If no estimator returns a value, an exception is raised.
estimators: [
// Using fieldConfigEstimator is mandatory to make it work with type-graphql.
fieldConfigEstimator(),
// Using fieldExtensionsEstimator is mandatory to make it work with type-graphql.
fieldExtensionsEstimator(),
// Add more estimators here...
// This will assign each field a complexity of 1
// if no other estimator returned a value.
Expand Down
6 changes: 3 additions & 3 deletions examples/query-complexity/index.ts
@@ -1,6 +1,6 @@
import "reflect-metadata";
import { ApolloServer } from "apollo-server";
import { getComplexity, simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
import { getComplexity, simpleEstimator, fieldExtensionsEstimator } from "graphql-query-complexity";
import { separateOperations } from "graphql";
import { buildSchema } from "../../src";

Expand Down Expand Up @@ -40,8 +40,8 @@ async function bootstrap() {
// numeric value that is being returned by an estimator is used as the field complexity.
// If no estimator returns a value, an exception is raised.
estimators: [
// Using fieldConfigEstimator is mandatory to make it work with type-graphql.
fieldConfigEstimator(),
// Using fieldExtensionsEstimator is mandatory to make it work with type-graphql.
fieldExtensionsEstimator(),
// Add more estimators here...
// This will assign each field a complexity of 1
// if no other estimator returned a value.
Expand Down
8 changes: 6 additions & 2 deletions src/schema/schema-generator.ts
Expand Up @@ -295,13 +295,15 @@ export abstract class SchemaGenerator {
);
fieldsMap[field.schemaName] = {
type: this.getGraphQLOutputType(field.name, field.getType(), field.typeOptions),
complexity: field.complexity,
args: this.generateHandlerArgs(field.params!),
resolve: fieldResolverMetadata
? createAdvancedFieldResolver(fieldResolverMetadata)
: createSimpleFieldResolver(field),
description: field.description,
deprecationReason: field.deprecationReason,
extensions: {
complexity: field.complexity,
},
};
return fieldsMap;
},
Expand Down Expand Up @@ -457,7 +459,9 @@ export abstract class SchemaGenerator {
resolve: createHandlerResolver(handler),
description: handler.description,
deprecationReason: handler.deprecationReason,
complexity: handler.complexity,
extensions: {
complexity: handler.complexity,
},
};
return fields;
}, {});
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/resolvers.ts
Expand Up @@ -19,7 +19,7 @@ import {
} from "graphql";
import * as path from "path";
import { plainToClass } from "class-transformer";
import { fieldConfigEstimator, simpleEstimator } from "graphql-query-complexity";
import { fieldExtensionsEstimator, simpleEstimator } from "graphql-query-complexity";
import ComplexityVisitor from "graphql-query-complexity/dist/QueryComplexity";

import {
Expand Down Expand Up @@ -1112,7 +1112,7 @@ describe("Resolvers", () => {
const context = new ValidationContext(schema, ast, typeInfo);
const visitor = new ComplexityVisitor(context, {
maximumComplexity,
estimators: [fieldConfigEstimator(), simpleEstimator({ defaultComplexity: 1 })],
estimators: [fieldExtensionsEstimator(), simpleEstimator({ defaultComplexity: 1 })],
});
visit(ast, visitWithTypeInfo(typeInfo, visitor));
return context;
Expand Down Expand Up @@ -1897,7 +1897,7 @@ describe("Resolvers", () => {
const context = new ValidationContext(schema, ast, typeInfo);
const visitor = new ComplexityVisitor(context, {
maximumComplexity: 2,
estimators: [fieldConfigEstimator(), simpleEstimator({ defaultComplexity: 1 })],
estimators: [fieldExtensionsEstimator(), simpleEstimator({ defaultComplexity: 1 })],
});
visit(ast, visitWithTypeInfo(typeInfo, visitor));
expect(context.getErrors().length).toEqual(1);
Expand Down

0 comments on commit 1cd84f7

Please sign in to comment.