boingo
boingo

Reputation: 1

Endpoint unit testing with Lacinia-Pedestal in Clojure

I have been using Pedestal for RESTful API servers and its endpoint unit testing. This approach is to set the server up and test it on endpoint level. The so-called "endpoint unit testing" is well documented in below page.

http://pedestal.io/reference/unit-testing#_testing_your_service_with_response_for

This time, however, I am using Lacinia-Pedestal for GraphQL (not RESTful, in other words) and am wondering if I can apply the same endpoint testing logic. In Lacinia-Pedestal repository (https://github.com/walmartlabs/lacinia-pedestal), I could not find a relevant instruction. In fact it doesn't mention about unit testing at all.

If anyone has experience with this approach, can you please share? Thanks!

-- edit I am adding my testing code here.

resources/main-schema.edn:

{:queries
 {:hello
  {:type String}}}

core.clj

(ns lacinia-pedestal-lein-clj.core
  (:require [clojure.edn :as edn]
            [clojure.java.io :as io]
            [com.walmartlabs.lacinia.pedestal2 :as p2]
            [com.walmartlabs.lacinia.schema :as schema]
            [com.walmartlabs.lacinia.util :as util]
            [io.pedestal.http :as http]))

(defn resolve-hello
  [_ _ _]
  "hello, darren")

(defn hello-schema
  []
  (-> (io/resource "main-schema.edn")
      slurp
      edn/read-string
      (util/inject-resolvers {:queries/hello resolve-hello})
      schema/compile))

(def service
  (-> (hello-schema)
      (p2/default-service nil)
      http/create-server
      http/start))

and Pedestal (not Lacinia-Pedestal) says that the server instance can be set up and tested by the following code snippet:

(ns lacinia-pedestal-lein-clj.core-test
  (:require [lacinia-pedestal-lein-clj.core :as core]
            [clojure.test :as t]
            [io.pedestal.http :as http]
            [io.pedestal.test :as ptest]
            [com.walmartlabs.lacinia.pedestal2 :as p2]
            [com.walmartlabs.lacinia.util :as util]))

(def service 
  (:io.pedestal.http/service-fn 
    (io.pedestal.http/create-servlet service-map)))

(is (= "Hello!" (:body (response-for service :get "/hello"))))

But, I believe this way works for RESTful but not for GraphQL because GraphQL needs to set schema (.edn file) and resolvers for a server.

So, I tried to tweak this.

(ns lacinia-pedestal-lein-clj.core-test
  (:require [lacinia-pedestal-lein-clj.core :as core]
            [clojure.test :as t]
            [io.pedestal.http :as http]
            [io.pedestal.test :as ptest]
            [com.walmartlabs.lacinia.pedestal2 :as p2]
            [com.walmartlabs.lacinia.util :as util]))

(defonce service
  (-> (core/hello-schema)
      (p2/default-service nil)
      http/create-server
      http/start))

(t/is (= "Hello!"
         (:body 
          (util/response-for service
                             :get "/hello")))) ;; NOT WORKING

But it does not work this way because response-for expects interceptor-service-fn type. So, as far as I know, the real question is how to use response-for function with GraphQL server instance.

Upvotes: 0

Views: 154

Answers (1)

boingo
boingo

Reputation: 1

in fact, i found a solution from Lacinia documentation. https://lacinia.readthedocs.io/en/latest/tutorial/testing-1.html

Upvotes: 0

Related Questions