graphql-java-tools icon indicating copy to clipboard operation
graphql-java-tools copied to clipboard

Generic wrapper don't work with Lists

Open b4eEX opened this issue 6 years ago • 0 comments

Generic wrappers don't work if used not as outermost wrapper, i.e. Wrapper<String> works while List<Wrapper<String>> doesn't.

My example, with Vavr Try:

package com.my.test;

import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.coxautodev.graphql.tools.SchemaParser;
import com.coxautodev.graphql.tools.SchemaParserOptions;
import graphql.GraphQL;
import io.vavr.control.Try;

import java.util.List;

public class GenericWrapperTest {

    private static final String schemaString = "type Query {\n" +
            "    payloads: [Payload!]!\n" +
            "}\n" +
            "\n" +
            "type Payload {\n" +
            "    data: String!\n" +
            "}";

    private static final String query = "{\n" +
            "    payloads {\n" +
            "        data\n" +
            "    }\n" +
            "}";

    public static class Payload {
        public Try<String> getData() {
            return Try.success("foo");
        }
    }

    public static class Query implements GraphQLQueryResolver {
        public List<Try<Payload>> getPayloads() {
            return List.of(Try.success(new Payload()));
        }
    }

    public static void main(String[] args) {

        final var tryWrapper = SchemaParserOptions.GenericWrapper.withTransformer(io.vavr.control.Try.class, 0,
                Try::get, type -> type);


        final var schema = SchemaParser.newParser()
                .schemaString(schemaString)
                .resolvers(new Query())
                .options(SchemaParserOptions.newOptions()
                        .genericWrappers(tryWrapper)
                        .build())
                .build()
                .makeExecutableSchema();

        System.out.println(GraphQL.newGraphQL(schema).build().execute(query));

    }
}

Trying to run this results in

2019-03-28 14:11:35.212 [main] WARN  g.e.SimpleDataFetcherExceptionHandler:23 - Exception while fetching data (/payloads[0]/data) : Expected source object to be an instance of 'com.my.test.GenericWrapperTest$Payload' but instead got 'io.vavr.control.Try$Success'
com.coxautodev.graphql.tools.ResolverError: Expected source object to be an instance of 'com.my.test.GenericWrapperTest$Payload' but instead got 'io.vavr.control.Try$Success'
	at com.coxautodev.graphql.tools.FieldResolver$getSourceResolver$2.invoke(FieldResolver.kt:27)

Switching payloads to just Try<Payload> makes it run as expected - fail field resolution on failed Try and complete on successful.

b4eEX avatar Mar 28 '19 13:03 b4eEX