Created
May 21, 2020 07:09
-
-
Save stevebuik/e289d6783b43639726cbc39357728bbf to your computer and use it in GitHub Desktop.
lacinia optional string params in mutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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