Skip to content

Generic wrapper don't work with Lists #254

Open

Description

@b4eEX

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.

Activity

  1. changed the title [-]Nested generic wrappers don't work[/-] [+]Generic wrappers aren't applied recursively.[/+] on Mar 28, 2019
  2. changed the title [-]Generic wrappers aren't applied recursively.[/-] [+]Generic wrappers aren't applied recursively[/+] on Mar 28, 2019
  3. changed the title [-]Generic wrappers aren't applied recursively[/-] [+]Generic wrapper don't work with Lists[/+] on Mar 28, 2019
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions