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

First Post!

pierreabreup

pierreabreup

I’m trying this Absinthe Root Query Pagination · GitHub but no success

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

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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
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
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
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 49266 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement