Skip to content

Commit

Permalink
gosura.csk 로 대체
Browse files Browse the repository at this point in the history
  • Loading branch information
neverlish committed Jan 19, 2023
1 parent d9f140d commit f634cb3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 72 deletions.
92 changes: 46 additions & 46 deletions src/gosura/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
gosura 에서 생성하는 resolver-fn 이 부적합 할 때는 따로 작성하는 것이 더 적절할 수 있습니다.
주의) resolver-config edn 에 사용하는 ns 는 (ns 선언만 있는 빈 파일을 만들고) 그 네임스페이스를 사용하세요."
(:require [camel-snake-kebab.core :as csk]
[clojure.set :as s]
(:require [clojure.set :as s]
[clojure.tools.logging :as log]
[com.walmartlabs.lacinia.resolve :refer [resolve-as]]
[com.walmartlabs.lacinia.schema :refer [tag-with-type]]
[failjure.core :as f]
[gosura.auth :as auth]
[gosura.csk :as csk]
[gosura.helpers.relay :as relay]
[gosura.helpers.resolver :as r]
[gosura.helpers.resolver2 :as r2]
Expand Down Expand Up @@ -74,7 +74,7 @@
#"resolve-delete-one" r/resolve-delete-one
#"resolve-update-multi" r/resolve-update-multi
#"resolve-one" r/resolve-one

;; resolver2
#"connection-by-(.*)" r2/connection-by
#"one-by-(.*)" r2/one-by)
Expand Down Expand Up @@ -123,51 +123,51 @@
(if (= :resolve-node resolver)
(intern target-ns (symbol resolver) (defmethod relay/node-resolver node-type [this ctx _args _parent]
(f/attempt-all
[{:keys [auth]} settings
auth-filter-opts (auth/->auth-result auth ctx)
config-filter-opts (auth/config-filter-opts filters ctx)
resolver-filter-opts (auth/config-filter-opts (:filters params) ctx)
filter-options (merge {:id (or (:db-id this)
(:id this))}
auth-filter-opts
config-filter-opts
resolver-filter-opts)
rows (table-fetcher (get ctx db-key) filter-options {})
_ (when (empty? rows)
(f/fail "NotExistData"))]
(-> (first rows)
(relay/build-node node-type post-process-row)
transform-keys->camelCaseKeyword
(tag-with-type (csk/->PascalCaseKeyword node-type)))
(f/when-failed [e]
(log/error e)
(resolve-as nil {:resolver (format "%s/%s" (str target-ns) (name resolver))
:message (f/message e)})))))
[{:keys [auth]} settings
auth-filter-opts (auth/->auth-result auth ctx)
config-filter-opts (auth/config-filter-opts filters ctx)
resolver-filter-opts (auth/config-filter-opts (:filters params) ctx)
filter-options (merge {:id (or (:db-id this)
(:id this))}
auth-filter-opts
config-filter-opts
resolver-filter-opts)
rows (table-fetcher (get ctx db-key) filter-options {})
_ (when (empty? rows)
(f/fail "NotExistData"))]
(-> (first rows)
(relay/build-node node-type post-process-row)
transform-keys->camelCaseKeyword
(tag-with-type (csk/->PascalCaseKeyword node-type)))
(f/when-failed [e]
(log/error e)
(resolve-as nil {:resolver (format "%s/%s" (str target-ns) (name resolver))
:message (f/message e)})))))
(intern target-ns (symbol resolver) (fn [ctx args parent]
(f/attempt-all
[{:keys [auth
kebab-case?
return-camel-case?]
:or {kebab-case? true
return-camel-case? true}} settings
{:keys [args parent]} (->kebab-case kebab-case? args parent)
auth-filter-opts (auth/->auth-result auth ctx)
config-filter-opts (auth/config-filter-opts filters ctx)
resolver-filter-opts (auth/config-filter-opts (:filters params) ctx)
required-keys-in-parent (remove nil? [fk-in-parent pk-list-name-in-parent])
required-keys (s/difference (set required-keys-in-parent) (set (keys parent)))
_ (when (seq required-keys)
(f/fail (format "%s keys are needed in parent" required-keys)))
resolver-fn (find-resolver-fn resolver)
added-params (merge params {:additional-filter-opts (merge auth-filter-opts
config-filter-opts
resolver-filter-opts)})]
(cond-> (resolver-fn ctx args parent added-params)
return-camel-case? (util/update-resolver-result transform-keys->camelCaseKeyword))
(f/when-failed [e]
(log/error e)
(resolve-as nil {:resolver (format "%s/%s" (str target-ns) (name resolver))
:message (f/message e)}))))))))
[{:keys [auth
kebab-case?
return-camel-case?]
:or {kebab-case? true
return-camel-case? true}} settings
{:keys [args parent]} (->kebab-case kebab-case? args parent)
auth-filter-opts (auth/->auth-result auth ctx)
config-filter-opts (auth/config-filter-opts filters ctx)
resolver-filter-opts (auth/config-filter-opts (:filters params) ctx)
required-keys-in-parent (remove nil? [fk-in-parent pk-list-name-in-parent])
required-keys (s/difference (set required-keys-in-parent) (set (keys parent)))
_ (when (seq required-keys)
(f/fail (format "%s keys are needed in parent" required-keys)))
resolver-fn (find-resolver-fn resolver)
added-params (merge params {:additional-filter-opts (merge auth-filter-opts
config-filter-opts
resolver-filter-opts)})]
(cond-> (resolver-fn ctx args parent added-params)
return-camel-case? (util/update-resolver-result transform-keys->camelCaseKeyword))
(f/when-failed [e]
(log/error e)
(resolve-as nil {:resolver (format "%s/%s" (str target-ns) (name resolver))
:message (f/message e)}))))))))

(log/info (format "Gosura has generated resolvers => %s"
(mapv str
Expand Down
3 changes: 3 additions & 0 deletions src/gosura/csk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@

(def ->snake_case_keyword
(m/lru csk/->snake_case_keyword {} :lru/threshold 512))

(def ->camelCaseKeyword
(m/lru csk/->camelCaseKeyword {} :lru/threshold 512))
4 changes: 2 additions & 2 deletions src/gosura/helpers/node.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns gosura.helpers.node
(:require [camel-snake-kebab.core :as csk]
[com.walmartlabs.lacinia.schema :as schema]))
(:require [com.walmartlabs.lacinia.schema :as schema]
[gosura.csk :as csk]))

(defn tag-with-subtype
[{:keys [subtype] :as row} subtype->node-type]
Expand Down
8 changes: 4 additions & 4 deletions src/gosura/helpers/relay.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns gosura.helpers.relay
(:require [camel-snake-kebab.core :as csk]
[clojure.string]
(:require [clojure.string]
[gosura.csk :as csk]
[gosura.schema :as gosura-schema]
[gosura.util :refer [stringify-ids]]
[malli.core :as m]
Expand Down Expand Up @@ -157,8 +157,8 @@
next-rows (drop page-size remaining-rows)
has-next? (boolean (seq next-rows))
paged-rows (cond->> remaining-rows
page-size (take page-size)
(= page-direction :backward) reverse)
page-size (take page-size)
(= page-direction :backward) reverse)
edges (->> paged-rows
(map #(node->edge order-by %)))]
{:count (count edges)
Expand Down
36 changes: 18 additions & 18 deletions src/gosura/helpers/resolver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
를 resolver-fn 이라 부르자, 약속해봅니다.
"
(:require [camel-snake-kebab.core :as csk]
[clojure.string :refer [ends-with?]]
(:require [clojure.string :refer [ends-with?]]
[clojure.tools.logging :as log]
[com.walmartlabs.lacinia.resolve :refer [resolve-as]]
[com.walmartlabs.lacinia.schema :refer [tag-with-type]]
[gosura.auth :as auth]
[gosura.csk :as csk]
[gosura.helpers.error :as error]
[gosura.helpers.relay :as relay]
[gosura.helpers.response :as response]
Expand Down Expand Up @@ -59,7 +59,7 @@
(if ~authorized?
(let [~result (do (let ~let-mapping ~@body))]
(cond-> ~result
~return-camel-case? (update-resolver-result transform-keys->camelCaseKeyword)))
~return-camel-case? (update-resolver-result transform-keys->camelCaseKeyword)))
(resolve-as nil {:message "Unauthorized"})))))

(defn parse-fdecl
Expand Down Expand Up @@ -192,11 +192,11 @@
:page-options nil})
superfetch-id (hash superfetch-arguments)]
(with-superlifter (:superlifter context)
(-> (superlifter-api/enqueue! db-key (superfetcher superfetch-id superfetch-arguments))
(prom/then (fn [rows]
(-> (first rows)
(relay/build-node node-type post-process-row)
transform-keys->camelCaseKeyword)))))))
(-> (superlifter-api/enqueue! db-key (superfetcher superfetch-id superfetch-arguments))
(prom/then (fn [rows]
(-> (first rows)
(relay/build-node node-type post-process-row)
transform-keys->camelCaseKeyword)))))))

(defn resolve-by-fk
"Lacinia 리졸버로서 config 설정에 따라 단건 조회 쿼리를 처리한다.
Expand Down Expand Up @@ -226,10 +226,10 @@
:page-options page-options})
superfetch-id (hash superfetch-arguments)]
(with-superlifter (:superlifter context)
(-> (superlifter-api/enqueue! db-key (superfetcher superfetch-id superfetch-arguments))
(prom/then (fn [rows] (-> (first rows)
(relay/build-node node-type post-process-row)
transform-keys->camelCaseKeyword)))))))
(-> (superlifter-api/enqueue! db-key (superfetcher superfetch-id superfetch-arguments))
(prom/then (fn [rows] (-> (first rows)
(relay/build-node node-type post-process-row)
transform-keys->camelCaseKeyword)))))))

(defn resolve-connection
"Lacinia 리졸버로서 config 설정에 따라 목록 조회 쿼리를 처리한다.
Expand Down Expand Up @@ -342,12 +342,12 @@
:page-options page-options})
superfetch-id (hash superfetch-arguments)]
(with-superlifter (:superlifter context)
(-> (superlifter-api/enqueue! db-key (superfetcher superfetch-id superfetch-arguments))
(prom/then (fn [rows]
(->> rows
(map #(relay/build-node % node-type post-process-row))
(relay/build-connection order-by page-direction page-size cursor-id)
transform-keys->camelCaseKeyword)))))))
(-> (superlifter-api/enqueue! db-key (superfetcher superfetch-id superfetch-arguments))
(prom/then (fn [rows]
(->> rows
(map #(relay/build-node % node-type post-process-row))
(relay/build-connection order-by page-direction page-size cursor-id)
transform-keys->camelCaseKeyword)))))))

; TODO 다른 mutation helper 함수와 통합
(defn pack-mutation-result
Expand Down
4 changes: 2 additions & 2 deletions src/gosura/util.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns gosura.util
(:require [camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]
(:require [camel-snake-kebab.extras :as cske]
[clojure.string :refer [ends-with?]]
[com.walmartlabs.lacinia.resolve :refer [is-resolver-result?]]
[com.walmartlabs.lacinia.select-utils :refer [is-wrapped-value?]]
[gosura.csk :as csk]
[sentry-clj.core :as sentry]))

(defn keyword-vals->string-vals
Expand Down

0 comments on commit f634cb3

Please sign in to comment.