Skip to content

Commit

Permalink
Update :venia/operation specs
Browse files Browse the repository at this point in the history
  • Loading branch information
macluck committed Jun 2, 2017
1 parent b8193c9 commit 2a50e08
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 92 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,24 @@ an operation type and name. Notice, currently on `:query` operations are support


```clj
(v/graphql-query {:venia/operation :query
:venia/operation-name "employeeQuery"
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [{:query/data [:employee {:id :$id
:active true
:name :$name}
:fragment/comparisonFields]
:query/alias :workhorse}
{:query/data [:employee {:id :$id
:active false}
:fragment/comparisonFields]
:query/alias :boss}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [:name :address [:friends [:name :email]]]}]})
(v/graphql-query {:venia/operation {:operation/type :query
:operation/name "employeeQuery"}
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [{:query/data [:employee {:id :$id
:active true
:name :$name}
:fragment/comparisonFields]
:query/alias :workhorse}
{:query/data [:employee {:id :$id
:active false}
:fragment/comparisonFields]
:query/alias :boss}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [:name :address [:friends [:name :email]]]}]})

=> prettified:
query employeeQuery($id: Int, $name: String) {
Expand Down
3 changes: 1 addition & 2 deletions src/venia/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@
[[_ query]]
"Given a spec conformed root query map, creates a complete query string."
(let [operation (:venia/operation query)
operation-name (:venia/operation-name query)
operation-with-name (when (and operation operation-name) (str (name operation) " " operation-name))
operation-with-name (when operation (str (name (:operation/type operation)) " " (:operation/name operation)))
variables (:venia/variables query)
variables-str (when variables (str "(" (variables->str variables) ")"))
fragments (:venia/fragments query)
Expand Down
7 changes: 4 additions & 3 deletions src/venia/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
(s/def :venia/fragment (s/keys :req [:fragment/name :fragment/type :fragment/fields]))
(s/def :venia/fragments (s/coll-of :venia/fragment :min-count 1))

(s/def :venia/operation #{:query})
(s/def :venia/operation-name string?)
(s/def :operation/type #{:query})
(s/def :operation/name string?)
(s/def :venia/operation (s/keys :req [:operation/type :operation/name]))

(s/def :variable/name string?)
(s/def :variable/type keyword?)
Expand All @@ -48,7 +49,7 @@

(s/def :venia/query-def (s/keys :req [:venia/queries]
:opt [:venia/fragments
:venia/operation :venia/operation-name
:venia/operation
:venia/variables]))

(defn query->spec [query]
Expand Down
56 changes: 28 additions & 28 deletions test/venia/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -104,39 +104,39 @@
(is (= query-str result))))

(testing "Should create a valid graphql query with variables"
(let [data {:venia/operation :query
:venia/operation-name "employeeQuery"
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [[:employee {:id :$id
:active true
:name :$name}
[:name :address [:friends [:name :email]]]]]}
(let [data {:venia/operation {:operation/type :query
:operation/name "employeeQuery"}
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [[:employee {:id :$id
:active true
:name :$name}
[:name :address [:friends [:name :email]]]]]}
query-str (str "query employeeQuery($id:Int,$name:String){employee(id:$id,active:true,name:$name){name,address,friends{name,email}}}")
result (v/graphql-query data)]
(is (= query-str result))))

(testing "Should create a valid graphql query with variables, aliases and fragments"
(let [data {:venia/operation :query
:venia/operation-name "employeeQuery"
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [{:query/data [:employee {:id :$id
:active true
:name :$name}
:fragment/comparisonFields]
:query/alias :workhorse}
{:query/data [:employee {:id :$id
:active false}
:fragment/comparisonFields]
:query/alias :boss}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [:name :address [:friends [:name :email]]]}]}
(let [data {:venia/operation {:operation/type :query
:operation/name "employeeQuery"}
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [{:query/data [:employee {:id :$id
:active true
:name :$name}
:fragment/comparisonFields]
:query/alias :workhorse}
{:query/data [:employee {:id :$id
:active false}
:fragment/comparisonFields]
:query/alias :boss}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [:name :address [:friends [:name :email]]]}]}
query-str (str "query employeeQuery($id:Int,$name:String){workhorse:employee(id:$id,active:true,name:$name){...comparisonFields},"
"boss:employee(id:$id,active:false){...comparisonFields}} fragment comparisonFields on Worker{name,address,friends{name,email}}")
result (v/graphql-query data)]
Expand Down
82 changes: 41 additions & 41 deletions test/venia/spec_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,44 @@
[:venia/field :email]]}]]}]]}]
(vs/query->spec {:venia/queries [[:employee {:id 1 :active true} [:name :address [:friends [:name :email]]]]]}))))
(testing "Valid vector with all possible data, should return conformed data"
(is (= [:venia/query-def {:venia/operation :query
:venia/operation-name "employeeQuery"
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [[:venia/field :name] [:venia/field :address]
[:venia/nested-field {:venia/nested-field-root :friends
:venia/nested-field-children [[:venia/field :name]
[:venia/field :email]]}]]}]
:venia/queries [[:venia/query-with-data {:query/data {:query :employee
:args {:id :$id
:active true
:name :$name}
:fields :fragment/comparisonFields}
:query/alias :workhorse}]
[:venia/query-with-data {:query/data {:query :employee
:args {:id :$id
:active false}
:fields :fragment/comparisonFields}
:query/alias :boss}]]}]
(vs/query->spec {:venia/operation :query
:venia/operation-name "employeeQuery"
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [{:query/data [:employee {:id :$id
:active true
:name :$name}
:fragment/comparisonFields]
:query/alias :workhorse}
{:query/data [:employee {:id :$id
:active false}
:fragment/comparisonFields]
:query/alias :boss}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [:name :address [:friends [:name :email]]]}]})))))
(is (= [:venia/query-def {:venia/operation {:operation/type :query
:operation/name "employeeQuery"}
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [[:venia/field :name] [:venia/field :address]
[:venia/nested-field {:venia/nested-field-root :friends
:venia/nested-field-children [[:venia/field :name]
[:venia/field :email]]}]]}]
:venia/queries [[:venia/query-with-data {:query/data {:query :employee
:args {:id :$id
:active true
:name :$name}
:fields :fragment/comparisonFields}
:query/alias :workhorse}]
[:venia/query-with-data {:query/data {:query :employee
:args {:id :$id
:active false}
:fields :fragment/comparisonFields}
:query/alias :boss}]]}]
(vs/query->spec {:venia/operation {:operation/type :query
:operation/name "employeeQuery"}
:venia/variables [{:variable/name "id"
:variable/type :Int}
{:variable/name "name"
:variable/type :String}]
:venia/queries [{:query/data [:employee {:id :$id
:active true
:name :$name}
:fragment/comparisonFields]
:query/alias :workhorse}
{:query/data [:employee {:id :$id
:active false}
:fragment/comparisonFields]
:query/alias :boss}]
:venia/fragments [{:fragment/name :comparisonFields
:fragment/type :Worker
:fragment/fields [:name :address [:friends [:name :email]]]}]})))))

0 comments on commit 2a50e08

Please sign in to comment.