Skip to content

Commit f9e30bf

Browse files
committed
Added example usage in README.
1 parent 944aa88 commit f9e30bf

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ type ComplexityEstimatorArgs = {
104104
type ComplexityEstimator = (options: ComplexityEstimatorArgs) => number | void;
105105
```
106106

107+
## Calculate query complexity
108+
```javascript
109+
import { calculateComplexity, simpleEstimator } from "graphql-query-complexity/dist/QueryComplexity";
110+
import { parse } from 'graphql';
111+
112+
// In a resolver the schema can be retrieved from the info argument.
113+
const schema = undefined;
114+
const query = parse(`
115+
query {
116+
some_value
117+
some_list(count: 10) {
118+
some_child_value
119+
}
120+
}
121+
`);
122+
123+
const complexity = calculateComplexity({
124+
estimators: [
125+
simpleEstimator({defaultComplexity: 1})
126+
],
127+
schema,
128+
query
129+
});
130+
131+
console.log(complexity); // Output: 3
132+
```
133+
107134
## Usage with express-graphql
108135

109136
To use the query complexity analysis validation rule with express-graphql, use something like the

0 commit comments

Comments
 (0)