 
 (declare check-selection-set)
 
+(defn- check-argument [a {:keys [name value] :as arg}]
+  ;; (println "ARG:" arg)
+  (case (:tag value)
+    :variable-reference (update a :var-use conj name)
+    a))
+                       
+(defn- check-arguments [a {argdecls :arguments :as fdecl} {args :arguments :as f}]
+  (if (and (nil? argdecls) (nil? args))
+    a ;; common case
+    (reduce check-argument a args)))
+
 (defn- check-field [tdef field-map a f]
   (case (:tag f)
     :selection-field
     (if-let [fdecl (field-map (:name f))]
-      (let [[errors f] (check-selection-set (base-type (:type fdecl)) a (assoc f :resolved-type (:type fdecl)))]
+      (let [[errors f] (check-selection-set (base-type (:type fdecl)) (check-arguments a fdecl f) (assoc f :resolved-type (:type fdecl)))]
         (-> (assoc a :errors errors)
             (update :vfields conj f)))
       (if (:alias f)

          a (reduce (partial check-field tdef field-map) (assoc a :vfields []) sset)]
      [(:errors a) (assoc def :selection-set (:vfields a))])
    [(:errors a) def]))

(defn- check-definition [a def]
  (case (:tag def)

  (case (:tag type)
    :basic-type (:name type)
    :list-type (recur (:inner-type type))))

(defn- type-string
  ([type] (.toString (type-string (StringBuilder.) type)))
  ([^StringBuilder buf type]
   (case (:tag type)
     :basic-type (.append buf (:name type))
     :list-type (-> buf
                    (.append \[)
                    (type-string (:inner-type type))
                    (.append \])))
   (if (:required type) (.append buf \!) buf)))     

(defn- get-root [schema def]
  (get (:roots schema) (if (= :mutation-definition (:tag def)) :mutation :query)))
