profile
viewpoint

Ask questionsReturn response fields based on the context

Need to return response based on the context.

This is my schema.ex

object(:user) do
    field :name, :string
    field :dob, :string
    field :test_1, dataloader(Repo)
    field :test_2, dataloader(Repo)
    field :test_3, dataloader(Repo)
    field :test_4, dataloader(Repo)
    field :test_5, dataloader(Repo)
end

def plugins do
    [Absinthe.Middleware.Dataloader | Absinthe.Plugin.defaults()]
  end

  def context(ctx) do
    loader =
      Dataloader.new()
      |> Dataloader.add_source(Repo, Dataloader.Ecto.new(Repo))
  
    Map.put(ctx, :loader, loader)
end

If the get_user API was hit by logged in user(By context, I was validating that). I will to return all the fields. The dataloader will do loading the associations job for me.

%{
    name: "some name", 
    dob: "some dob", 
    test_1:  "some_test_1", 
    test_2: "some_test_2",
    test_3: "some_test_3",
    test_4: "some_test_4",
    test_5: "some_test_5"
}

If the same get_user API was hit by not logged user(Without token). I only want to return the “name” field from the backend!
%{
    name: "some name", 
    dob: nil, 
    test_1:  nil, 
    test_2: nil,
    test_3: nil,
    test_4: nil,
    test_5: nil
}

How can i achieve this with dataloader?. Dataloader try to load all the association every time(If that particular field was passed from the frontend).

absinthe-graphql/dataloader

Answer questions benwilson512

You need to be patient. It doesn't help that your question contains lots of code sketches that aren't real Elixir code. If you make things more concrete and real it helpful to people.

useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List