Fork me on GitHub
#graphql
<
2017-12-13
>
guy06:12:17

thanks a bunch hlship!

PB22:12:54

Hey all... I'm an idiot. I've been toying around with https://github.com/walmartlabs/clojure-game-geek I've been trying to use curl to do the queries but I'm tripping over the simplest ones. I can't figure out what I'm doing wrong:

PB22:12:02

curl --request POST \
  --url  \
  --header 'content-type: application/graphql' \
  --data '{"query":"query { game_by_id(id: \"1237\") { name designers { name }}}"}'
{"errors":[{"message":"Failed to parse GraphQL query.","errors":[{"location":{"line":1,"column":null},"parse-error":"mismatched input '\"query\"' expecting {'query', 'mutation', 'subscription', '...', NameId}"}]}]}%

admay23:12:30

@petr give this a go, curl -XPOST localhost:8888/graphql -H 'Content-Type:application/graphql' -d '{game_by_id(id: "1237") {name designers {name}}}'

PB23:12:02

@admay THanks for the reply! I seem to have gotten it working with this:

curl --request POST \
  --url  \
  --header 'content-type: application/json' \
  --data '{"query":"query { game_by_id(id: \"1237\") { name designers { name }}}"}'
So the difference is that the graphql content type automagically adds the "query" part of the request?

admay23:12:53

I have no idea lol

admay23:12:21

To be honest, I only test using the q function from the user ns that the Lacinia team so graciously provided for us, https://github.com/walmartlabs/clojure-game-geek/blob/master/dev-resources/user.clj#L29

PB23:12:42

Fair enough. Did you get the query string using that q fn?

admay23:12:08

Nope, just kind of lucked out. I tried it without any headers to see what would happen and I got an error telling me in so many words, “You have to have a content type of json or graphql”. So I tried graphql and it worked!

PB23:12:20

I'm a little confused, looking at this project, how I would write logic for handling requests

PB23:12:27

I.e. if I wanted to transform data

PB23:12:46

Where does that go? This project seems to magically pass through to the db layer

admay23:12:17

I’m not sure I understand your question. Are you wondering how to write mutations? Or are you wondering how Lacinia handles the request?

PB23:12:55

SO mutations is another question, but not this one I.e. Say I wanted to do more than just pull data out of a DB. Say I wanted an endpoint that rather than made mutations, or got data, it instead called a function that kicked off an asynchronous process

admay23:12:17

Ah, okay, so you want to work with the resolvers a bit then, correct? What I mean by that is, you’re looking for a resolver that instead of just reading something from a data store would send an HTTP request or something like that

admay23:12:48

Yeah, if you look in that file, you’ll see something like this,

(defn resolver-map
  [component]
  (let [db (:db component)]
    {:query/game-by-id (game-by-id db)
     :query/member-by-id (member-by-id db)
     :BoardGame/designers (board-game-designers db)
     :BoardGame/rating-summary (rating-summary db)
     :GameRating/game (game-rating->game db)
     :Designer/games (designer-games db)
     :Member/ratings (member-ratings db)}))
That’s your resolver map.

admay23:12:14

That map is the go-between for the schema and the actual code.

admay23:12:47

So if you want another query to send an http request and return a success or fail status, you’ll need a few things. In the context of the clojure-game-geek code, you will need: 1. A status object in the schema 2. A query object in the schema with a resolver 3. A resolver function in the schema.clj namespace 4. A mapping in the resolver-map to connect the query object from the schema to the code in the schema namespace

admay23:12:18

I have to hop off for a bit for a meet up, keep posting questions and I’ll answer them when I’m back online as soon as I can!

PB23:12:34

Ok that makes sense. So then how do I call one of the non-queries?

PB23:12:56

I.e. (board-game-designers db)

admay23:12:39

(defn resolver-map
  [component]
  (let [db (:db component)]
    {:query/game-by-id (game-by-id db)
     :query/member-by-id (member-by-id db)
     :BoardGame/designers (board-game-designers db)
     :BoardGame/rating-summary (rating-summary db)
     :GameRating/game (game-rating->game db)
     :Designer/games (designer-games db)
     :Member/ratings (member-ratings db)
     :Request/make-request (my-request-function ... )}))
Once you’ve created all your schema objects and add the mapping, Lacinia knows what to do

PB23:12:41

So... like this?`BoardGame { board-game-designers }`?

PB23:12:13

Also it doesn't allow me to change these fns without restarting the server whcih is a little frustrating

PB23:12:43

Is it common with graphql to throw exceptions such as 400s, 404s etc?

PB23:12:57

It also appears that status codes aren't a thing?