ospaarmann
Elixir: Using Absinthe to query Dgraph, a graph database. GraphQL to GraphQL+ mapping
I am using Absinthe to build a GraphQL API. The datastore is Dgraph, which uses GraphQL+ as query language. It is similar to GraphQL but not identical.
This would in theory put me in a wonderful situation. A GraphQL query like
query {
user {
id
username
posts {
title
text
comments {
text
}
}
}
}
could be also just one query in Dgraph. It would look almost identical:
{
users(func: has(type_user))
{
id
username
posts {
title
text
comments {
text
}
}
}
}
This power of graph databases to load complex relations in one go is something that I would like to use. The problem is just: In Absinthe the schema is supposed to be composable. In the schema would have one :user object that has a :posts field which would be a list_of(:post). And then a :post object. Etc. pp.
To help prevent N+1 queries you would use dataloader or batch loading.
Now I could just load everything in one go. I could for example write a resolver that does just that:
defmodule MyApp.Resolvers.User do
alias MyApp.Users
def users(_, _args, _) do
{:ok, Users.all()}
end
end
And the user context that actually queries the db
defmodule MyApp.Users do
alias MyApp.Users.User
def all do
query = """
{
users(func: has(type_user))
{
id
username
posts {
title
text
comments {
text
}
}
}
}
"""
case ExDgraph.query(conn(), query) do
{:ok, msg} ->
%{result: %{users: users}} = msg
Enum.map(users, fn x -> struct(User, x) end)
{:error, _error} ->
[]
end
end
end
The issue here is that I overfetch. I ALWAYS query everything, even if I only want a list of users. This works but is not very good performance wise. And I loose the composability.
What would be a solution is if I had access to the query in the resolver to understand which fields are requested. I could then use pattern matching to build the query and then send it to Dgraph. I could even have one central resolver and many query builders. But I would need to hook into the query and parse it directly.
Is something like that possible? Any idea where I could find something interesting to solve this? Maybe with Absinthe middleware?
Thanks!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 7 of 7 Posts
WolfDan
Yeah is possible! ^^
I’ve been working on a Dgraph library that does exactly that (non open source atm, but hope soon will if this pull is accepted), here the parser:
So basically you can get the absinthe request data by doing project function, the
infoparameter is the one given by the resolve function in absinte, it gives you the info you need to build the query by parsing the Field struct (seemap_fieldfunction in my code example), and you can get the childs by recursion ^^I was really exited about the dgraph official support for graphql that could simplify a lot all of this, but seems like we need to wait a lot for that
Hope that helps
ospaarmann
Thank you for your input! I will have a look into that. Not sure yet if it solves my issue. But I already had a look into the project function of Absinthe.
I also replied to your PR on my client.
ospaarmann
I think I found a solution. Let’s take a resolver that gets you a list of users. Something like this:
If you hit it with a nested query like:
you will see a complex nested map in the terminal. And it contains all the info we need. The selections contain the fields. Each field has a name. And if it has a nested object it contains again a list
selections.Now we only have to recurse our way down the rabbit hole, collect info for our Dgraph query and build it accordingly. And since it already passed the Absinthe validation etc. it looks like a good option.
Interesting will be if an approach like that will lead to problems with the optimizations Absinthe comes with, like in memory caching of already fetched objects…
WolfDan
Well that solution is exactly what the code I gave to you does, if you check the
map_fieldfunction it takesAbsinthe.Blueprint.Document.Fieldcheck the name and if have selection (childs) map them as well, the other code is for ensure some functionalities of my library, but your solution is exactly the same as mine in some way ^^rrevanth
@ospaarmann Hey, I’m thinking of using Dgraph for a social network type web application.
I want to know how it’s working out for you? How do we handle authentication in dgraph?
alamba78
Any idea when ExDgraph will be close to production ready? Not that I’m impatient or anything. LOL. I just stumbled into Dgraph and it looks really interesting. Gonna dig into the docs.
ospaarmann
Hey there, I am still missing transaction support. And there were two issues that I couldn’t resolve so far (see GitHub). After that it should be good to go. If you want it quicker you can always contribute