Skip to content

Commit

Permalink
Merge pull request #4 from Xerpa/fix-nested-fields-with-args
Browse files Browse the repository at this point in the history
Fix nested fields with arguments causing errors
  • Loading branch information
macluck authored May 25, 2017
2 parents 77fba3c + 9ca38cd commit a79ce55
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/venia/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
(->> (for [[type value] fields]
(condp = type
:venia/field (name value)
:venia/nested-field (str (name (:venia/nested-field-root value)) "{" (fields->str (:venia/nested-field-children value)) "}")))
:venia/nested-field (str (name (:venia/nested-field-root value)) (when (:args value) (str "(" (arguments->str (:args value)) ")")) "{" (fields->str (:venia/nested-field-children value)) "}")))
(interpose ",")
(apply str)))

Expand Down Expand Up @@ -119,4 +119,4 @@
[data]
(->> (map ->query-str (spec/query->spec data))
(interpose " ")
(apply str)))
(apply str)))
4 changes: 2 additions & 2 deletions src/venia/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

(s/def :venia/query-name keyword?)
(s/def :venia/fields (s/coll-of (s/or :venia/field keyword?
:venia/nested-field (s/cat :venia/nested-field-root keyword? :venia/nested-field-children :venia/fields))))
:venia/nested-field (s/cat :venia/nested-field-root keyword? :args (s/? :venia/args) :venia/nested-field-children :venia/fields))))
(s/def :venia/args (s/keys :opt [:venia/alias]))
(s/def :venia/query (s/cat :query :venia/query-name :args (s/? :venia/args) :fields :venia/fields))
(s/def :venia/query-with-fragment (s/cat :query :venia/query-name :args (s/? :venia/args) :fragment-name keyword?))
Expand Down Expand Up @@ -33,4 +33,4 @@
(if (invalid? conformed)
(ex/throw-ex {:venia/ex-type :venia/spec-validation
:venia/ex-explain (s/explain :venia/query-def query)})
conformed)))
conformed)))
5 changes: 5 additions & 0 deletions test/venia/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
query-str "{employee(id:1,active:true){name,address,friends{name,email}}}"]
(is (= query-str (v/graphql-query data)))))

(testing "Should create a valid graphql string using params on nested fields."
(let [data [[[:employee {:id 1 :active true} [:name :address [:friends {:id 1} [:name :email]]]]]]
query-str "{employee(id:1,active:true){name,address,friends(id:1){name,email}}}"]
(is (= query-str (v/graphql-query data)))))

(testing "Invalid query, should throw exception"
(is (thrown? #?(:clj Exception
:cljs js/Error) (v/graphql-query []))))
Expand Down

0 comments on commit a79ce55

Please sign in to comment.