walmartlabs / lacinia

GraphQL implementation in pure Clojure
http://lacinia.readthedocs.io/en/latest/
Other
1.81k stars 162 forks source link

cryptic errors when input is the wrong type #429

Closed souenzzo closed 1 year ago

souenzzo commented 1 year ago

When the input type is a ["a"] and the graphql schema describes it as a input-object, it throws

java.lang.ClassCastException
Stack trace of root exception is empty; this is likely due to a JVM optimization that can be disabled with -XX:-OmitStackTraceInFastThrow.
java.lang.ClassCastException:

After add -XX:-OmitStackTraceInFastThrow, we can find a less-worst message:

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.Map$Entry (java.lang.String and java.util.Map$Entry are in module java.base of loader 'bootstrap')

But I think that it could be improved. For both the developer and the API consumer.

The root cause is this line: it assumes that the object is a {} and tries to reduce over its keys.

https://github.com/walmartlabs/lacinia/blob/master/src/com/walmartlabs/lacinia/parser.clj#L541

Here a test that reproduce the error

;; use -XX:-OmitStackTraceInFastThrow to easier debug
(deftest issue
  (with-redefs [lacinia/as-errors (fn [ex]
                                    (.printStackTrace ex)
                                    ex)]

    (is (= {}
          (-> {:scalars {:Label {:parse str
                                 :serialize str}}
               :input-objects {:LabelsInput {:fields {:v {:type 'String}}}}
               :queries {:hello {:type 'String
                                 :args {:labels {:type '(non-null :LabelsInput)}}
                                 :resolve (fn [_ args _]
                                            (prn args)
                                            "42")}}}
            schema/compile
            (lacinia/execute "query ($li: LabelsInput) { hello(labels: $li) }" {:li #{"a"}}
              {})
            (doto clojure.pprint/pprint))))))