0

I'm just trying to figure out GraphQL in Clojure using Lacinia with a simple "wiki" app.

The app. has Pages which are sequences of Cards. And the API can return either a raw text version of the page, or a sequence of Card objects.

Here's my schema defined in EDN


{:enums
 {:cardtype
  {:description "A type that is attached to each card"
   :values [:RAW :MARKDOWN :CALCULATED]}}

 :objects
 {:Card
  {:description "A card within the system"
   :fields
   {}}
  :RawPage
  {:description "A page always has a representation whose body is nothing but a string of plaintext"
   :fields
   {:page_name {:type String}
    :body {:type String}}}
  :Page
  {:description "A Page is a named page within the wiki. It is made of a series of cards"
   :fields
   {:page_name {:type String}
    :cards {:type (list :Card)}} }}


 :queries
 {:raw_page
  {:type :RawPage
   :description "Retrieve the raw version of a single page by its name"
   :args {:page_name {:type (non-null String)}}
   :resolve :resolve-raw-page}

  :cooked_page
  {:type :Page
   :description "Retrieve the cooked version of a single page by its name"
   :args {:page_name {:type (non-null String)}}
   :resolve :resolve-cooked-page}
  }
 }

And I'm trying to test it by firing this query up from GraphiQL

  {
    raw_page(page_name: "HelloWorld")
    page_name
    body
  }

The handler is


(defn graphql-handler [request]
  {:status 200
   :headers {"Content-Type" "application/json"}
   :body (let [query (extract-query request)
               result (execute pagestore/pagestore-schema query nil nil)]
           (json/write-str result))})

And the resolver for the raw_page query I'm asking is :

(defn resolve-raw-page [context arguments value]
  (let [{:keys [page_name]} arguments]
    {:page_name page_name
     :body (get-page-from-file page_name)}))

And I'm getting this error message :

{
  "errors": [
    {
      "message": "Cannot query field `page_name' on type `QueryRoot'.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "extensions": {
        "type": "QueryRoot",
        "field": "page_name"
      }
    }
  ]
}

This is probably a really simple n00b mistake. Either with the schema or my query. But I can't see what's wrong.

I take it QueryRoot is the root of the query. But is this something I have to explicitly navigate down from somewhere?

interstar
  • 26,048
  • 36
  • 112
  • 180

1 Answers1

1

OK.

After banging my head for a few hours, I finally figured it out.

It was a missing pair of curly-brackets after the query argument and around the fields.

Query should have been :

  {
    raw_page(page_name: "HelloWorld") {
      page_name
      body
    }
  }
interstar
  • 26,048
  • 36
  • 112
  • 180