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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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 I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
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...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New

We're in Beta

About us Mission Statement