[ 
https://issues.apache.org/jira/browse/SLING-10502?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17366501#comment-17366501
 ] 

Bertrand Delacretaz commented on SLING-10502:
---------------------------------------------

I haven't found a nice pattern with {{Optional}}, didn't find an elegant way to 
set the value on first use.

Here's a simple {{LazyLoader}} helper that does the job, complete example at 
https://gist.github.com/bdelacretaz/d0b12a4eec4c134fdbea06aba66de907

{code}
class LazyLoader<T> {
        private final Supplier<T> sup;
        private T value;
        
        LazyLoader(Supplier<T> sup) {
            this.sup = sup;
        }

        T get() {
            if(value == null) {
                synchronized(this) {
                    if(value == null) {
                        value = sup.get();
                    }
                }
            }
            return value;
        }
    }

    class LazyLoadingPojo {
        LazyLoader<Integer> count = new LazyLoader<>(this::computeCount);

        public int getCount() {
            return count.get();
        }

        private int computeCount() {
            return ++callCount;
        }
    }
{code}

> Helpers for lazy loading of GraphQL query results
> -------------------------------------------------
>
>                 Key: SLING-10502
>                 URL: https://issues.apache.org/jira/browse/SLING-10502
>             Project: Sling
>          Issue Type: Improvement
>          Components: GraphQL
>    Affects Versions: GraphQL Core 0.0.10
>            Reporter: Bertrand Delacretaz
>            Priority: Minor
>
> Lazy loading of "expensive" field values can help the performance of GraphQL 
> queries, by omitting calls to methods that retrieve or compute data if the 
> corresponding fields are not used in the result set.
> A natural way of doing that is to return a {{Supplier<SomeType>}} instead of 
> {{SomeType}} directly. I'm not sure if the GraphQL Core supports that 
> correctly out of the box, it would be good to try that and maybe implement 
> the corresponding improvements.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to