diff --git a/src/gosura/core.clj b/src/gosura/core.clj index a5220ae..c988056 100644 --- a/src/gosura/core.clj +++ b/src/gosura/core.clj @@ -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] @@ -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) @@ -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 diff --git a/src/gosura/csk.clj b/src/gosura/csk.clj index 49ca688..5af34a6 100644 --- a/src/gosura/csk.clj +++ b/src/gosura/csk.clj @@ -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)) \ No newline at end of file diff --git a/src/gosura/helpers/node.clj b/src/gosura/helpers/node.clj index 910ae8d..c2a4b71 100644 --- a/src/gosura/helpers/node.clj +++ b/src/gosura/helpers/node.clj @@ -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] diff --git a/src/gosura/helpers/relay.clj b/src/gosura/helpers/relay.clj index ca000fa..b2e2f1c 100644 --- a/src/gosura/helpers/relay.clj +++ b/src/gosura/helpers/relay.clj @@ -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] @@ -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) diff --git a/src/gosura/helpers/resolver.clj b/src/gosura/helpers/resolver.clj index 16a9427..7122855 100644 --- a/src/gosura/helpers/resolver.clj +++ b/src/gosura/helpers/resolver.clj @@ -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] @@ -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 @@ -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 설정에 따라 단건 조회 쿼리를 처리한다. @@ -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 설정에 따라 목록 조회 쿼리를 처리한다. @@ -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 diff --git a/src/gosura/util.clj b/src/gosura/util.clj index a6055fc..bb3c967 100644 --- a/src/gosura/util.clj +++ b/src/gosura/util.clj @@ -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