Skip to content

Commit

Permalink
Extract ubiq as a lib and erp as an app, logging is still an issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shivekkhurana committed May 8, 2019
1 parent 41c12f4 commit 75954ff
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pom.xml.asc
.hgignore
.hg/
.cpcache
resources/dev.db
resources/**/dev.db
log/
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

Ubiq is an experimental way to build GraphQL apis with minimal code.

### Introduction

The central idea is to define all resolvers as a series of interceptors. These interceptors are invoked in order, can pass data down the next interceptor and can exit in case of errors. All interceptors are defined statically in `resolvers.edn` file.

Ubiq also provides components that help you tie GraphQL queries and mutations directly to SQL statements via HugSQL.
Ubiq also provides components that help you tie GraphQL queries and mutations directly to SQL statements via HugSQL and an authentication mechanism to speed up development of new projects.


### Getting started
Expand Down
12 changes: 6 additions & 6 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
[integrant.core :as ig]
[integrant.repl :refer [clear go halt prep init reset reset-all]]
[aero.core :refer [read-config reader]]
[components.graphql-server]
[components.migrator]
[components.domain]
[components.seeder]
[components.resolver]))
[ubiq.components.graphql-server]
[ubiq.components.migrator]
[ubiq.components.domain]
[ubiq.components.seeder]
[ubiq.components.resolver]))


(defmethod reader 'ig/ref [_ _ value]
(ig/ref value))

(def config
(read-config (io/resource "config.edn") {:profile :dev}))
(read-config (io/resource "erp_app/config.edn") {:profile :dev}))

(integrant.repl/set-prep! (constantly (:system config)))

Expand Down
23 changes: 0 additions & 23 deletions resources/config.edn

This file was deleted.

28 changes: 28 additions & 0 deletions resources/erp_app/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{:app-folder "erp_app"
:sqlite-config
#profile
{:dev {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname #join ["resources/" #ref [:app-folder] "/dev.db"]}}

:seed-data?
#profile
{:default false
:dev true}

:system
{:graphql-server {:enable-graphiql? true
:port 8000
:resolver #ig/ref :resolver
:domain #ig/ref :domain
:schema-resource #join [#ref [:app-folder] "/schema.edn"]}
:migrator {:db #ref [:sqlite-config]
:migrations-folder #join [#ref [:app-folder] "/migrations"]}
:resolver {:domain #ig/ref :domain
:resolver-config-resource #join [#ref [:app-folder] "/resolvers.edn"]}
:domain {:db #ref [:sqlite-config]
:sql-folder #join ["src/" #ref [:app-folder] "/sql"]}
:seeder {:seed-count {:parties 10}
:seed-data? #ref [:seed-data?]
:domain #ig/ref :domain
:migrator #ig/ref :migrator}}}
File renamed without changes.
File renamed without changes.
58 changes: 0 additions & 58 deletions resources/logback.xml

This file was deleted.

35 changes: 0 additions & 35 deletions src/components/domain.clj

This file was deleted.

File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions src/ubiq/components/domain.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns ubiq.components.domain
(:require [clojure.java.io :as io]
[clojure.string :as str]
[hugsql.core :as hugsql]
[integrant.core :as ig]))

(defn- get-db-fns-map [file-path-dot-sql]
(hugsql/map-of-db-fns file-path-dot-sql))

(defn- read-file-tree
"Given a directory path, recursively read all files
and return a vector containing path of all files."
[path]
(->> path
io/file
file-seq
(filter #(.isFile %))
(map #(.getPath %))))

(defn- inject-db [db db-fns-map]
(into {} (map (fn [[k v]]
[k (partial (:fn v) db)])
db-fns-map)))

(defn- path->file [p]
(last (str/split p #"/")))

(defmethod ig/init-key :domain [_ {:keys [db sql-folder]}]
(let [sql-file-paths (read-file-tree sql-folder) ; read all files in src/sql folder returns ("src/erp_app/sql/products.sql" "src/erp_app/sql/parties.sql")
sql-files (map path->file sql-file-paths) ; figure out the name of sql file (sql folder cannot have sub folders with this setup)
sql-folder-without-src (str/replace sql-folder #"src/" "")]
(into {} (map (fn [file-name-dot-sql]
[(keyword (str/replace file-name-dot-sql #".sql" ""))
(inject-db db (get-db-fns-map (str sql-folder-without-src "/" file-name-dot-sql)))])
sql-files))))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns components.graphql-server
(ns ubiq.components.graphql-server
(:require [clojure.java.io :as io]
[clojure.edn :as edn]
[integrant.core :as ig]
Expand All @@ -7,8 +7,8 @@
[com.walmartlabs.lacinia.schema :as schema]
[io.pedestal.http :as http]))

(defmethod ig/init-key :graphql-server [_ {:keys [enable-graphiql? port resolver domain]}]
(-> "schema.edn"
(defmethod ig/init-key :graphql-server [_ {:keys [enable-graphiql? port resolver domain schema-resource]}]
(-> schema-resource
io/resource
slurp
edn/read-string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns components.migrator
(ns ubiq.components.migrator
(:require [ragtime.jdbc :as jdbc]
[ragtime.repl :as repl]
[integrant.core :as ig]))

(defmethod ig/init-key :migrator [_ {:keys [db]}]
(defmethod ig/init-key :migrator [_ {:keys [db migrations-folder]}]
(let [config {:datastore (jdbc/sql-database db)
:migrations (jdbc/load-resources "migrations")}]
:migrations (jdbc/load-resources migrations-folder)}]
(repl/migrate config)
config))

Expand Down
16 changes: 8 additions & 8 deletions src/components/resolver.clj → src/ubiq/components/resolver.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns components.resolver
(ns ubiq.components.resolver
(:require [clojure.java.io :as io]
[clojure.edn :as edn]
[integrant.core :as ig]
[intercepts.auth :as auth]
[intercepts.domain :as domain]))
[ubiq.intercepts.auth :as auth]
[ubiq.intercepts.domain :as domain]))

(defn- handle-exit-wrapper [{:keys [i-fn i-args]} ctx]
(let [ctx-with-i-args (assoc-in ctx [:interceptor-args] i-args)]
Expand All @@ -14,7 +14,7 @@
(defn- resolve-interceptor-fn-symbol [s]
;; assuming that all intercepts will be under intercepts. namespace
;; and will be imported in this domain! (Can improve DX here by checking that ns exists)
(resolve (symbol (str "intercepts." (namespace s) "/" (name s)))))
(resolve (symbol (str "ubiq.intercepts." (namespace s) "/" (name s)))))

(defn- intercept->interceptor-fn [intercept]
;; allow for different kinds of interceptor config and normalise it here
Expand Down Expand Up @@ -46,14 +46,14 @@
:args args
:value value}))))

(defn get-resolvers-config []
(-> "resolvers.edn"
(defn get-resolvers-config [resolver-config-resource]
(-> resolver-config-resource
io/resource
slurp
edn/read-string))

(defmethod ig/init-key :resolver [_ {:keys []}]
(let [resolvers-config (get-resolvers-config)]
(defmethod ig/init-key :resolver [_ {:keys [resolver-config-resource]}]
(let [resolvers-config (get-resolvers-config resolver-config-resource)]
(zipmap
(keys resolvers-config)
(map compile-intercepts (vals resolvers-config)))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns components.seeder
(ns ubiq.components.seeder
(:require [clojure.tools.logging :as log]
[integrant.core :as ig]
[hugsql.core :as hugsql]
Expand Down
2 changes: 1 addition & 1 deletion src/intercepts/auth.clj → src/ubiq/intercepts/auth.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns intercepts.auth)
(ns ubiq.intercepts.auth)

(defn- is-logged-in? [_]
true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns intercepts.domain)
(ns ubiq.intercepts.domain)

(defn executor [{:keys [lacinia-ctx args value interceptor-args] :as ctx}]
(let [domain (:domain lacinia-ctx)
Expand Down

0 comments on commit 75954ff

Please sign in to comment.