Skip to content

Instantly share code, notes, and snippets.

@Zak-Kent
Last active June 27, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zak-Kent/8198b46ac85205bd0e9601d169fed87a to your computer and use it in GitHub Desktop.
Save Zak-Kent/8198b46ac85205bd0e9601d169fed87a to your computer and use it in GitHub Desktop.
(def my-games {
"game1" {:key "gamey-1" :name "hello-game" :created 2017 :sub-game ["foo1" "bar2"]}
"game2" {:key "gamey-2" :name "goodbye-game" :created 2019 :sub-game ["bar3" "baz8"]}
})
(def my-sub-games {
"foo1" {:name "foo game 1"}
"bar2" {:name "bar game 2"}
"bar3" {:name "bar game 3"}
"baz8" {:name "baz game 8"}
})
(def testy-schema
'{:objects
{:Sub-Games {:description "a sub game check of nested resolvers"
:fields {:name {:type (non-null String)}}}
:Game {:description "A game object"
:fields {:key {:type (non-null String)
:description "Unique identifier for this game"}
:created {:type Int}
:name {:type (non-null String)}
:subgame {:type (list :Sub-Games)
:resolve :sub-game-resolver}}}}
:queries
{:game {:type :Game
:description "Retrieve a single Game by its name"
:args {:name {:type (non-null String)
:description "Unique name for game."}}
:resolve :game-resolver}}})
(defn game-resolver-func [app-context-map field-args outer-field-resolved-val]
;; in this case outer-field-resolved-val is nil
(get my-games (:name field-args)))
(defn sub-game-resolver-func [app-context-map field-args outer-field-resolved-val]
;; here outer-field-resolved-val is the map returned from game-resolve-func
(map #(get my-sub-games %) (:sub-game cont-field-resolved-val)))
(defn compiled-schema
[]
(-> testy-schema
(attach-resolvers {:game-resolver game-resolver-func
:sub-game-resolver sub-game-resolver-func})
schema/compile))
;;query
{ game(name: "game1") {
key
created
subgame {
name
}
}
}
;; output
{:data #ordered/map ([:game #ordered/map
([:key "gamey-1"]
[:created 2017]
[:subgame (#ordered/map
([:name "foo game 1"])
#ordered/map
([:name "bar game 2"]))])])}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment