walmartlabs/lacinia

NullPointerException when processing dynamic argument

lxbr opened this issue · 3 comments

lxbr commented

We use filters on collections that can be composed recursively via boolean logic. In the example below the or field takes an array of StringFilters. When the array contains a StringFilter with a constant ("blue") and another one with a dynamic argument ($color) the query execution returns an error. The NPE seems to occur at https://github.com/walmartlabs/lacinia/blob/v0.38.0/src/com/walmartlabs/lacinia/parser.clj#L637 when literal-values is {:color {:equals "blue"}} and dynamic-extractor is nil.

Setup

com.walmartlabs/lacinia {:mvn/version "0.38.0"}
(require '[com.walmartlabs.lacinia.schema :as schema]
         '[com.walmartlabs.lacinia.util :as util]
         '[com.walmartlabs.lacinia :as lacinia])

(let [schema (-> '{:queries
                   {:cars {:type :CarCollection
                           :args {:filter {:type :CarCollectionFilter}}
                           :resolve :get-cars}}
                   
                   :input-objects
                   {:CarCollectionFilter
                    {:fields {:or {:type (list :CarCollectionFilter)}
                              :color {:type :StringFilter}}}
                    
                    :StringFilter
                    {:fields {:equals {:type String}}}}
                   
                   :objects
                   {:Car
                    {:fields {:color {:type String}}}
                    
                    :CarCollection
                    {:fields {:nodes {:type (list :Car)}}}}}
                 (util/attach-resolvers {:get-cars (fn [context args value]
                                                     {:nodes []})})
                 (schema/compile))
      query "query($color: String) {
               cars(filter: {or: [{color: {equals: \"blue\"}},
                                  {color: {equals: $color}}]}) {
                 nodes {
                   color
                 }
               }
             }"
      args {:color "red"}]
  (lacinia/execute schema query args nil))

Expected result

{:data #ordered/map ([:cars #ordered/map ([:nodes ()])])}

Actual result

{:errors [{:message "java.lang.NullPointerException"}]}

Nice write up; this is on my radar.

I'm looking into this; seems like a dynamic arguments extractor is ending up as nil when it shouldn't; might be related to the heavy recursion in your input fields, but it should still work.

Taking a step away helps and the solution was pretty obvious once I looked into it carefully.