thojanssens1

thojanssens1

Absinthe GraphQL: best practice for object graph

I work on a calendar app, and to query appointments, my query document looks like below:

me {
  calendar {
    appointments { ... }
    availabilities { ... }
  }
}

To access the appointments, I have to resolve the current user, then the calendar, then finally the appointments.

This follows the Ecto schemas associations.

And in the JS client, I have account.calendar.appointments.

It made me thinking, shouldn’t I also offer the possibility to fetch the appointments directly from the account?

me {
  appointments { ... }
  availabilities { ... }
}

But then I need to handle two function clauses for the resolver:

appointments(%Calendar{} = calendar, %{date_range: date_range}, _)
appointments(%Account{} = account, %{date_range: date_range}, _)

Question:
If I further reason like that for other parts, I’ll have resolvers that resolve an entity for multiple parent types with multiple clauses, is that a normal thing to have? Do you resolve entities for multiple parents to avoid having to access deeply nested objects?
Or do you design the graph closely following the Ecto schema associations (such as seen in first query document)? I’m confused..

Secondly, when the JS client requests for example the appointments, I end up with an account (as I’m using the viewer ‘me’ pattern as seen above), and not directly appointments (or as in my first example, an account, then a calendar, then appointments). So code/variables/function names tends to be a little incoherent on the JS client side, e.g. a fetchAppointments function requesting the appointments will result into an account.. there are many ways to solve this issue, but how do you deal with that?

Any guidance is welcome!

First Post!

cjbottaro

cjbottaro

I’m interested in how people approach this also. We got a fairly large Absinthe project and faced some of these same decisions.

For the most part no. It doesn’t feel very dry to me (too many ways to skin a cat). Or it feels like it’s just making convenience functions just for the reason of making your query smaller.

The biggest issue for us (and this might relate to some of your issues) is that we use Dataloader exclusively, which makes it where resolvers can’t “call” other resolvers, so when we want to share functionality, we make “resolver helpers” that different resolvers can call. Which looks like this for your example:

def appointments(%Calendar{} = calendar, args, res) do
  res.context.loader
  |> load_appointments(calendar, args, fn _loader, appointments ->
    {:ok, appointments}
  end)
end

def appointments(%Account{} = account, args, res) do
  res.context.loader
  |> load(:db, :calendar, account)
  |> on_load(fn loader, calendar ->
    loader
    |> load_appointments(calendar, args, fn _loader, appointments ->
      {:ok, appointments}
    end)
  end)
end

(we have our own Dataloader helpers that make this API possible; I know it’s not standard)

I don’t know if that’s the best way to share functionality between Dataloader based resolvers, but it works.

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14403 124
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement