Skip to content

Commit

Permalink
Merge pull request #50 from benjamin-asdf/specs-from-metadata
Browse files Browse the repository at this point in the history
Add query and mutation specs from resolver-fn metadata #4
  • Loading branch information
acron0 authored Feb 8, 2023
2 parents c936738 + d25e764 commit 2b7cedc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ To add a query to the schema use `attach-query`:
```
`::query-spec` is a spec for the GraphQL query, `::object` is the spec for the returned data, and `query-resolver-fn` is the resolver function that will fetch and return the data.

Alternatively, your `query-resolver-fn` has `:leona/query-spec` and `:leona/results-spec` metadata:

```clojure
(defn
^{:leona/query-spec ::query-spec
:leona/results-spec ::object}
query-resolver-fn [])

(-> (leona/create)
(leona/attach-query query-resolver-fn))
```

### Mutations

Mutations are very similar to queries. To add a mutation to the schema use `attach-mutation`:
Expand All @@ -50,6 +62,18 @@ Mutations are very similar to queries. To add a mutation to the schema use `atta
```
`::mutation-spec` is a spec for the GraphQL mutation, `::object` is the spec for the returned data, and `mutator-fn` is the function that will mutate the existing data and return the new, mutated data.

Alternatively, your `mutator-fn` has `:leona/mutation-spec` and `:leona/results-spec` metadata:

```clojure
(defn
^{:leona/mutation-spec ::mutation-spec
:leona/results-spec ::object}
mutator-fn [])

(-> (leona/create)
(leona/attach-query mutator-fn))
```

### Field Resolvers

To provide a resolver for a specific field, use `attach-field-resolver`:
Expand Down
26 changes: 18 additions & 8 deletions src/leona/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,15 @@
input? (update :input-objects conj object-spec)))

(defn attach-query
"Adds a query resolver into the provided pre-compiled data structure"
#_([m resolver]
;; TODO infer specs from fdef
)
"Adds a query resolver into the provided pre-compiled data structure.
In the form [m resolver] I expect `:leona/query-spec` and `:leona/results-spec`
as metadata on `resolver`.
Specs should be keywords, see [[::query-spec]]."
([m resolver]
(let [{:leona/keys [query-spec results-spec]} (meta resolver)
_ (assert query-spec)
_ (assert results-spec)]
(attach-query m query-spec results-spec resolver)))
([m query-spec results-spec resolver]
{:pre [(s/valid? ::pre-compiled-data m)]}
(-> m
Expand All @@ -174,10 +179,15 @@
:query-spec query-spec}))))

(defn attach-mutation
"Adds a mutation resolver fn into the provided pre-compiled data structure"
#_([m resolver]
;; TODO infer specs from fdef
)
"Adds a mutation resolver fn into the provided pre-compiled data structure
In the form [m resolver] I expect `:leona/mutation-spec` and `:leona/results-spec`
as metadata on `resolver`.
Specs should be keywords, see [[::mutation-spec]]."
([m resolver]
(let [{:leona/keys [mutation-spec results-spec]} (meta resolver)
_ (assert mutation-spec)
_ (assert results-spec)]
(attach-mutation m mutation-spec results-spec resolver)))
([m mutation-spec results-spec resolver]
{:pre [(s/valid? ::pre-compiled-data m)]}
(-> m
Expand Down
37 changes: 37 additions & 0 deletions test/leona/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,40 @@
(leona/compile))
r (leona/execute schema "{ myQuery { result } }")]
(is (= "hello" (get-in r [:data :myQuery :result])))))

;; https://github.com/WorksHub/leona/issues/4 #4
(deftest provide-input-spec-from-metadata
(let [droid-resolver (fn [ctx query value])
droid-resolver-with-meta
(with-meta droid-resolver
{:leona/query-spec ::test/droid-query
:leona/results-spec ::test/droid})
droid-mutator (fn [ctx query value])
droid-mutator-with-meta
(with-meta droid-mutator
{:leona/mutation-spec ::test/droid-mutation
:leona/results-spec ::test/droid})
human-resolver (fn [ctx query value])
middleware (fn [handler ctx query value])
precompiled-schema-with-args
(-> (leona/create)
(leona/attach-query ::test/droid-query ::test/droid droid-resolver)
(leona/attach-mutation ::test/droid-mutation ::test/droid droid-mutator)
(leona/attach-field-resolver ::test/owner human-resolver)
(leona/attach-middleware middleware))
precompiled-schema-with-metadata
(-> (leona/create)
(leona/attach-query droid-resolver-with-meta)
(leona/attach-mutation droid-mutator-with-meta)
(leona/attach-field-resolver ::test/owner human-resolver)
(leona/attach-middleware middleware))]
(is
(= (-> precompiled-schema-with-args :mutations ::droid :mutation-spec)
(-> precompiled-schema-with-metadata :mutations ::droid :mutation-spec)))
(is
(= (-> precompiled-schema-with-args :queries ::droid :query-spec)
(-> precompiled-schema-with-metadata :queries ::droid :query-spec)))
(is
(= (-> precompiled-schema-with-args :specs)
(-> precompiled-schema-with-metadata :specs)))))

0 comments on commit 2b7cedc

Please sign in to comment.