woohaaha

woohaaha

Absinthe GraphQL viewer/me pattern

There is a pattern in GraphQL called viewer. Using the example in the Absinthe book the me is the root viewer:

{
  me {
    orders {
      id
      items { 
        name
        quantity
      }
    }
  }
    menuItems {
    name 
  }
}

It is still not so clear to me how to implement this. I did some searching online as well and couldn’t find any examples.

Does anyone have any examples of how to implement the root viewer pattern with absinthe?

Thank you

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I can’t say I have much insight into the ex-Facebook crowd. I think whether or not me is a useful pattern depends a bit on how you do authentication and authorization in your application. If you have entities owned by the current user then I definitely think it makes sense to scope it under that. It gets a bit trickier if you’re using role based authentication and authorization is less about “me” and more about “the role I want to use for this request”.

You can also find minor inconsistencies between the me pattern and the node pattern. I absolutely adore the node field pattern because it saves you from redundantly setting up hundreds of “get this entity” fields. However it’s { node(id: $id) { ... Fields } } not { me { node() } }, but of course the permissions you have will affect whether or not you can access it.

Really though I think the decline of me or viewer is less about it not being a good idea, and more about the ex-Facebook crowd wanting to distance themselves from a dogma which made it a required pattern. Early versions of Relay (Facebook’s client) were exceptionally dogmatic with respect to how the schema needed to be organized, and it has tried to become more flexible over time.

I suppose where that is leaving me is this: I think me is a valuable pattern for accessing user specific data. I do not necessarily think that it is the most consistent pattern for acting as the primary entrypoint to the rest of the graph. However if that’s the route you want to go I think there can still be real upsides, so it’s a judgement call you’ll have to make.

hykw

hykw

Does this showcase help you?

Where Next?

Popular in Questions Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement