Skip to content

Type dictionary for ListType #437

Closed
Closed

Description

@mmatela

I'm trying to define some graphQL types dynamically using a typeDefinitionFactory and the fact that field values can be resolved using the Map.get() method. It seems to be working fine as long as I don't have fields with list types. Here's a minimal example of the problem I bumped into:

public static class MyFooType {
    public String bar;
}

public static class FooResolver extends HashMap<String, Object> implements GraphQLQueryResolver {
    @Override
    public Object get(Object key) {
        if ("foo".equals(key)) {
            return Arrays.asList(new MyFooType(), new MyFooType());
        }
        throw new IllegalArgumentException("unknown field " + key);
    }
}

public static void main(String[] args) {
    GraphQLSchema schema =
        SchemaParser.newParser().schemaString("""
                type Query{ foo: [FooType] }
                type FooType { bar: String }
                """)
            .resolvers(new FooResolver())
            .dictionary("FooType", MyFooType.class)
            .build().makeExecutableSchema();
    System.out.println("OK!");
}

This causes an exception:

Exception in thread "main" graphql.kickstart.tools.SchemaClassScannerError: Unable to match type definition (ListType{type=TypeName{name='FooType'}}) with java type (java.lang.Object): Java class is not a List or generic type information was lost: class java.lang.Object
	at graphql.kickstart.tools.TypeClassMatcher.error(TypeClassMatcher.kt:20)
	at graphql.kickstart.tools.TypeClassMatcher.match(TypeClassMatcher.kt:78)
	at graphql.kickstart.tools.TypeClassMatcher.match(TypeClassMatcher.kt:23)
	at graphql.kickstart.tools.SchemaClassScanner.scanResolverInfoForPotentialMatches(SchemaClassScanner.kt:257)
	...

I guess the problem could be that the framework doesn't know whether to expect a java.util.List or a java array as the foo value. Is there a way to pass this information?
I tried .dictionary("[FooType]", List.class), but that results in another exception:

Exception in thread "main" graphql.kickstart.tools.SchemaClassScannerError: Class in supplied dictionary 'java.util.List' specified type name '[FooType]', but a type definition with that name was not found!
	at graphql.kickstart.tools.SchemaClassScanner.<init>(SchemaClassScanner.kt:53)
	at graphql.kickstart.tools.SchemaParserBuilder.scan(SchemaParserBuilder.kt:153)

Activity

  1. nnrudakov commented on Mar 25, 2021

    @nnrudakov

    Did you solve it?

  2. mmatela commented on Jun 2, 2021

    @mmatela
    Author

    Nope, I had to add a wrapper class and declare an accessor method for each Collection<?> value in the map, lots of boilerplate.

  3. oryan-block commented on Jan 31, 2024

    @oryan-block
    Collaborator

    Closing this since it's old but here is a working example anyway:

        @Test
        fun `bing`() {
            val schema = SchemaParser.newParser()
                    .schemaString(
                        """
                        type Query{ foo: [FooType] }
                        type FooType { bar: String }
                        """)
                    .resolvers(FooResolver())
                    .dictionary("FooType", FooType::class.java)
                    .build().makeExecutableSchema()
        }
    
        private class FooResolver : GraphQLQueryResolver {
            fun foo(): List<FooType> = listOf()
        }
        data class FooType(val bar: String = "")
    
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions