Skip to content

Commit

Permalink
Merge pull request #387 from walmartlabs/hls/restore-multi-errors
Browse files Browse the repository at this point in the history
Restore ability to add multiple errors at once
  • Loading branch information
hlship committed Nov 2, 2021
2 parents 147d340 + 43f85c2 commit 0ac3556
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 0 additions & 4 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ to boost performance.

### Incompatible Changes

* `com.walmartlabs.lacinia.resolve/resolve-as` and `with-error` did, in prior releases,
support an argument that could be either a single error map, or a sequence of error maps. This was rarely used, and
has been changed: only a single error map is supported.

* Previously, a resolver, including a default resolver, could return a map containing a value that was
a `ResolverResult`, and Lacinia would de-reference it; this is no longer allowed, resolvers must return
a value (or a value delivered by a `ResolverResult`). The value may be wrapped
Expand Down
20 changes: 13 additions & 7 deletions src/com/walmartlabs/lacinia/resolve.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@
"The analog of a field resolver function, this method is passed the instance, and the standard
context, field arguments, and container value, and returns a resolved value."))

(defn ^:private wrap-error-maps
[value behavior error]
(cond
;; Old behavior --- supported but not documented
(sequential? error)
(reduce #(wrap-value %1 behavior (assert-error-map %2)) value (reverse error))

(some? error)
(wrap-value value behavior (assert-error-map error))))

(defn with-error
"Wraps a value, modifying it to include an error map (or seq of error maps).
"Wraps a value, modifying it to include an error map.
The provided error map will be enhanced with a :location key,
identifying where field occurs within the query document, and a :path key,
Expand All @@ -62,9 +72,7 @@
and must be a string) will be added to an embedded :extensions map."
{:added "0.19.0"}
[value error]
(if error
(wrap-value value :error (assert-error-map error))
value))
(wrap-error-maps value :error error))

(defn with-context
"Wraps a value so that when nested fields (at any depth) are executed, the provided values will be in the context.
Expand Down Expand Up @@ -328,6 +336,4 @@
for an error and what call for a warning."
{:added "0.31.0"}
[value warning]
(if warning
(wrap-value value :warning (assert-error-map warning))
value))
(wrap-error-maps value :warning warning))
12 changes: 5 additions & 7 deletions test/com/walmartlabs/lacinia/resolver_errors_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
;; We could add a serializer that converts this to :BLUE, but we haven't.
Color/BLUE)
:multiple-errors (fn [_ _ _]
(reduce #(with-error %1 %2)
"Value"
(resolve-as "Value"
[{:message "1" :other-key 100}
{:message "2"}
{:message "3"}
{:message "4"}]))
{:message "2"}
{:message "3"}
{:message "4"}]))
:resolve-root (fn [_ _ _] {})})

(def default-schema
Expand Down Expand Up @@ -113,8 +112,7 @@
:message "4"
:path [:root :multiple_errors_field]}]
(->> (execute default-schema "{ root { multiple_errors_field }}")
:errors
(sort-by :message)))))
:errors))))

(deftest extensions-are-merged
(is (= {:data {:root {:with_extensions nil}}
Expand Down

0 comments on commit 0ac3556

Please sign in to comment.