core.clj 2.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
(ns lacinia-datomic-pull.core
  (:require [clojure.edn :as edn]
            [com.walmartlabs.lacinia :refer [execute]]
            [com.walmartlabs.lacinia.util :refer [attach-resolvers]]
            [com.walmartlabs.lacinia.schema :as schema]
            [com.walmartlabs.datascope :as ds]
            [lacinia-datomic-pull.pattern :as pattern]
            [lacinia-datomic-pull.lift-enums :as enums]
            [datomic.api :as d]
            [clojure.pprint :refer [pprint]]
            [dato-schema.core]
            [clojure.java.io :as io])
  (:import (java.util UUID)))

(defn create-empty-in-memory-db []
  (let [uri "datomic:mem://woof"]
    (d/delete-database uri)
    (d/create-database uri)
    (let [conn (d/connect uri)]
      conn)))

(def conn (create-empty-in-memory-db))

(def schema
  #d/schema[[:human/id :one :uuid :unique]
            [:human/name :one :string]
            [:human/age :one :long]
            [:human/address :one :ref :component]
            [:human/mood :one :ref]
            [:human/moods :many :ref]
            [:human/liste :many :ref]
            [:human/visited_countries :many :ref]
            [:mood/good :enum]
            [:mood/bad :enum]
            [:address/street :one :string]
            [:address/city :one :string]
            [:address/country :one :ref]
            [:country/id :one :string :unique]
            [:country/name :one :string]])

@(d/transact conn schema)

(def human-id #uuid"5bbb36ea-4e3a-4e21-a7d6-bed3450a5d34")

@(d/transact conn [{:db/id        "NO"
                    :country/id   "no"
                    :country/name "Noreg"}
                   {:db/id        "SE"
                    :country/id   "SE"
                    :country/name "Sverige"}
                   {:human/id                human-id
                    :human/name              "Ola"
                    :human/age               40
                    :human/mood              :mood/good
                    :human/moods             #{:mood/good :mood/bad}
                    :human/visited_countries #{"NO" "SE"}
                    :human/address           {:address/street  "Asdf"
                                              :address/city    "Bergen"
                                              :address/country "NO"}}])

(defn get-human [context arguments value]
  (let [pattern (pattern/context->pull-pattern context)
        ent (d/pull (d/db conn)
                    pattern
                    [:human/id (UUID/fromString (:id arguments))])]
    (println "pull pattern is:")
    (pprint pattern)
    (enums/lift-enums context ent)))

(def star-wars-schema
  (-> "schema.edn"
      io/resource
      slurp
      edn/read-string
      (attach-resolvers {:human get-human})
      schema/compile))

(pprint (execute star-wars-schema
                 "query { human(id: \"5bbb36ea-4e3a-4e21-a7d6-bed3450a5d34\") {
id
name
mood
moods
visited_countries {
  name
}
address {
  street
  city
  country {
    id
    name
  }
}
}}"
                 nil nil))