Skip to content

Commit

Permalink
쿼리복잡도 초과시 errors 대신 extensions.warnings 로 리졸빙되도록 수정 (#15)
Browse files Browse the repository at this point in the history
* complexity errors to extensions.warnings

* some fix

* fix test

* remove prn

* apply review

* indent
  • Loading branch information
1e16miin authored Aug 23, 2024
1 parent c360d04 commit e297fe6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
16 changes: 6 additions & 10 deletions src/com/walmartlabs/lacinia.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@

(seq validation-errors)
(resolve/resolve-as {:errors validation-errors})

:let [complexity-error (when (:max-complexity options)
(complexity-analysis/complexity-analysis prepared options))]

(some? complexity-error)
(resolve/resolve-as {:errors complexity-error})

:else
(executor/execute-query (assoc context constants/parsed-query-key prepared
::tracing/validation {:start-offset start-offset
:duration (tracing/duration start-nanos)})))))
:else (let [complexity-warning (when (:max-complexity options)
(complexity-analysis/complexity-analysis prepared options))]
(executor/execute-query (assoc context constants/parsed-query-key prepared
:complexity-warning complexity-warning
::tracing/validation {:start-offset start-offset
:duration (tracing/duration start-nanos)}))))))

(defn execute-parsed-query
"Prepares a query, by applying query variables to it, resulting in a prepared
Expand Down
7 changes: 5 additions & 2 deletions src/com/walmartlabs/lacinia/executor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,14 @@
(let [parsed-query (get context constants/parsed-query-key)
{:keys [selections operation-type ::tracing/timing-start]} parsed-query
schema (get parsed-query constants/schema-key)
^Executor executor (::schema/executor schema)]
^Executor executor (::schema/executor schema)
complexity-warning (:complexity-warning context)]
(binding [resolve/*callback-executor* executor]
(let [enabled-selections (remove :disabled? selections)
*errors (atom [])
*warnings (atom [])
*warnings (if complexity-warning
(atom [complexity-warning])
(atom []))
*extensions (atom {})
*resolver-tracing (when (::tracing/enabled? context)
(atom []))
Expand Down
9 changes: 5 additions & 4 deletions test/com/walmartlabs/lacinia/complexity_analysis_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@

(defn ^:private resolve-node
[_ _ _]
{:edges []
:pageInfo {}})
nil)

(def ^:private schema
(utils/compile-schema "complexity-analysis-error.edn"
Expand All @@ -58,7 +57,8 @@
(deftest over-complexity-analysis
(testing "It is possible to calculate the complexity of a query in the Relay connection spec
by taking into account both named fragments and inline fragments."
(is (= {:errors {:message "Over max complexity! Current number of resources to be queried: 27"}}
(is (= {:data {:node nil}
:extensions {:warnings [{:message "Over max complexity! Current number of resources to be queried: 27"}]}}
(q "query ProductDetail($productId: ID){
node(id: $productId) {
... on Product {
Expand Down Expand Up @@ -101,7 +101,8 @@
}
}" {:productId "id"}))))
(testing "If no arguments are passed in the query, the calculation uses the default value defined in the schema."
(is (= {:errors {:message "Over max complexity! Current number of resources to be queried: 22"}}
(is (= {:data {:node nil}
:extensions {:warnings [{:message "Over max complexity! Current number of resources to be queried: 22"}]}}
(q "query ProductDetail($productId: ID){
node(id: $productId) {
... on Product {
Expand Down

0 comments on commit e297fe6

Please sign in to comment.