LCOV - code coverage report
Current view: top level - matchscore/api - graphql.clj Coverage Total Hit
Test: lcov.info Lines: 100.0 % 42 42
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.api.graphql
       2              :   (:require
       3              :    [clojure.string :as str]
       4              :    [com.walmartlabs.lacinia :as lacinia]
       5              :    [matchscore.graphql.schema :as schema]
       6              :    [matchscore.utils.otel :as otel]
       7              :    [superlifter.api :as s]
       8              :    [taoensso.telemere :as t]))
       9              : 
      10              : 
      11            4 : (defn- detect-operation-type
      12              :   [query]
      13           12 :   (when query
      14           24 :     (let [q (str/triml (str/lower-case query))]
      15           34 :       (cond (str/starts-with? q "mutation")     "mutation"
      16           16 :             (str/starts-with? q "subscription") "subscription"
      17              :             :else                               "query"))))
      18              : 
      19              : 
      20            8 : (def ^:private superlifter-interval-ms
      21              :   "How often queued batch loaders are flushed. Every nested GraphQL level waits
      22              :   for the next flush, so this is paid once per level of the query."
      23              :   5)
      24              : 
      25              : 
      26            4 : (def ^:private superlifter-config
      27           48 :   {:buckets {:default {:triggers {:queue-size {:threshold 100}
      28           12 :                                   :interval   {:interval superlifter-interval-ms}}}}})
      29              : 
      30              : 
      31            4 : (defn graphql-handler
      32              :  {:authz {:public? true}}
      33              :  [{:keys [body config repos auth-headers casbin ctx bot goose]}]
      34           16 :  (let [query          (:query body)
      35           12 :        variables      (:variables body)
      36           12 :        operation-name (:operationName body)]
      37           20 :    (if (nil? query)
      38            8 :      {:status 400
      39            6 :       :body   {:message "Missing 'query' in request body"}}
      40           28 :      (let [hot-reload?    (get-in config [:options :hot-reload-enabled?])
      41            8 :            sl-instance    (s/start!
      42           12 :                            (assoc-in superlifter-config
      43           12 :                             [:urania-opts :env]
      44           24 :                             {:repos         repos
      45           12 :                              :pg            (:pg ctx)
      46            4 :                              :config        config
      47           12 :                              :media-storage (:media-storage ctx)}))
      48           28 :            enforcer       (or (:enforcer ctx) (:enforcer casbin))
      49           48 :            context        {:repos         repos
      50            4 :                            :auth-headers  auth-headers
      51            4 :                            :superlifter   sl-instance
      52            4 :                            :casbin        casbin
      53            4 :                            :enforcer      enforcer
      54            4 :                            :bot           bot
      55            4 :                            :goose         goose
      56           12 :                            :pg            (:pg ctx)
      57            4 :                            :config        config
      58           12 :                            :media-storage (:media-storage ctx)}
      59           12 :            operation-type (detect-operation-type query)
      60           33 :            span-name      (str "graphql." (or operation-name "anonymous"))
      61              :            ;; Truncate the query body to keep span attributes small.
      62           44 :            query-snip     (when query (subs query 0 (min 1024 (count query))))]
      63            4 :        (try
      64            4 :          {:status 200
      65              :           :body   (otel/with-span
      66              :                    span-name
      67              :                    {:graphql.operation      operation-name
      68              :                     :graphql.operation.type operation-type
      69              :                     :graphql.document       query-snip
      70              :                     :graphql.variables.len  (count (or variables {}))}
      71              :                    (let [result (lacinia/execute
      72              :                                  (schema/get-schema hot-reload?)
      73              :                                  query
      74              :                                  variables
      75              :                                  context
      76              :                                  {:operation-name operation-name})]
      77              :                      (when-let [errors (seq (:errors result))]
      78              :                        (let [first-msg (str (get (first errors) :message (first errors)))]
      79              :                          (t/log!
      80              :                           :error
      81              :                           {:message   "GraphQL errors"
      82              :                            :errors    (mapv :message errors)
      83              :                            :operation operation-name
      84              :                            :query     query-snip})
      85              :                          (otel/add-attrs!
      86              :                           {:graphql.errors.count (count errors)
      87              :                            :graphql.error.first  (subs first-msg 0 (min 500 (count first-msg)))})))
      88              :                      result))}
      89           12 :          (finally (s/stop! sl-instance)))))))
        

Generated by: LCOV version 2.0-1