Repository: mcollina/fastify-gql
Pull request #239 (2020-08-06 23:09:41)

fix query depth calculation
- Fixed query depth calculation (length of flat fields list (not only depth) and fields order were matter in calculation)
- Added current query depth to error message
- Fixed tests


Patch:
diff --git a/lib/queryDepth.js b/lib/queryDepth.js
index 279a5c7..4dd7cc8 100644
--- a/lib/queryDepth.js
+++ b/lib/queryDepth.js
@@ -24,7 +24,7 @@ function queryDepth (definitions, queryDepthLimit) {
     for (const query of Object.keys(queryDepth)) {
       const totalDepth = queryDepth[query]
       if (totalDepth > queryDepthLimit) {
-        const queryDepthError = new Error(`${query} query exceeds the query depth limit of ${queryDepthLimit}`)
+        const queryDepthError = new Error(`${query} query depth (${totalDepth}) exceeds the query depth limit of ${queryDepthLimit}`)
         errors.push(queryDepthError)
       }
     }
@@ -33,13 +33,10 @@ function queryDepth (definitions, queryDepthLimit) {
   return errors
 }
 function determineDepth (node, current = 0) {
-  let result = current
   if (node.selectionSet) {
-    for (const selection of node.selectionSet.selections) {
-      result = determineDepth(selection, current++)
-    }
+    return Math.max(...node.selectionSet.selections.map((selection) => determineDepth(selection, current + 1)))
   }
-  return result
+  return current
 }
 function getQueriesAndMutations (definitions) {
   return definitions.reduce((map, definition) => {
diff --git a/package.json b/package.json
index 55c4ce7..59bc2a4 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,7 @@
     "@types/ws": "^7.2.6",
     "@typescript-eslint/eslint-plugin": "^3.6.1",
     "@typescript-eslint/parser": "^3.6.1",
-    "autocannon": "^5.0.0",
+    "autocannon": "^6.0.0",
     "fastify": "^3.0.2",
     "graphql-tools": "^6.0.14",
     "pre-commit": "^1.2.2",


Test patch:
diff --git a/test/query-depth.js b/test/query-depth.js
index 5337209..151bd08 100644
--- a/test/query-depth.js
+++ b/test/query-depth.js
@@ -144,7 +144,7 @@ test('queryDepth - test total depth is within queryDepth parameter', async (t) =
   app.register(GQL, {
     schema,
     resolvers,
-    queryDepth: 5
+    queryDepth: 6
   })
 
   // needed so that graphql is defined
@@ -162,14 +162,14 @@ test('queryDepth - test total depth is over queryDepth parameter', async (t) =>
   app.register(GQL, {
     schema,
     resolvers,
-    queryDepth: 3
+    queryDepth: 5
   })
 
   // needed so that graphql is defined
   await app.ready()
 
   const err = new BadRequest()
-  const queryDepthError = new Error('unnamedQuery query exceeds the query depth limit of 3')
+  const queryDepthError = new Error('unnamedQuery query depth (6) exceeds the query depth limit of 5')
   err.errors = [queryDepthError]
 
   try {
@@ -303,6 +303,7 @@ test('queryDepth - ensure query depth is correctly calculated', async (t) => {
           {
             id: 1,
             name: 'Strawberry',
+            description: 'Lorem ipsum',
             seasons: [
               { name: 'Spring' },
               { name: 'Summer' }
@@ -356,6 +357,7 @@ test('queryDepth - ensure query depth is correctly calculated', async (t) => {
     flavors {
       id
       name
+      description
       nutrition {
         calories,
         description {
@@ -394,6 +396,7 @@ test('queryDepth - ensure query depth is correctly calculated', async (t) => {
         {
           id: '1',
           name: 'Strawberry',
+          description: 'Lorem ipsum',
           nutrition: {
             calories: 123,
             description: {