Fork me on GitHub
#graphql
<
2022-09-29
>
localshred16:09:31

(update: problem solved. don't print the ctx to the repl or you'll be waiting a long time. le sigh) hey friends! using lacinia+ring and have noticed some serious lag time when trying to resolve more than a few dozen entities. If there's a few small entities to return results are snappy but it seems the more nodes I return the longer the wait that feels non-linear. I've profiled all relevant app code from ring through to the (single) resolver and determined that the lag is most likely within lacinia itself, so I'm trying to figure out how best to debug at this point. I'll add more relevant code/info in the thread. Thanks in advance for any help! I'm hoping I've just forgotten to enable the "run-fast" flag or something 😉

localshred16:09:47

Schema composition:

(defn- -load-schema!
  [{:keys [resolvers scalars schema-file]}]
  (-> (io/resource schema-file)
      (slurp)
      (edn/read-string)
      (util/attach-resolvers resolvers)
      (util/attach-scalar-transformers scalars)
      (build-and-inject-enum-serializers)
      (schema/compile {:disable-java-objects?  true
                       :default-field-resolver schema/hyphenating-default-field-resolver})))

localshred16:09:57

Ring to graphql exec and result simplification:

(defn simplify
  "Converts all ordered maps nested within the map into standard hash maps, and
  sequences into vectors, which makes for easier constants in the tests, and
  eliminates ordering problems."
  [m]
  (walk/postwalk
   (fn [node]
     (cond
       (instance? IPersistentMap node) (into {} node)
       (seq? node)                     (vec node)
       :else                           node))
   m))

(defn exec
  "Execute a graphql query"
  [schema {:keys [query variables] :as query-data} ctx]
  (log/timer :graphql/exec
             (spec/v! ::query-data query-data
                      (log/debug ::exec query-data)
                      (-> (lacinia/execute schema query variables ctx)
                          (simplify)))))

(defmethod routes/handler [:post ::routes/graphql]
  [{:keys  [body-params]
    ::keys [schema]
    :as    req}]
  (log/timer :graphql/routes-handler
             (let [ctx    (select-keys req [::db/conn :identity])
                   result (exec schema body-params ctx)]
               {:status (if (seq (:errors result)) 400 200)
                :body   result})))

localshred16:09:59

Gah. Apparently printing the ctx to the repl means you'll be waiting a very long time for your very quickly resolved results to be sent in the response. Lesson received

😢 1
localshred16:09:12

Leaving here for posterity's sake