Skip to content

Commit

Permalink
First commit, extracted from BandSquare's internal code
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Waeselynck committed Mar 27, 2018
0 parents commit 2301de6
Show file tree
Hide file tree
Showing 11 changed files with 849 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/

*.iml
.idea
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## [Unreleased]

## [0.0.1-alpha] - 2018-03-27
### Added
- Basic implementation and tests based on BandSquare's internal code, supports asynchronous and tabular resolvers.

[Unreleased]: https://github.com/your-name/d2q/compare/0.1.1-alpha...HEAD
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 BandSquare and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# d2q

A Clojure library which provides a generic, expressive, efficient way of implementing query servers for demand-driven languages such as GraphQL.

**Project status:** less than alpha, open-sourced to allow for development outside of the company. Do not use yet, as major breaking changes will occur soon.

## Usage

TBD

## License

Copyright © 2018 BandSquare and contributors

Distributed under the MIT license.
3 changes: 3 additions & 0 deletions doc/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction to d2q

TODO: write [great documentation](http://jacobian.org/writing/what-to-write/)
11 changes: 11 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(defproject bandsquare/d2q "0.0.1-SNAPSHOT"
:description "An expressive toolkit for building efficient GraphQL-like query servers"
:url "https://github.com/bandsquare/d2q"
:license {:name "MIT License"
:url "https://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.8.0"]
[manifold "0.1.6"]]
:profiles
{:dev
{:dependencies
[[midje "1.7.0"]]}})
95 changes: 95 additions & 0 deletions src/d2q/api.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
(ns d2q.api
"Demand-driven querying à la GraphQL / Datomic Pull"
(:require [clojure.walk]
[manifold.deferred :as mfd]
[d2q.impl.utils :as impl.utils]
[d2q.impl]))

;; TODO improvements (Val, 15 Mar 2018)
;; 1. √ Remove specific authorization model - replace by a query-level resolution step
;; 2. Subquery preview in resolvers? May require to change the format of resolvers.
;; 3. Partial errors => resolvers return a map with {:data :errors} key
;; 4. Maybe skip the normalization of query - should be done by the caller
;; 5. Mutual recursion (requires to change the query format)
;; 6. Source mapping - keep track of (reversed) query-path and data-path
;; 7. Maybe faster processing via records ?

;; IMPROVEMENT maybe we can let the client express than some field is required etc. (Val, 30 May 2017)
(defn normalize-query-field
[f]
(as-> f f
(if (map? f)
f
{:d2q.fcall/field f})

(update f
:d2q.fcall/key
#(or % (:d2q.fcall/field f)))

(if-let [nested (:d2q.fcall/nested f)]
(assoc f :d2q.fcall/nested (into [] (map normalize-query-field) nested))
f)))

(defn normalize-query
[pull-spec]
(into [] (map normalize-query-field) pull-spec))

(defn query-engine
"Compiles tabular field resolvers + fields specs + entity-transformation fn to a function for computing demand-driven queries.
The returned function has signature [qctx, q, obj] -> Deferred<query-result>"
[tabular-resolvers
fields
transform-entities-fn ;; TODO find a better name for this step (Val, 17 Mar 2018)
]
(let [eng (d2q.impl/engine tabular-resolvers fields transform-entities-fn)]
(fn query [qctx q obj]
(let [query (normalize-query q)]
(d2q.impl/query eng qctx query obj)))))

;; ------------------------------------------------------------------------------
;; Generalization of the Fields-resolver model

(defn tabular-resolver-from-field-resolvers
[tr-name fields]
{:d2q.resolver/name tr-name
:d2q.resolver/compute
(let [frs-by-field-name (impl.utils/index-and-map-by
:d2q.field/name
:bs.d2q.field/compute
fields)]
(fn resolve-table [qctx f+args o+is]
(mfd/future
(let [fs (->> f+args
(mapv (fn [[k field-name args]]
(let [compute-fn (or
(frs-by-field-name field-name)
(throw (ex-info
(str "No field with name " (pr-str field-name)
" is supported by tabular resolver " (pr-str tr-name))
{:d2q.fcall/field field-name
:d2q.fcall/args args
:d2q.resolver/name tr-name})))]
[k compute-fn args field-name]))))]
(into []
(remove nil?)
(for [[obj i] o+is
[k compute-fn args field-name] fs]
(let [v (try
;; TODO use deps argument ? (Val, 16 Nov 2017)
(compute-fn qctx obj nil args)
(catch Throwable err
(throw
(ex-info
(str
"Field Resolver for key " (pr-str (:d2q.fcall/key k))
" failed with " (pr-str (type err))
" : " (.getMessage err))
(merge
{:q-field {:d2q.fcall/field field-name
:d2q.fcall/key k
:d2q.fcall/args args}
:d2q.error/type :d2q.error.type/field-resolver-failed-to-compute}
(ex-data err))
err))))]
(when (some? v)
[i k v]))))))))})
Loading

0 comments on commit 2301de6

Please sign in to comment.