Fork me on GitHub
#graphql
<
2018-07-09
>
curtosis18:07:28

hi all, question: where do folks usually inject the datomic component into a lacinia system?

eraserhd18:07:33

@curtosis Mine looks like (def web-routes (into [ ...some static routes... ] (map (fn [graphql-route] (update graphql-route 2 #(into [server/webapp-get-db server/check-auth set-error-response] %))))))

eraserhd18:07:57

which, I understand, is kind of not the right way, but it works.

curtosis18:07:12

ah, so you’re not using component (or pedestal)?

eraserhd18:07:50

I am using both.

eraserhd18:07:31

Although there's only one tiny reason to use pedestal, and it adds a bunch of heft for that one tiny reason. 😞

curtosis18:07:46

i see .. so your routes gets the server injected, and you call webapp-get-db to pull it out of there?

curtosis18:07:56

s/it/datomic/

eraserhd18:07:07

So there's a webapp component, which has all the dependencies needed by the pedestal stuff, (it doesn't do anything, just has dependencies).

eraserhd18:07:09

(defn- insert-webapp-interceptor
  "Returns a Pedestal interceptor that injects the web app component
  into the request map as :webapp"
  [webapp]
  (interceptor
   {:name ::insert-webapp
    :enter (fn [context]
             (assoc-in context [:request :webapp] webapp))}))

eraserhd18:07:39

And then this gets a stable db for the request:

eraserhd18:07:41

(def webapp-get-db
  "Pedestal interceptor: Gets the current database value and assoc's
  it into the Pedestal context as :request ::db. Uses the `:webapp`
  component injected by twou.centralpark.server.server."
  (interceptor/interceptor
   {:name ::webapp-get-db
    :enter (fn [ctx]
             (let [database (get-in ctx [:request :webapp :database])]
               (assoc-in ctx [:request ::db] (database/db database))))}))

hlship18:07:50

Why are you using Pedestal if you don't like it?

eraserhd18:07:07

The one feature that keeps me on it right now is server-side events.

eraserhd18:07:49

The code was written well before I was on the project. Pedestal is actually not bad, but I think we didn't need it.

hlship18:07:13

Our code very rarely relies on the injecting any data into the field resolver context; we mostly inject Component dependencies into the field resolvers themselves. We do a lot of validation and auth in the Pedestal interceptor chain before executing the GraphQL query.

eraserhd18:07:22

Interesting... how does one inject a component dependency into the resolvers?

eraserhd18:07:41

Oh wait, I'm probably using lacinia weird in other ways, too 😄

hlship18:07:47

Often using partial ... which is why we added the FieldResolver protocol.

hlship18:07:22

I hope to be back on track to extend the tutorial on fridays.

PB16:07:30

Would love to see this

eraserhd18:07:15

I was happy to discover, by the way, that I could just reference resolver functions in the uncompiled schema, since our schema 90% computed from our data model description.

hlship18:07:15

... and that's why we don't have some comprehensive "read the EDN and compile it all in one go" function, but rather a recipe for the many steps ... because every application is going to want to fabricate or transform various parts of the input schema sooner or later.

👍 4
curtosis19:07:54

ah, yes… partial does seem like it fits in there somewhere