Skip to content

Instantly share code, notes, and snippets.

@clonekim
Last active November 12, 2021 04:14
Show Gist options
  • Save clonekim/8425a849d55e543209ce4f597ae40a0a to your computer and use it in GitHub Desktop.
Save clonekim/8425a849d55e543209ce4f597ae40a0a to your computer and use it in GitHub Desktop.
Lacinia Setup
(defmethod ig/init-key :http/graphql [_ {:keys [schema]}]
(do
(log/info "initializing schema")
(graphql/init-schema schema)))
(defmethod ig/init-key :http/server [_ {:keys [handler port]}]
(let [server (http/run-server handler {:port port})]
(log/info "Starting HTTP server on port" port)
server))
(defmethod ig/halt-key! :http/server [_ server]
(do
(server :timeout 100)
(log/info "HTTP server stopped")))
(defmethod ig/init-key :handler/app [_ {:keys [schema]}]
(let [app (routes
(GET "/" [] "Hello World!")
(POST "/graphql" req
(let [query (:query
(json/read-str (slurp (:body req)) :key-fn keyword))]
(log/debug "Query ->" query)
{:status 200
:headers {"Content-Type" "application/json"}
:body (json/write-str (execute schema query nil nil))}))
(route/not-found "404 Not Found"))]
(wrap-defaults
(-> app
wrap-internal-error)
(-> site-defaults
(assoc-in [:security :anti-forgery] false)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment