diff --git a/src/alumbra/analyzer.clj b/src/alumbra/analyzer.clj index 011437a..3e5a49e 100644 --- a/src/alumbra/analyzer.clj +++ b/src/alumbra/analyzer.clj @@ -9,8 +9,9 @@ [unions :as unions] [valid-fragment-spreads :as valid-fragment-spreads]] [alumbra.canonical + [variables :refer [resolve-variables]] [fragments :refer [resolve-fragments]] - [operations :refer [resolve-operation]]] + [operations :refer [select-operation resolve-operation]]] [clojure.java.io :as io])) ;; ## Base Functionality @@ -136,11 +137,13 @@ ([analyzed-schema document operation-name] (canonicalize-operation analyzed-schema document operation-name {})) ([analyzed-schema document operation-name variables] - (let [{:keys [alumbra/fragments alumbra/operations]} document] + (let [{:keys [alumbra/fragments alumbra/operations]} document + operation (select-operation operations operation-name)] (-> {:schema analyzed-schema :variables variables} + (resolve-variables operation) (resolve-fragments fragments) - (resolve-operation operations operation-name))))) + (resolve-operation operation))))) (defn canonicalizer "Create a function canonicalizing GraphQL documents conforming to diff --git a/src/alumbra/canonical/operations.clj b/src/alumbra/canonical/operations.clj index 3e66473..01c4e44 100644 --- a/src/alumbra/canonical/operations.clj +++ b/src/alumbra/canonical/operations.clj @@ -1,8 +1,7 @@ (ns alumbra.canonical.operations (:require [alumbra.canonical [selection-set :refer [resolve-selection-set]] - [directives :refer [resolve-directives]] - [variables :refer [resolve-variables]]])) + [directives :refer [resolve-directives]]])) ;; ## Helpers @@ -18,7 +17,7 @@ ;; ## Operation Resolution -(defn- select-operation +(defn select-operation [operations operation-name'] (cond operation-name' (or (some @@ -36,21 +35,15 @@ :else (first operations))) -(defn- resolve-operation* +(defn resolve-operation [opts {:keys [alumbra/selection-set alumbra/directives alumbra/operation-type alumbra/operation-name] :as op}] (let [opts (-> opts - (assoc :scope-type (root-type opts op)) - (resolve-variables op)) + (assoc :scope-type (root-type opts op))) selection (resolve-selection-set opts selection-set)] {:operation-name operation-name :operation-type operation-type :selection-set selection :directives (resolve-directives opts directives)})) - -(defn resolve-operation - [opts operations operation-name] - (->> (select-operation operations operation-name) - (resolve-operation* opts))) diff --git a/test/alumbra/canonicalizer_test.clj b/test/alumbra/canonicalizer_test.clj index 649687a..6621e66 100644 --- a/test/alumbra/canonicalizer_test.clj +++ b/test/alumbra/canonicalizer_test.clj @@ -124,3 +124,34 @@ randomCat(q: $q) { name } }" {"q" nil}))))) + +(deftest t-fragment-arguments + (letfn [(canonicalize [query & [variables]] + (let [ast (ql/parse-document query)] + (if variables + (analyzer/canonicalize-operation schema ast nil variables) + (analyzer/canonicalize-operation schema ast))))] + (is (= {"q" + {"emotions" + [{:type-name "Emotion", :non-null? true, :value "HAPPY"}]}} + (-> (canonicalize + "query ($q: CatQuery = {emotions: [HAPPY HAPPIER]}) { + randomCat(q: $q) @test { name } + }" + {"q" {"emotions" ["HAPPY"]}}) + :selection-set + first + :arguments) + (-> (canonicalize + "query ($q: CatQuery = {emotions: [HAPPY HAPPIER]}) { + ...F0 + } + fragment F0 on QueryRoot @test { + randomCat(q: $q) { name } + }" + {"q" {"emotions" ["HAPPY"]}}) + :selection-set + first + :selection-set + first + :arguments)))))