Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for empty result set with selectOne #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/fluree/server/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
(def QueryResponse
(m/schema [:orn
[:select [:sequential [:or coll? map?]]]
[:select-one [:or coll? map?]]]))
[:select-one [:or coll? map? nil? [:enum "null"]]]]))

(def HistoryQuery
(m/schema (fqh/history-query-schema [[:from LedgerAlias]])
Expand Down Expand Up @@ -248,6 +248,15 @@
(handler req*))))
(handler req)))))

(def wrap-no-result
"Handle a nil response from a selectOne query with no results."
(fn [handler]
(fn [req]
(let [{:keys [status body] :as result} (handler req)]
(if (and (= 200 status) (nil? body))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like something that should be handled by muuntaja instead of rolling our own conversion for this specific case. Is there no way to accomplish this using that library since we're already using it to convert between formats? I can see this causing issues if we ever decide to respond with edn as well as json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it an honest try with muuntaja but wasn't willing to devote more than 2 hours to it. Once I couldn't get it working in that window I went back to this. I figure that if we ever do support edn there will need to be a lot of updates to the middleware anyways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a problem with the QureryResponse schema? What happens when you just account for the possibility that :select-one query responses can also be nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it converts the :body key to the empty string "", which isn't valid json.

(assoc result :body "null")
result)))))

(defn wrap-set-fuel-header
[handler]
(fn [req]
Expand Down Expand Up @@ -361,6 +370,7 @@
[200 coercion/coerce-exceptions-middleware]
[300 coercion/coerce-response-middleware]
[400 coercion/coerce-request-middleware]
[450 wrap-no-result]
[500 wrap-policy-metadata]
[600 (wrap-closed-mode root-identities closed-mode)]
[1000 exception-middleware]])))
Expand Down
42 changes: 27 additions & 15 deletions test/fluree/server/integration/basic_query_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,33 @@
"type" "schema:Test"
"ex:name" "query-test"}]})
:headers json-headers}
txn-res (api-post :transact txn-req)
_ (assert (= 200 (:status txn-res)))
query-req {:body
(json/write-value-as-string
{"@context" test-system/default-context
"from" ledger-name
"selectOne" '{?t ["*"]}
"where" '{"id" ?t, "type" "schema:Test"}})
:headers json-headers}
query-res (api-post :query query-req)]
(is (= 200 (:status query-res)))
(is (= {"id" "ex:query-test"
"type" "schema:Test"
"ex:name" "query-test"}
(-> query-res :body json/read-value)))))
txn-res (api-post :transact txn-req)]
(assert (= 200 (:status txn-res)))
(testing "with result"
(let [query-req {:body
(json/write-value-as-string
{"@context" test-system/default-context
"from" ledger-name
"selectOne" '{?t ["*"]}
"where" '{"id" ?t, "type" "schema:Test"}})
:headers json-headers}
query-res (api-post :query query-req)]
(is (= 200 (:status query-res)))
(is (= {"id" "ex:query-test"
"type" "schema:Test"
"ex:name" "query-test"}
(-> query-res :body json/read-value)))))
(testing "without result"
(let [query-req {:body
(json/write-value-as-string
{"@context" test-system/default-context
"from" ledger-name
"selectOne" '{?t ["*"]}
"where" '{"id" ?t, "type" "schema:Foo"}})
:headers json-headers}
query-res (api-post :query query-req)]
(is (= 200 (:status query-res)))
(is (nil? (-> query-res :body json/read-value)))))))

(testing "bind query works"
(let [ledger-name (create-rand-ledger "query-endpoint-bind-test")
Expand Down
Loading