Skip to content

Commit

Permalink
Add list support for :variable/type
Browse files Browse the repository at this point in the history
  • Loading branch information
r0man committed Mar 23, 2018
1 parent 0335a25 commit 349c90b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/venia/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,22 @@
(interpose ",")
(apply str))))

(defn- variable-type->str
"Convert a spec conformed variable type to a string."
[[tag type]]
(case tag
:list
(str "[" (-> type first name) "]")
:type
(name type)))

(defn variables->str
"Given a vector of variable maps, formats them and concatenates to string.
E.g. (variables->str [{:variable/name \"id\" :variable/type :Int}]) => \"$id: Int\""
[variables]
(->> (for [{var-name :variable/name var-type :variable/type var-default :variable/default} variables]
(str "$" var-name ":" (name var-type) (when var-default (str "=" (arg->str var-default)))))
(str "$" var-name ":" (variable-type->str var-type) (when var-default (str "=" (arg->str var-default)))))
(interpose ",")
(apply str)))

Expand Down
5 changes: 4 additions & 1 deletion src/venia/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@
(s/def :venia/operation (s/keys :req [:operation/type :operation/name]))

(s/def :variable/name string?)
(s/def :variable/type keyword?)

(s/def :variable/type
(s/or :type keyword? :list (s/coll-of keyword? :kind vector? :count 1)))

(s/def :query/variable (s/keys :req [:variable/name :variable/type]
:opt [:variable/default]))
(s/def :venia/variables (s/coll-of :query/variable :min-count 1))
Expand Down
26 changes: 20 additions & 6 deletions test/venia/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@

(deftest variables->str-test
(is (= "$id:Int" (v/variables->str [{:variable/name "id"
:variable/type :Int}])))
:variable/type [:type :Int]}])))
(is (= "$ids:[Int!]" (v/variables->str [{:variable/name "ids"
:variable/type [:list [:Int!]]}])))
(is (= "$id:Int=2" (v/variables->str [{:variable/name "id"
:variable/type :Int
:variable/type [:type :Int]
:variable/default 2}])))
(is (= "$id:Int,$name:String" (v/variables->str [{:variable/name "id"
:variable/type :Int}
:variable/type [:type :Int]}
{:variable/name "name"
:variable/type :String}])))
:variable/type [:type :String]}])))
(is (= "$id:Int=1,$name:String=\"my-name\"" (v/variables->str [{:variable/name "id"
:variable/type :Int
:variable/type [:type :Int]
:variable/default 1}
{:variable/name "name"
:variable/type :String
:variable/type [:type :String]
:variable/default "my-name"}])))
(is (= "" (v/variables->str nil)))
(is (= "" (v/variables->str []))))
Expand Down Expand Up @@ -280,3 +282,15 @@
query-str (str "mutation AddProjectToEmployee($id:Int!,$project:ProjectNameInput!){addProject(employeeId:$id,project:$project){allocation,name}}")
result (v/graphql-query data)]
(is (= query-str result)))))

(deftest test-query-with-list-variables
(is (= (v/graphql-query
{:venia/operation
{:operation/type :query
:operation/name "MyQuery"}
:venia/queries
[[:node {:ids :$ids} [:id]]]
:venia/variables
[{:variable/name "ids"
:variable/type [:ID!]}]})
"query MyQuery($ids:[ID!]){node(ids:$ids){id}}")))
4 changes: 2 additions & 2 deletions test/venia/spec_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
(is (= [:venia/query-def {:venia/operation {:operation/type :query
:operation/name "employeeQuery"}
:venia/variables [{:variable/name "id"
:variable/type :Int}
:variable/type [:type :Int]}
{:variable/name "name"
:variable/type :String}]
:variable/type [:type :String]}]
:venia/fragments [{:fragment/name "comparisonFields"
:fragment/type :Worker
:fragment/fields [[:venia/field :name] [:venia/field :address]
Expand Down

0 comments on commit 349c90b

Please sign in to comment.