From 0211bc8730fb39e05779c2d957f3472f370cdd02 Mon Sep 17 00:00:00 2001 From: Ilmo Raunio Date: Fri, 26 Apr 2024 23:26:58 +0300 Subject: [PATCH] fix: Restrict sequential selection sets --- src/oksa/parse.cljc | 11 +++++++---- test/oksa/alpha/api_test.cljc | 12 ++++++++---- test/oksa/core_test.cljc | 6 +++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/oksa/parse.cljc b/src/oksa/parse.cljc index 08316cc..2eee3b6 100644 --- a/src/oksa/parse.cljc +++ b/src/oksa/parse.cljc @@ -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]]]] diff --git a/test/oksa/alpha/api_test.cljc b/test/oksa/alpha/api_test.cljc index 3c51ad9..35e4b7e 100644 --- a/test/oksa/alpha/api_test.cljc +++ b/test/oksa/alpha/api_test.cljc @@ -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]))) @@ -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 @@ -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))))))) diff --git a/test/oksa/core_test.cljc b/test/oksa/core_test.cljc index 1f9dba3..f150e6b 100644 --- a/test/oksa/core_test.cljc +++ b/test/oksa/core_test.cljc @@ -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]))) @@ -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}"