How to execute a graphQL query without http layer?

I’ve seen it mentioned that graphQL is transport agnostic, but all the examples I’ve seen in the docs for Absinthe involve wiring Absinthe into the router.

I’ve seen test examples where one can supply a string that is the graphQL query, but it still tests going through the ConnCase and http layer.

Are there any examples of taking a string that is the graphQL POST body and triggering the schema’s resolvers directly?

The Absinthe.run/3 does what you want. You pass it a string, the schema you want to run it against and some options like variables/context.

There’s an example in the docs Absinthe — absinthe v1.7.0

  """
  query GetItemById($id: ID) {
    item(id: $id) {
      name
    }
  }
  """
  |> Absinthe.run(App.Schema, variables: %{"id" => params[:item_id]})
7 Likes

I had a similar need. I wanted to query without the HTTP layer. Absinthe.run/3 works, but the result is map that’s meant to be turned into JSON, so things like dates are strings instead of actual date objects.

I created a custom library scratch this itch: qry | Hex