Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. (def my-games {
  2. "game1" {:key "gamey-1" :name "hello-game" :created 2017 :sub-game ["foo1" "bar2"]}
  3. "game2" {:key "gamey-2" :name "goodbye-game" :created 2019 :sub-game ["bar3" "baz8"]}
  4. })
  5.  
  6. (def my-sub-games {
  7. "foo1" {:name "foo game 1"}
  8. "bar2" {:name "bar game 2"}
  9. "bar3" {:name "bar game 3"}
  10. "baz8" {:name "baz game 8"}
  11. })
  12.  
  13.  
  14. (def testy-schema
  15. '{:objects
  16. {:Sub-Games {:description "a sub game check of nested resolvers"
  17. :fields {:name {:type (non-null String)}}}
  18.  
  19. :Game {:description "A game object"
  20. :fields {:key {:type (non-null String)
  21. :description "Unique identifier for this game"}
  22. :created {:type Int}
  23. :name {:type (non-null String)}
  24. :subgame {:type (list :Sub-Games)
  25. :resolve :sub-game-resolver}}}}
  26. :queries
  27. {:game {:type :Game
  28. :description "Retrieve a single Game by its name"
  29. :args {:name {:type (non-null String)
  30. :description "Unique name for game."}}
  31. :resolve :game-resolver}}})
  32.  
  33. (defn game-resolver-func [app-context-map field-args outer-field-resolved-val]
  34. ;; in this case outer-field-resolved-val is nil
  35. (get my-games (:name field-args)))
  36.  
  37. (defn sub-game-resolver-func [app-context-map field-args outer-field-resolved-val]
  38. ;; here outer-field-resolved-val is the map returned from game-resolve-func
  39. (map #(get my-sub-games %) (:sub-game cont-field-resolved-val)))
  40.  
  41. (defn compiled-schema
  42. []
  43. (-> testy-schema
  44. (attach-resolvers {:game-resolver game-resolver-func
  45. :sub-game-resolver sub-game-resolver-func})
  46. schema/compile))
  47.  
  48.  
  49. ;;query
  50. { game(name: "game1") {
  51. key
  52. created
  53. subgame {
  54. name
  55. }
  56. }
  57. }
  58.  
  59. ;; output
  60. {:data #ordered/map ([:game #ordered/map
  61. ([:key "gamey-1"]
  62. [:created 2017]
  63. [:subgame (#ordered/map
  64. ([:name "foo game 1"])
  65. #ordered/map
  66. ([:name "bar game 2"]))])])}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement