pierreabreup

pierreabreup

Paginate root object with Absinthe

This is my schema:

defmodule ContentProxyGraphql.Schema.ObjectTypes.Content do
  use Absinthe.Schema.Notation

  import_types Absinthe.Type.Custom
  import_types ContentProxyGraphql.Schema.ObjectTypes.{
    Person,
    Language,
    Publisher
  }

  alias ContentProxyGraphql.Resolvers

  object :content do
    field :type_of, non_null(:string)
    field :name, non_null(:string)
    field :date_created, non_null(:datetime)
    field :date_modified, non_null(:datetime)
    field :learning_resource_type, non_null(:string)
    field :description, :string
    field :thumbnail_url, :string
    field :license, :string
    field :educational_use, :string
    field :interactivity_type, :string
    field :url, :string
    field :author, list_of(:person)
    field :additional_property, list_of(:property_value)
    field :educational_alignment, list_of(:alignment_object)
    field :identifier, non_null(:property_value)
    field :publisher, :publisher
    field :in_language, :language
    field :audience, :education_audience
    field :is_part_of, :content_reference
  end

  object :property_value do
    field :type_of, non_null(:string)
    field :property_id,  non_null(:string)
    field :value, non_null(:string)
  end

  object :education_audience do
    field :type_of, non_null(:string)
    field :education_role, non_null(:string)
  end

  object :alignment_object do
    field :type_of, non_null(:string)
    field :alignment_type, non_null(:string)
    field :educational_framework, non_null(:string)
    field :target_name, non_null(:string)
    field :target_url, :string
  end

  object :content_reference do
    field :type_of, non_null(:string)
    field :identifier, :property_value
  end

  object :content_queries do
    field :contents, list_of(:content) do
      arg :type_of, :string
      resolve &Resolvers.Resources.Content.list/2
    end
  end


end

When I doing a query, I get this result:

I’m struggling to implement absinthe pagination on root object (in my case :content) because all of absinthe examples are about nested objects ( Absinthe.Relay.Connection — absinthe_relay v1.6.0 ). Is there a way to implement root object pagination?

To exemplify my problem, I want this exact pagination

Most Liked

pierreabreup

pierreabreup

Hi @benwilson512

Works like a charm!

connection field :contents, node_type: :content do
      resolve fn
        pagination_args, _ ->
          Resolvers.Resources.Content.list(pagination_args)
          |> Absinthe.Relay.Connection.from_list(pagination_args)
      end
    end

I’ve used Absinthe.Relay.Connection.from_list just for tests propose. The correct solution is to use from_slice

It is worth noting In this case, source: is unuseful, as you had predict. The source: is used when you are listing associated object ( Absinthe.Relay.Connection — absinthe_relay v1.6.0 )

object :person do
  field :first_name, :string
  connection field :pets, node_type: :pet do
    resolve fn
      pagination_args, %{source: person} ->
        Absinthe.Relay.Connection.from_list(
          Enum.map(person.pet_ids, &pet_from_id(&1)),
          pagination_args
        )
      end
    end
  end
end

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
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
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
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement