Skip to content
Draft
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
96 changes: 47 additions & 49 deletions src/leona/core.clj
Original file line number Diff line number Diff line change
@@ -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]))

Expand Down Expand Up @@ -68,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."
Expand Down Expand Up @@ -242,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]
Expand Down Expand Up @@ -290,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"
Expand All @@ -328,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.
Expand Down
8 changes: 3 additions & 5 deletions src/leona/schema.clj
Original file line number Diff line number Diff line change
@@ -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})
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -471,7 +470,6 @@
(catch Exception _
(when spec ::invalid))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn combine
Expand Down
16 changes: 8 additions & 8 deletions src/leona/util.clj
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -106,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
Expand All @@ -121,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) ":")
Expand Down
10 changes: 3 additions & 7 deletions test/leona/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -375,7 +372,6 @@
(leona/compile))]
(is (= identity (get-in r [:generated :queries :alice :resolve])))))


;;;;;;;

(deftest add-alias-basic-test
Expand Down Expand Up @@ -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])))))

Expand Down
17 changes: 8 additions & 9 deletions test/leona/custom_scalar_test.clj
Original file line number Diff line number Diff line change
@@ -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?)
Expand Down
12 changes: 6 additions & 6 deletions test/leona/real_schema_test.clj
Original file line number Diff line number Diff line change
@@ -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?)
Expand Down
13 changes: 6 additions & 7 deletions test/leona/schema_test.clj
Original file line number Diff line number Diff line change
@@ -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)}}}}},
Expand Down
3 changes: 1 addition & 2 deletions test/leona/test_spec.clj
Original file line number Diff line number Diff line change
@@ -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?)
Expand Down