Skip to content

Commit

Permalink
ensure variables are preprocessed before fragment canonicalisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mverardo authored and Yannick Scherer committed Mar 6, 2017
1 parent f5f3586 commit ceb2dd2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/alumbra/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 4 additions & 11 deletions src/alumbra/canonical/operations.clj
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -18,7 +17,7 @@

;; ## Operation Resolution

(defn- select-operation
(defn select-operation
[operations operation-name']
(cond operation-name'
(or (some
Expand All @@ -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)))
31 changes: 31 additions & 0 deletions test/alumbra/canonicalizer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit ceb2dd2

Please sign in to comment.