tendant / graphql-clj

A Clojure library that provides GraphQL implementation.
Eclipse Public License 1.0
285 stars 22 forks source link

Passing variables with keyword keys doesn't work #46

Closed aew closed 7 years ago

aew commented 7 years ago

In Clojure, keyword keys would be more idiomatic than string keys in a map. graphql-clj seems to prefer string keys, and keyword keys on not consistently supported.

tendant commented 7 years ago

I prefer keyword keys in map as well. However there it is no common naming convention between GraphQL and Clojure.

GraphQL uses UpperCamelCase for Type and lowerCamelCase for field and argument.

I like to use :dash-separated-lowercase-word as keyword in Clojure.

Don't know what is the good way to convert them back and forth. So I left it open.

I am fine to switch over to use keyword and make it consistent in graphql-clj.

carld commented 7 years ago

I discovered this as well, for the life of me I could not understand why my GraphQL queries were not working with query variables. It looks like compojure would parse the request query variables into keyword keys.

In the CIDER repl I found could get the expected query result using string keywords, for example,

bk1.core> (bk1.graphql/execute q {:id 1})
"Warning: the result of graphql-clj.validator/validate-statement should be passed to execute instead of a statement string"
{:data {"account" nil}}
bk1.core> (bk1.graphql/execute q {"id" 1})
"Warning: the result of graphql-clj.validator/validate-statement should be passed to execute instead of a statement string"
{:data {"account" {"id" 1, "name" "Test One"}}}
bk1.core>

So in the ring handler I used clojure.walk/stringify-keys on the variables from the request.

(defroutes routes
  (POST "/graphql" [schema query variables :as request]
        (response/response
         (try
           (graphql/execute query (clojure.walk/stringify-keys variables))
           (catch Throwable e
             (println e))))))
tendant commented 7 years ago

Thanks @carld. Added clojure.walk/stringify-keys in executor, so it will accept keyword key as variable name.