Skip to content

Commit

Permalink
fix: Restrict sequential selection sets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilmo Raunio committed Apr 26, 2024
1 parent 34f8b4f commit 0211bc8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/oksa/parse.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@
[:? [:schema [:ref ::TypeOpts]]]
[:schema [:ref ::Type]]]
::SelectionSet [:orn
[::SelectionSet [:+ [:catn
[::node [:schema [:ref ::Selection]]]
[::children [:? [:schema [:ref ::SelectionSet]]]]]]]]
[::SelectionSet [:+ [:alt
[:catn
[::node [:schema [:ref ::Selection]]]
[::children [:? [:schema [:ref ::SelectionSet]]]]]
[:catn
[::node [:schema [:ref ::FieldSelection]]]]]]]]
::FieldSelection [:orn [::WrappedField [:schema [:ref ::Field]]]]
::Selection [:orn
[::WrappedField [:schema [:ref ::Field]]]
[::NakedField [:schema [:ref ::NakedField]]]
[::FragmentSpread [:schema [:ref ::FragmentSpread]]]
[::InlineFragment [:schema [:ref ::InlineFragment]]]]
Expand Down
12 changes: 8 additions & 4 deletions test/oksa/alpha/api_test.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns oksa.alpha.api-test
(:require [#?(:clj clojure.test
:cljs cljs.test) :as t]
[oksa.core :as oksa]
[oksa.alpha.api :as api])
#?(:clj (:import [graphql.parser Parser])))

Expand Down Expand Up @@ -98,9 +99,6 @@
(unparse-and-validate (api/select :bar
(api/select :qux
(api/select :baz))))
(unparse-and-validate (api/select (api/field :bar)
(api/select (api/field :qux)
(api/select (api/field :baz)))))
(unparse-and-validate (api/select
(api/field :bar
(api/select
Expand Down Expand Up @@ -469,4 +467,10 @@
(t/is (= "query ($foo:Bar @fooDirective(fooArg:123)){fooField}"
(unparse-and-validate (api/query (api/opts (api/variable :foo (api/opts (api/directive :fooDirective {:fooArg 123}))
:Bar))
(api/select :fooField)))))))
(api/select :fooField))))))
(t/testing "sequential selection sets should throw an exception"
(t/is (thrown? #?(:clj Exception :cljs js/Error)
(oksa/gql
(api/select (api/field :foo
(api/select :qux :baz))
(api/select :basho)))))))
6 changes: 5 additions & 1 deletion test/oksa/core_test.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns oksa.core-test
(:require [#?(:clj clojure.test
:cljs cljs.test) :as t]
[oksa.core :as oksa]
[oksa.test-util :refer [unparse-and-validate]]
[oksa.alpha.api :as api])
#?(:clj (:import [graphql.parser Parser])))
Expand Down Expand Up @@ -290,7 +291,10 @@
[:fooField]])))
(t/is (= "query ($foo:Bar @fooDirective(fooArg:123)){fooField}"
(unparse-and-validate [:oksa/query {:variables [:foo {:directives [[:fooDirective {:arguments {:fooArg 123}}]]} :Bar]}
[:fooField]])))))
[:fooField]]))))
(t/testing "sequential selection sets should throw an exception"
(t/is (thrown? #?(:clj Exception :cljs js/Error)
(oksa/gql [[:foo {} [:qux :baz]] [:basho]])))))

(t/deftest gql-test
(t/is (= "{foo}\nquery {bar}\nmutation {qux}\nsubscription {baz}\nfragment foo on Foo{bar}\n{foo2}\nquery {bar2}\nmutation {qux2}\nsubscription {baz2}\nfragment foo2 on Foo2{bar2}"
Expand Down

0 comments on commit 0211bc8

Please sign in to comment.