Skip to content

Issues using TypeResolver, BatchedExecutionStrategy and CompletableFuture together #672

Closed

Description

@drewolson

It seems that if you're using the BatchedExecutionStrategy and a DataFetcher that provides an interface type returns a CompletableFuture, the CompletableFuture rather than the raw value is passed to the TypeResolver for the interface.

Here is a test that demonstrates the issue.

Activity

  1. vincentDAO commented on Sep 1, 2017

    @vincentDAO

    I also got same issue when using BatchedExecutionStrategy and DataFetcher<CompletableFuture<?>> together.

  2. added this to the 5.0 milestone on Sep 5, 2017
  3. bbakerman commented on Sep 6, 2017

    @bbakerman
    Member

    I have had a look at this more and its tricky one to fix (easily) because of the internal structure of the BatchExecutionStrategy

    the root cause is that the BacthedES expects the return value to always be a Iterable but what we need up is producing a Iterable that is a list with a CF inside it

    We wrap that list in a CF in the code but its a CF pointing to a List.

    These objects get passed into as handled as actual objects and not CFS
    handleResult ...

            List<Object> values = result;
            List<FetchedValue> retVal = new ArrayList<>();
            for (int i = 0; i < parentResults.size(); i++) {
                Object value = unboxPossibleOptional(values.get(i));
                retVal.add(new FetchedValue(parentResults.get(i), value));
            }
            return retVal;
    

    eg the object placed into FetchedValue is in fact treated as completed - when in fact it is not completed yet.

    Later those fetched results are sent to handlePrimitive and so on a they get treated as coerced objects etc...

    I think the FetchedValue idea needs to be come a FetchedValue of CF promises and not objects

  4. bbakerman commented on Sep 6, 2017

    @bbakerman
    Member

    also the mutation of the parent structure makes this super tough

     private void handlePrimitives(List<FetchedValue> fetchedValues, String fieldName,
                                  GraphQLType type) {
        for (FetchedValue value : fetchedValues) {
            Object coercedValue = coerce(type, value.getValue());
            //6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
            if (coercedValue instanceof Double && ((Double) coercedValue).isNaN()) {
                coercedValue = null;
            }
            value.getParentResult().putOrAdd(fieldName, coercedValue);
        }
     }
    

    This is pushing state up into the parent which also assumes the value is resolved. Hmmmm

  5. bbakerman commented on Sep 6, 2017

    @bbakerman
    Member

    I take it back its easy to fix. As I described the problem it told me the answer

    List<CF> needs to become CF<List

    And CF.allOf() allows this

        CompletableFuture[] cfs = results.toArray(new CompletableFuture[results.size()]);
        return CompletableFuture.allOf(cfs)
                .thenApply(v -> results.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList()));
    

  6. arlampin commented on Sep 6, 2017

    @arlampin
    Contributor

    @bbakerman there's already an open PR that fixes this

  7. drewolson commented on Sep 7, 2017

    @drewolson
    Author

    I have confirmed this is fixed for me on master. It would be really great to get this into a 4.1.x or a 4.2 release along with the introspection fixes. Thanks!

  8. andimarek commented on Sep 7, 2017

    @andimarek
    Member

    yes, we will do 4.2.

  9. drewolson commented on Sep 7, 2017

    @drewolson
    Author

    Great, thanks!

  10. vincentDAO commented on Sep 8, 2017

    @vincentDAO

    Great! We're waiting to upgrade our app to 4.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions