sandorbedo
How to return different field values with aliases?
Hi,
Let’s say I have a GraphQL schema like this:
type Ops {
add(a: Int, b: Int): Int
}
type Query {
calc: Ops
}
…and I want my Absinthe server to answer to queries like this:
query {
calc {
some: add(a: 1, b: 2)
some_other: add(a: 41, b: 1)
}
}
The expected answer is like this:
{
"data": {
"calc": {
"some": 3,
"some_other": 42
}
}
}
Now my problem is that I can’t figure out what to return in the resolver of the calc query. The first guess was to just return {:ok, %{some: 3, some_other: 42}}, but that doesn’t work. I tried a map with some and some_other key as strings. Also tried to return nested maps, and binary keys like "some:add" and "some: add". None of them worked. The only way Absinthe gives me a non-null response is to use the :add key in the return map, but that way I cannot distinguish between the two separate add calculations. (That means some and some_other collapses into the same value.)
What is the proper way to answer a query like this? What should I return from my resolver to get back two separate values for my tow aliases?
First Post!
benwilson512
Resolvers don’t care about field aliases (your “some” or “some_other”). Just return a value consistent with your resolver type eg {:ok, 42}.







