question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return POJO instead of LinkedHashMap

See original GitHub issue

Dear all, I’m trying to use graphql-java for loading a graph of in-memory java objects and doing some transformations on them (e.g. exporting to CSV). When I use graphql.execute(ExecutionInput), it always provides a list of nested LinkedHashMaps However, I would prefer having results as POJOs.

I dig a little bit into the code, and found, that the results are aggregated in AbstractAsyncExecutionStrategy#handleResults. So I decided to implement a custom strategy, which would build POJO directly. However, I don’t see any way, how to know, which output class it should be. I could take it from the current ExecutionPath (which is available in ExecutionStrategyParameters), but handleResults doesn’t have this parameter.

So, my questions are:

  1. Is there any way to get current ExecutionPath in handlerResults?
  2. Is there any other way to produce POJO instead of LinkedHashMap?

PS thanks a lot for the wonderful library!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bbakermancommented, Apr 16, 2020

I am not sure that returning POJOs is the right thing to do.

graphql is a system where the consumer indicates what data they want. They may ask for 6 fields or 2. They may ask for it down 3 levels or 1.

The map approach we use (where ExecutionResult is a temporary holder and converted into a map as we descend and return up from the query level is very deliberate.

It allows

{
     pet {
           name
            tricks {
                  type
            }
    }
}

To become a map like

{
     "pet" :  {
           "name": "spot",
            "tricks" :  [
                  "type" : "sit"
            ]
    }
}

Maybe you ask for tricks in a query or maybe you dont. The idea of this being POJOs as we execute the engine is not sensible in my opinion.

Let’s say you had POJOs hypothetically. What would you do with them? Throw them at Jackson ObjectMapper and send them over the wire as JSON? A Map is just as good for that, in some ways better (no reflection needed).

You say

However, I would prefer having results as POJOs.

Can you outline your thoughts on why POJOs would help you build better graphql based software?

0reactions
bugycommented, Jan 8, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Casting LinkedHashMap to Complex Object - Stack Overflow
You can use ObjectMapper.convertValue() , either value by value or even for the whole list. But you need to know the type to...
Read more >
Jackson: java.util.LinkedHashMap cannot be cast to X
Let's create a simple Java application to reproduce this exception to understand when the exception will occur. 2.1. Creating a POJO Class.
Read more >
Query a list of DTO (pojo class) returns a list of LinkedHashMap
I use Kotlin 1.3.61, spring boot 2.2.4, jackson 2.10.2, axon 4.2.1, jdk 12, axon server. Multi module maven project, let's call them "query"...
Read more >
How to Convert all LinkedHashMap Values to a List in Java?
To convert all values of the LinkedHashMap to a List using the values() method. The values() method of the LinkedHashMap class returns a ......
Read more >
10 Examples of Converting a List to Map in Java 8 - Javarevisited
toMap() method to convert a List of an object into a Map in Java. Remember, the Map returned by Collector is not necessarily...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found