Skip to content

Instantly share code, notes, and snippets.

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 stevebuik/e289d6783b43639726cbc39357728bbf to your computer and use it in GitHub Desktop.
Save stevebuik/e289d6783b43639726cbc39357728bbf to your computer and use it in GitHub Desktop.
lacinia optional string params in mutations
(ns tools.graphql-fiddle
(:require [clojure.pprint :refer [pprint]]
[com.walmartlabs.lacinia.schema :as schema]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[com.walmartlabs.lacinia :as lacinia]))
(def definition
{:objects {:person {:fields {:name '{:type String}
:nicknames '{:type (list String)}}}
:saveResult {:fields {:status '{:type String}}}}
:queries {:people {:type :person
:resolve :person}}
:mutations {:savePerson {:type :saveResult
:args {:name '{:type (non-null String)}
:nicknames '{:type (list String)}}
:resolve :save-person}}})
(defn resolve-person
[ctx args v]
{:name "john"
:nicknames ["Jonnie" "Big John"]})
(defn save-person
[ctx args v]
{:status "saved"})
(def resolvers
{:person resolve-person
:save-person save-person})
(def schema
(-> definition
(attach-resolvers resolvers)
(schema/compile {})))
(defn execute
[gql vars]
(let [ctx {::loader (atom {:pending #{} :cache {}})}]
(lacinia/execute schema gql vars ctx)))
(comment
(pprint
(execute
"query {
people {
name nicknames
}
}"
{}))
(pprint
(execute
"mutation savePerson($name: String!, $nicknames: [String!]) {
savePerson(name: $name, nicknames: $nicknames) {
status
}
}"
{:name "Pete"
:nicknames nil}))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment