From 2e9f648674e08337055b09eaf6527994adb0ac93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KARASZI=20Istv=C3=A1n?= Date: Wed, 12 Jun 2024 13:06:41 +0200 Subject: [PATCH 1/4] Remove unused requires --- src/leona/core.clj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/leona/core.clj b/src/leona/core.clj index e353896..cf9790c 100644 --- a/src/leona/core.clj +++ b/src/leona/core.clj @@ -1,14 +1,12 @@ (ns leona.core (:refer-clojure :exclude [compile]) - (:require [camel-snake-kebab.core :as csk] - [camel-snake-kebab.extras :as cske] + (:require [camel-snake-kebab.extras :as cske] [clojure.spec.alpha :as s] [clojure.string :as str] [clojure.walk :as walk] [com.walmartlabs.lacinia :as lacinia] [com.walmartlabs.lacinia.resolve :as lacinia-resolve] [com.walmartlabs.lacinia.schema :as lacinia-schema] - [com.walmartlabs.lacinia.util :as lacinia-util] [leona.schema :as leona-schema] [leona.util :as util])) From 44342f25f80c90a8e30d85adfb7c589744b38fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KARASZI=20Istv=C3=A1n?= Date: Wed, 12 Jun 2024 13:08:57 +0200 Subject: [PATCH 2/4] Add type hints to avoid reflection warnings --- src/leona/util.clj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/leona/util.clj b/src/leona/util.clj index c7a145d..ce83f87 100644 --- a/src/leona/util.clj +++ b/src/leona/util.clj @@ -1,7 +1,8 @@ (ns leona.util (:require [camel-snake-kebab.core :as csk] [clojure.spec.alpha :as s] - [clojure.string :as str])) + [clojure.string :as str]) + (:import (java.util.regex Pattern))) (defn replace-punctuation [s] @@ -16,7 +17,7 @@ (str/replace #"_XMARK_" "!"))) (defn- split-keep-delim - [s re-delim] + [^String s ^Pattern re-delim] (let [m (.matcher re-delim s)] ((fn step [last-end] (if (.find m) From 8905daabbba853f615a50ae688e8cac0d2979c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KARASZI=20Istv=C3=A1n?= Date: Wed, 12 Jun 2024 13:19:31 +0200 Subject: [PATCH 3/4] Remove unused requires --- test/leona/custom_scalar_test.clj | 17 ++++++++--------- test/leona/real_schema_test.clj | 12 ++++++------ test/leona/schema_test.clj | 13 ++++++------- test/leona/test_spec.clj | 3 +-- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/test/leona/custom_scalar_test.clj b/test/leona/custom_scalar_test.clj index 824a689..6f19eb5 100644 --- a/test/leona/custom_scalar_test.clj +++ b/test/leona/custom_scalar_test.clj @@ -1,13 +1,12 @@ (ns leona.custom-scalar-test - (:require [clj-time.core :as t] - [clj-time.format :as tf] - [clj-time.types :as tt] - [clojure.spec.alpha :as s] - [clojure.string :as str] - [clojure.test :refer :all] - [leona.core :as leona] - [leona.schema :as leona-schema] - [spec-tools.core :as st])) + (:require [clj-time.core :as t] + [clj-time.format :as tf] + [clj-time.types :as tt] + [clojure.spec.alpha :as s] + [clojure.test :refer :all] + [leona.core :as leona] + [leona.schema :as leona-schema] + [spec-tools.core :as st])) (deftest custom-scalars-are-preserved-at-schema-time (s/def ::date tt/date-time?) diff --git a/test/leona/real_schema_test.clj b/test/leona/real_schema_test.clj index 9eb46da..c819dfe 100644 --- a/test/leona/real_schema_test.clj +++ b/test/leona/real_schema_test.clj @@ -1,10 +1,10 @@ (ns leona.real-schema-test - (:require [clojure.spec.alpha :as s] - [clojure.spec.gen.alpha :as gen] - [clojure.string :as str] - [clojure.test :refer :all] - [leona.core :as leona] - [leona.schema :as schema])) + (:require [clojure.spec.alpha :as s] + [clojure.spec.gen.alpha :as gen] + [clojure.string :as str] + [clojure.test :refer :all] + [leona.core :as leona] + [leona.schema :as schema])) ;; Location (s/def :wh.location/street string?) diff --git a/test/leona/schema_test.clj b/test/leona/schema_test.clj index ec5b3c1..9c404ac 100644 --- a/test/leona/schema_test.clj +++ b/test/leona/schema_test.clj @@ -1,11 +1,10 @@ (ns leona.schema-test - (:require [clojure.spec.alpha :as s] - [clojure.test :refer :all] - [leona.core :as leona] - [leona.schema :as schema] - [leona.test-spec :as test] - [leona.util :as util] - [spec-tools.core :as st])) + (:require [clojure.spec.alpha :as s] + [clojure.test :refer :all] + [leona.schema :as schema] + [leona.test-spec :as test] + [leona.util :as util] + [spec-tools.core :as st])) (deftest fix-references-test (let [s {:objects {:Test {:fields {:b {:objects {:B {:fields {:a {:type '(non-null Int)}}}}}, diff --git a/test/leona/test_spec.clj b/test/leona/test_spec.clj index f0cf661..7dda340 100644 --- a/test/leona/test_spec.clj +++ b/test/leona/test_spec.clj @@ -1,6 +1,5 @@ (ns leona.test-spec - (:require [clojure.spec.alpha :as s] - [clojure.test :as t])) + (:require [clojure.spec.alpha :as s])) (s/def ::primary-functions (s/coll-of string?)) (s/def ::home-planet string?) From beecd25f96dd1284a9895e1481e22d6e871c852a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KARASZI=20Istv=C3=A1n?= Date: Wed, 12 Jun 2024 13:21:21 +0200 Subject: [PATCH 4/4] Reformat code with cljfmt --- src/leona/core.clj | 92 ++++++++++++++++++++-------------------- src/leona/schema.clj | 8 ++-- src/leona/util.clj | 11 +++-- test/leona/core_test.clj | 10 ++--- 4 files changed, 57 insertions(+), 64 deletions(-) diff --git a/src/leona/core.clj b/src/leona/core.clj index cf9790c..f639a3d 100644 --- a/src/leona/core.clj +++ b/src/leona/core.clj @@ -66,18 +66,18 @@ (defn format-enums [f m spec] (reduce - (fn [a spec-key] - (let [s (enum-spec->values spec-key) - v (util/get* m spec-key) - k (and v (if (contains? m spec-key) - spec-key - (util/remove-ns* spec-key)))] - (if (and s v) - (let [new-v (if (sequential? v) - (mapv #(f s %) v) - (f s v))] - (assoc a k new-v)) - a))) m (util/spec-keys spec))) + (fn [a spec-key] + (let [s (enum-spec->values spec-key) + v (util/get* m spec-key) + k (and v (if (contains? m spec-key) + spec-key + (util/remove-ns* spec-key)))] + (if (and s v) + (let [new-v (if (sequential? v) + (mapv #(f s %) v) + (f s v))] + (assoc a k new-v)) + a))) m (util/spec-keys spec))) (def format-input-enums "Format enum values in the input." @@ -240,12 +240,12 @@ "Walks a set of objects, attempting to inject field resolvers into certain types" [m frs] (update - m :objects - #(walk/postwalk - (fn [d] - (if (s/valid? ::field-with-type d) - (reduce-kv (fn [a k _] (inject-field-resolver a k frs)) d d) - d)) %))) + m :objects + #(walk/postwalk + (fn [d] + (if (s/valid? ::field-with-type d) + (reduce-kv (fn [a k _] (inject-field-resolver a k frs)) d d) + d)) %))) (defn add-external-schemas [generated schemas] @@ -288,21 +288,21 @@ "Given a map and map of input objects, replaces instances of input-object types with their transformed form" [m input-objects] (walk/postwalk - (fn replace-input-object-types [d] - (if (and (map? d) - (contains? d :type)) - (if (keyword? (:type d)) - (if (some #(= (:type d) %) (keys input-objects)) - (update d :type transform-input-object-key) - d) - (walk/postwalk - (fn replace-matched-type [n] ;; {:type ;..... } - (if (some #(= n %) (keys input-objects)) - (transform-input-object-key n) - n)) - d)) - d)) - m)) + (fn replace-input-object-types [d] + (if (and (map? d) + (contains? d :type)) + (if (keyword? (:type d)) + (if (some #(= (:type d) %) (keys input-objects)) + (update d :type transform-input-object-key) + d) + (walk/postwalk + (fn replace-matched-type [n] ;; {:type ;..... } + (if (some #(= n %) (keys input-objects)) + (transform-input-object-key n) + n)) + d)) + d)) + m)) (defn inject-custom-scalars "Add custom scalars to the Lacinia schema" @@ -326,20 +326,20 @@ field-resolvers (not-empty (:field-resolvers m)) custom-scalars (not-empty (:custom-scalars m))] (cond-> (apply leona-schema/combine-with-opts opts (:specs m)) - queries (assoc :queries (-> queries - (dissoc-all :enums) - (dissoc-all :input-objects) + queries (assoc :queries (-> queries + (dissoc-all :enums) + (dissoc-all :input-objects) + (replace-input-objects input-objects))) + mutations (assoc :mutations (-> mutations + (dissoc-all :enums) + (dissoc-all :input-objects) + (replace-input-objects input-objects))) + input-objects (assoc :input-objects (-> input-objects + (transform-input-object-keys) (replace-input-objects input-objects))) - mutations (assoc :mutations (-> mutations - (dissoc-all :enums) - (dissoc-all :input-objects) - (replace-input-objects input-objects))) - input-objects (assoc :input-objects (-> input-objects - (transform-input-object-keys) - (replace-input-objects input-objects))) - enums (update :enums merge enums) - field-resolvers (inject-field-resolvers field-resolvers) - custom-scalars (inject-custom-scalars custom-scalars)))) + enums (update :enums merge enums) + field-resolvers (inject-field-resolvers field-resolvers) + custom-scalars (inject-custom-scalars custom-scalars)))) (defn compile "Generates a Lacinia schema from pre-compiled data structure and compiles it. diff --git a/src/leona/schema.clj b/src/leona/schema.clj index e4909ca..8fda317 100644 --- a/src/leona/schema.clj +++ b/src/leona/schema.clj @@ -1,14 +1,13 @@ (ns leona.schema (:refer-clojure :exclude [list]) - (:require [clojure.spec.alpha :as s] - [clojure.set :as set] + (:require [clojure.set :as set] + [clojure.spec.alpha :as s] [clojure.walk :as walk] [leona.util :as util] [spec-tools.core :as st] [spec-tools.impl :as impl] [spec-tools.visitor :as visitor])) - (def valid-type-override-syms "These symbols can be used to wrap types when using `st/spec`, e.g. `(st/spec my-date-pred? {:type '(custom :date)})`. They only exist separately to provide semantic information - it's not asserted anywhere that the types they reference ever exist anywhere so USE WITH CAUTION." #{'enum 'custom}) @@ -357,7 +356,7 @@ :spec spec}))} (when enums {:enums enums}))) - (throw (Exception. (str "Cannot process anonymous `s/keys` specs. Please provide a name: " spec) ))))) + (throw (Exception. (str "Cannot process anonymous `s/keys` specs. Please provide a name: " spec)))))) (defmethod accept-spec 'clojure.spec.alpha/or [_ spec children _] (if-let [t (some #(when (not= ::invalid %) %) children)] @@ -471,7 +470,6 @@ (catch Exception _ (when spec ::invalid)))) - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn combine diff --git a/src/leona/util.clj b/src/leona/util.clj index ce83f87..52781fc 100644 --- a/src/leona/util.clj +++ b/src/leona/util.clj @@ -107,7 +107,6 @@ (def clj-name->qualified-gql-enum-name (partial clj-name->qualified-gql-name-impl SCREAMING_SNAKE_CASE)) - (defn gql-name->clj-name [t] (-> t @@ -122,11 +121,11 @@ "In this fn we attempt to contains? k on m (map or set), but we spin through some cases and return the one that fits" ([m t] ;; this is simply necessary to keep csk loaded (find-case-match - m t [csk/->camelCase - csk/->PascalCase - csk/->SCREAMING_SNAKE_CASE - csk/->snake_case - csk/->kebab-case])) + m t [csk/->camelCase + csk/->PascalCase + csk/->SCREAMING_SNAKE_CASE + csk/->snake_case + csk/->kebab-case])) ([m t fns] (when-let [case-fn (first fns)] (let [s (if (str/starts-with? (str t) ":") diff --git a/test/leona/core_test.clj b/test/leona/core_test.clj index e330cd1..a8cb2b6 100644 --- a/test/leona/core_test.clj +++ b/test/leona/core_test.clj @@ -222,7 +222,6 @@ {:test_query {:input {:num 1, :nums [2 3]}}} {})] (is (= 6 (get-in result [:data :test :num]))))) - ;;;;; (deftest input-objects-nested-list @@ -242,8 +241,6 @@ result (leona/execute compiled-schema "query test($inputs: [InputInput!]!) { test(inputs: $inputs) { num }}" {:inputs [{:num 1, :nums [2 3]}]} {})] (is (= 6 (get-in result [:data :test :num]))))) - - ;;;;; (deftest middleware-test @@ -375,7 +372,6 @@ (leona/compile))] (is (= identity (get-in r [:generated :queries :alice :resolve]))))) - ;;;;;;; (deftest add-alias-basic-test @@ -462,9 +458,9 @@ (s/def ::result string?) (s/def ::my-query (s/keys :req-un [::result])) (let [my-query-resolver (fn [ctx args value] {:result "hello"}) - schema(-> (leona/create) - (leona/attach-query ::no-args ::my-query my-query-resolver) - (leona/compile)) + schema (-> (leona/create) + (leona/attach-query ::no-args ::my-query my-query-resolver) + (leona/compile)) r (leona/execute schema "{ myQuery { result } }")] (is (= "hello" (get-in r [:data :myQuery :result])))))