Fork me on GitHub
#graphql
<
2019-04-04
>
orestis11:04:47

So after writing a few graphql resolvers that do a whole bunch of destructuring of input objects, there seems to be an obvious type system - shaped hole in how the code looks: If the GraphQL schema changes (e.g. a field is renamed or moved around etc etc), the destructuring code doesn’t know.

orestis11:04:00

I wonder if there is a way to write a macro that does GraphQL aware destructuring — so in addition to having :keys it also knows the graphql input object that it destructures, so it can check (at compile time!) that all keys are present and correct.

3Jane11:04:27

(if the graphql schema changes, you’ve got bigger problems than destructuring code inside the server, b/c you just broke the api for your clients)

3Jane11:04:32

(Facebook’s way of resolving that is a “never remove, just add new things” though it can get unwieldy for a smaller company that doesn’t have to handle clients of all the versions)

orestis11:04:37

I’m my own client 🙂

3Jane11:04:41

convenient 😄

orestis11:04:26

I mean, sure, once you go public you need to be very careful about these things. But during development where the API is malleable and might get renamed as we get better at naming things…

3Jane11:04:37

then the problem converges to wanting an ORM, ie. “if I change my DB schema, my queries stop working”

orestis11:04:15

One step at a time 🙂 (or, we use MongoDB, what is that Schema you’re talking about?)

orestis11:04:08

Hey, we are sleeping in the gutter but we are at least looking at the stars 😛

orestis12:04:47

D E C E P T I O N 😄

3Jane12:04:25

because trust!

3Jane12:04:54

(I love how the guy is a new meme, but includes normal people as well as internet weirdos)

orestis13:04:21

Yeah — we got a new code with my wife from him 🙂

3Jane11:04:10

(fwiw I’m not a fan of multiplying layers, I just like having schema somewhere for reference 😄 )

orestis11:04:24

I’m overstating it, we do have a schema, but of course it relies on a lot of manual work to keep it up to date. So that’s why since we’re using GraphQL now I’d like to at least make sure we don’t have a second source of bugs.

orestis11:04:52

An example of what I mean:

(defn my-resolver [context args value]
 (let [{:keys [foo_id bar_id flag_present]} args]
   ...))

orestis12:04:19

All these keys correspond 1-1 to my graphql schema definition. I’d like to have some warm fuzzy feeling that the computer made sure these keys actually exist in that schema and I didn’t mistype anything.

domkm12:04:42

@orestis I don't know of a macro to do this but it wouldn't surprise me if someone had written one to introspect the keys of a map spec and destructure for that

domkm12:04:53

Alternatively, it kind of sounds like you want a combination of destructure+assert so as to fail fast if the data doesn't match up with the destructure keys. Destructuring in Clojure is now implemented with spec, so you might be able to borrow that spec and implement your own let. Maybe something like assert-let where it verifies that all destructured keys on the left side exist in data on the right side.

orestis12:04:01

Good pointer! However I’d like to validate against the GraphQL schema, not the data on the right side (as these might be nil/missing anyway since arguments can be optional).

domkm12:04:09

I would hope, but don't know, that Lacinia would ensure that every key and value are present in input objects and that missing ones are added with nil values. If that's the case, you could validate against the data directly because Lacinia would ensure the correct shape based on the schema and you would only have to check for missing keys.

orestis13:04:00

Oh, I wouldn’t expect this by Lacinia, but it’s a good point — if some layer above could do it then validation becomes more straightforward. I wonder what about nested stuff though — and it’s still going to be a runtime validation, whereas I’d prefer if I could do this at compile time.

donaldball15:04:46

I’m working on couple of prototypes of an internal api, where for the foreseeable future, all clients are going to be clj/cljs. I’m exploring using venia to render the graphql queries themselves so I can work with edn representations pervasively, but I wanted to pause and take stock: have other folk used graphql in an all clj environment, and liked or disliked the experience?

👍 4
bpiel15:04:16

Started a new project in Jan. I've been using this fork of venia so that I can can send edn (as transit) from cljs over websocket to an internal graphql api (powered by hasura, but probably switching to lacinia soonish). I definitely prefer it over anything else I've done in the past (REST etc) https://github.com/district0x/graphql-query

donaldball15:04:44

The wins over REST endpoints for queries seem obvious on a few fronts. I’m a bit less sure about the mutations though. Do you or anyone else have any good or bad patterns for those to share?

bpiel15:04:51

Ah yeah, I'm actually not using graphql for mutations. Not necessarily for any good reason though.

bpiel15:04:23

This is my first experience with graphql. I guess I just felt like I wanted to focus on the query/schema aspect first.

amar15:04:39

I found representing queries in EDN/venia somewhat difficult to work with. Ended up using strings. I haven't found a great need for changing what fields to dynamically add/remove from a query.

orestis15:04:46

I like being able to copy paste a query from graphiql

orestis15:04:01

Haven’t needed so far to make a dynamic query at all

donaldball16:04:26

For my team, it’s almost certainly going to be less about the dynamism than the ability to use our editors’ structural manipulation capabilities, but those are good perspectives, thanks for sharing.

timgilbert19:04:41

We've been using https://github.com/workframers/artemis/. We use transit-json for the serialization part. The biggest PITA is translating back and forth from namespaced keys to non-namespaced ones, IMHO, but adding a translation layer at both sides also lets you shape the data into a conventient form

steveb8n22:04:56

I’m using Venia with re-graph for reads and mutations. I find re-graph much easier to grok than Artemis. Venia providing structural editing is a win for me too

myguidingstar09:04:07

@U4FFD43T4 implementing graphql/lacinia directly may be not as convenient implementing the Clojure native alternative EQL via pathom then providing an extra graphql endpoint as a bridge https://github.com/denisidoro/graffiti

myguidingstar09:04:41

by using eql endpoint with clojurescript client you don't need an extra layer like venia/artemis while still having graphql endpoint for non-cljs clients

myguidingstar09:04:11

@U04V4HWQ4 Graphql's cons from a Clojure perspective: 1. static typing 2. string based therefore not very composable. Also, Timothy discussed it here https://www.reddit.com/r/Clojure/comments/as16f6/transcript_of_timothy_baldridge_and_me_chatting/

bpiel10:04:06

@U0E2YV1UZ I'll check it out. Thanks

donaldball17:04:47

@U0E2YV1UZ Thanks for the link to that conversation, it was extremely interesting.