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

csk memoize + 직접 구현 #96

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
io.github.hlship/trace {:git/url "https://github.com/jungwookim/trace"
:git/sha "27e0faf04fa865d902730da9b944053445f0be7d"}
io.sentry/sentry-clj {:mvn/version "5.7.180"}
org.clojure/core.memoize {:mvn/version "1.0.257"}
org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/tools.logging {:mvn/version "1.2.4"}
org.clojure/core.match {:mvn/version "1.0.0"}
Expand Down
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
47 changes: 47 additions & 0 deletions src/gosura/csk.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns gosura.csk
(:require [camel-snake-kebab.core :as csk]
[clojure.core.memoize :as m]
Copy link
Member

Choose a reason for hiding this comment

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

no need!

[clojure.string :as string]))

(def ->kebab-case
(memoize csk/->kebab-case))

(defn- request-camelCase->kebab-case-keyword
[s]
(let [s (name s)]
(keyword (string/replace s #"[A-Z]" #(str "-" (string/lower-case %))))))

(defn- request-kebab-case->camelCaseKeyword
[s]
(let [s (name s)]
(keyword (string/replace s #"-([a-z])" #(string/upper-case (second %))))))

(def camelCase->kebab-case-keyword
(memoize request-camelCase->kebab-case-keyword))

(def kebab-case->camelCaseKeyword
(memoize request-kebab-case->camelCaseKeyword))

(def ->kebab-case-keyword
(memoize csk/->kebab-case-keyword))

(def ->kebab-case-string
(memoize csk/->kebab-case-string))

(def ->PascalCaseString
(memoize csk/->PascalCaseString))

(def ->PascalCaseKeyword
(memoize csk/->PascalCaseKeyword))

(def ->PascalCase
(memoize csk/->PascalCase))

(def ->snake_case
(memoize csk/->snake_case))

(def ->snake_case_keyword
(memoize csk/->snake_case_keyword))

(def ->camelCaseKeyword
(memoize csk/->camelCaseKeyword))
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
8 changes: 4 additions & 4 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 All @@ -23,12 +23,12 @@
(defn transform-keys->kebab-case-keyword
"재귀적으로 form 안에 포함된 모든 key를 camelCase keyword로 변환한다"
[form]
(cske/transform-keys csk/->kebab-case-keyword form))
(cske/transform-keys csk/camelCase->kebab-case-keyword form))

(defn transform-keys->camelCaseKeyword
"재귀적으로 form 안에 포함된 모든 key를 camelCase keyword로 변환한다"
[form]
(cske/transform-keys csk/->camelCaseKeyword form))
(cske/transform-keys csk/kebab-case->camelCaseKeyword form))

(defn send-sentry-server-event
[event]
Expand Down