chu

chu

I don’t know how to structure this question well so here it goes:
I have this:

[%{"first_name" => "John", "id" => "651029dc-636d-4e54-a03d-d80097c7567e", "last_name" => "Doe", "user_id" => "23232323"}, %{"first_name" => "John", "id" => "41188ffc-4304-4b01-9c45-7565aa8a4aab", "last_name" => "Doe", "user_id" => "23232323"}, %{"first_name" => "John", "id" => "277dd91f-c8e6-4104-b7b0-fecedf933b54", "last_name" => "Doe", "user_id" => "23232323"}]

I need to retrieve
%{"first_name" => "John", "id" => "651029dc-636d-4e54-a03d-d80097c7567e", "last_name" => "Doe", "user_id" => "23232323"}, %{"first_name" => "John", "id" => "41188ffc-4304-4b01-9c45-7565aa8a4aab", "last_name" => "Doe", "user_id" => "23232323"}, %{"first_name" => "John", "id" => "277dd91f-c8e6-4104-b7b0-fecedf933b54", "last_name" => "Doe", "user_id" => "23232323"}

Because of how I rendering these results on my GraphQL endpoint. How can I extract the data without the ?

Showing Posts 10 to 1

peerreynders

peerreynders

Essentially you had an error in your schema:

web/schema.ex

query do
  @desc "Get all blog posts"
  field :posts, list_of(:post) do      # This needs list_of()
     resolve &Blog.PostResolver.all/2  # because this will return 0-to-many results
  end

  @desc "Get a user of the blog"
  field :user, type: :user do          # This doesn't need list_of()
    arg :id, non_null(:id)
    resolve &Blog.PostResolver.find/2  # because this will return 0-or-1 results
  end
end

The missing list_of resulted in the default_resolve being presented with data that it shouldn’t have to handle.
This demonstrates that one should not immediately assume that the fault lies where it manifests itself - the problem is often further up stream.

chu

chu OP

Adam Brodzinski, the author of that example stack was kind enough to direct me to the right place. This is his response:

so either you need to use List.first to get one item or change the query from

mutation do
  field :create_user, type: :user do
    arg :email, non_null(:string)
    arg :password, non_null(:string)
    resolve &GraphQL.Resolvers.User.create/2
  end
end

to a list_of:

mutation do
  field :create_user, type: list_of(:user) do
    arg :email, non_null(:string)
    arg :password, non_null(:string)
    resolve &GraphQL.Resolvers.User.create/2
  end
end

more info: http://absinthe-graphql.org/tutorial/query-arguments

So basically using type: list_of(:user) resolved the issue I was having. :thumbsup:

chu

chu OP

Thanks for that response, have a look at the phoenix-1.2-new-graphql branch of the same repo. That is the one I am using. I also feel that I might be missing something fundamental on how to resolve Lists to Maps in each query in GraphQL helpers to ensure compatibility.

If you look at that branch he is using this block of code to handle the ‘transformations’

` default_resolve fn _, %{source: source, definition: %{name: name}} ->
    if is_boolean(Map.get(source, name)) do
      {:ok, Map.get(source, name)}
    else
      {:ok, Map.get(source, name) || Map.get(source, String.to_existing_atom(name))}
    end
  end`

This is where it’s hitting a snag.

peerreynders

peerreynders

I think you are facing an issue with your design - have a look at query.ex - get queries use handle_get_response (is_map) in the pipeline while the filter queries use handle_get_many_response (is_list) - i.e. by design get queries return at most one result as a single map while filter queries return zero-to-many results (each in their own map) in a list.

Now look at user_query.ex - it uses get and you can’t just drop filter in its place as that would (in this case) break convert_to_string_map:

iex> convert_to_string_map = fn(data) -> for {key, val} <- data, into: %{}, do: {String.to_atom(key), val} end
iex> data = %{"first_name" => "John", "id" => "651029dc-636d-4e54-a03d-d80097c7567e","last_name" => "Doe", "user_id" => "23232323"}
iex> convert_to_string_map.(data)
%{first_name: "John", id: "651029dc-636d-4e54-a03d-d80097c7567e", last_name: "Doe", user_id: "23232323"} 
# That worked!

iex> data = [%{"first_name" => "John", "id" => "651029dc-636d-4e54-a03d-d80097c7567e","last_name" => "Doe", "user_id" => "23232323"},
%{"first_name" => "John", "id" => "41188ffc-4304-4b01-9c45-7565aa8a4aab","last_name" => "Doe", "user_id" => "23232323"}]
iex> convert_to_string_map.(data)
%{} 
# Oops!

iex> data |> Enum.map(convert_to_string_map)
[%{first_name: "John", id: "651029dc-636d-4e54-a03d-d80097c7567e", last_name: "Doe", user_id: "23232323"},
 %{first_name: "John", id: "41188ffc-4304-4b01-9c45-7565aa8a4aab", last_name: "Doe", user_id: "23232323"}]
# That's better

In essence the filter query needs a pipeline after run that can process a list of maps. Now you could wrap the result of a get query inside a list so that it can be processed by the pipeline that was designed for results from a filter query but I suspect that would create problems with the GraphQL response.

NobbZ

NobbZ

It had been better if you had answered instead of editing, but OK, lets do it this way.

As far as I can tell you get a list of maps from somwhere and need to send only a single map per call so somewhere else, so a simple proxy from list to many calls with single maps, something along the following might help:

function_that_retrieves_the_list_of_maps
|> Enum.map(&send_a_single_map_to_the_receiver/1)

PS: The link you gave, seems to be dead…

NobbZ

NobbZ

Since you not really did go into my questions, I can’t really help you.

Can you point us to the exact libraries you are using, can you tell us how you get the data and how you emit the data to the receiver?

Is the list you get from a message sent by another node or was it returned by a simple function call?
Do you need to send the Stuff Map by Map to your GraphQL-thing? Do you need to do it by a function call against a library or do you send a message?

If you can’t answer these questions using words, set up a minimum mix-project which shows your problem in a simplified manner.

chu

chu OP

My data is coming from a RethinkDB store, let me investigate further because I am using a resolver I got from a git repo to send that data into my GraphQL resolvers. If I have my data in the second format

%{"first_name" => "John", "id" => "651029dc-636d-4e54-a03d-d80097c7567e",
"last_name" => "Doe", "user_id" => "23232323"},

it works but not when it’s a list of Maps hence my frustration.

I am using rethinkdb-elixir and I the absinthe-graphql plugin. If I query my database using |> get(args.user_id) it returns data in %{} format but when I use |> filter(%{user_id: args.user_id}) it returns [%{}] and I get an error saying

expected a map, got: [%{

I am hitting a snag within this code:

# convert db keys to symbols since we're not using Ecto
  default_resolve fn _, %{source: source, definition: %{name: name}} ->
    if is_boolean(Map.get(source, name)) do
      {:ok, Map.get(source, name)}
    else
      {:ok, Map.get(source, name) || Map.get(source, String.to_existing_atom(name))}
    end
  end

Generally I am following this repo

The source part of the code above is:

[%{"first_name" => "John", "id" => "651029dc-636d-4e54-a03d-d80097c7567e",
"last_name" => "Doe", "user_id" => "23232323"},
%{"first_name" => "John", "id" => "41188ffc-4304-4b01-9c45-7565aa8a4aab",
"last_name" => "Doe", "user_id" => "23232323"},
%{"first_name" => "John", "id" => "277dd91f-c8e6-4104-b7b0-fecedf933b54",
"last_name" => "Doe", "user_id" => "23232323"}]
NobbZ

NobbZ

The first part of your question shows a List of Maps, the second example you show, is not valid Elixir… At least not for itself…

So please try to elaborate further what exactly you want to achieve, where your data comes from and where you want to send it into.

uranther

uranther

I am a little confused by the question, but maybe you want to use Enum.each/2 or some of the other Enum functions. I hope that helps!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews