benzene73

benzene73

Absinthe Resolver: FunctionClauseError When Optional Argument is Missing

I’m running into a FunctionClauseError in my Absinthe resolver when an optional argument (cursor) is omitted from the query. Here’s my setup:

I’m defining a paginated query for recent reviews, using cursor-based pagination:


@desc "Get recent reviews (cursor-based pagination)"
field :recent_reviews, :review_connection do
  arg :cursor, :string, default_value: nil
  arg :limit, :integer, default_value: 10

  resolve &Resolvers.list_recent_reviews/3
end

In my resolver, I expect both cursor and limit:


def list_recent_reviews(_parent, %{cursor: cursor, limit: limit}, _resolution) do
  Reviews.list_recent_reviews(cursor, limit)
end

However, when I run this GraphQL query without providing cursor:


query RecentReviews {
  recentReviews {
    nextCursor
    items {
      id
    }
  }
}

I get this error:


[error] ** (FunctionClauseError) no function clause matching in KaguyaWeb.GraphQL.Reviews.Resolvers.list_recent_reviews/3
    (kaguya 0.1.0) lib/kaguya_web/graphql/reviews/resolvers.ex:39:
    KaguyaWeb.GraphQL.Reviews.Resolvers.list_recent_reviews(%{}, %{limit: 10}, #Absinthe.Resolution{})

What I Found So Far

  • Even though I set default_value: nil in the schema, Absinthe omits cursor entirely from args when it’s not provided.
  • My function clause expects %{cursor: cursor, limit: limit}, but if cursor is missing, it doesn’t match, causing the FunctionClauseError.

Temporary Fix

I changed the resolver to use Map.get/3 to avoid a missing key issue:


def list_recent_reviews(_parent, args, _resolution) do
  cursor = Map.get(args, :cursor, nil)
  limit = Map.get(args, :limit, 10)

  Reviews.list_recent_reviews(cursor, limit)
end

This works fine, but I’m wondering:

  • Is this the best way to handle optional GraphQL arguments in Absinthe?
  • Shouldn’t default_value: nil ensure that cursor is always present in args, even if the user doesn’t provide it?
  • Why does Absinthe sometimes omit cursor entirely, rather than including it as nil?

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement