Skip to content

How to do batching? "Source type is not expected type (java.lang.Iterable)!" #151

Closed
@karl-run

Description

@karl-run

Using graphql-spring-boot-starter, graphql-java-servlet and graphql-java-tools.

I have this resolver:

@Component
public class SomethingGroupResolver implements GraphQLResolver<SomethingGroup> {

  @Autowired
  private SomethingStructureService somethingStructureService;

  @Allow
  public List<Something> getSomethings(SomethingGroup somethingGroup) {
    return somethingStructureService.getAllSomethings(somethingGroup);
  }
}

In my case resolving all somethings on a something group creates a lot of individual database queries so I would like to batch these requests.

My understanding of @Batched is to use it as following:

  @Component
  public class SomethingGroupResolver implements GraphQLResolver<SomethingGroup> {
  
    @Autowired
    private SomethingStructureService somethingStructureService;
  
    @Allow
+   @Batched
-   public List<Something> getSomethings(SomethingGroup somethingGroup) {
+   public List<List<Something>> getSomethings(List<SomethingGroup> somethingGroups) {
      return somethingStructureService.getAllSomethingsForGroups(somethingGroups);
    }
  }

When running this the schema and resolvers validate fine, however at runtime I get this error:

com.coxautodev.graphql.tools.ResolverError: Source type (run.karl.example.SomethingGroup) is not expected type (java.lang.Iterable)!
	at com.coxautodev.graphql.tools.MethodFieldResolver$createDataFetcher$1.invoke(MethodFieldResolver.kt:52) ~[graphql-java-tools-5.2.0.jar:na]
	at com.coxautodev.graphql.tools.MethodFieldResolver$createDataFetcher$1.invoke(MethodFieldResolver.kt:20) ~[graphql-java-tools-5.2.0.jar:na]
	at com.coxautodev.graphql.tools.MethodFieldResolverDataFetcher.get(MethodFieldResolver.kt:146) ~[graphql-java-tools-5.2.0.jar:na]
	at com.coxautodev.graphql.tools.BatchedMethodFieldResolverDataFetcher.get(MethodFieldResolver.kt:166) ~[graphql-java-tools-5.2.0.jar:na]

Debugging a bit shows that SomethingGroupResolver.getSomethings that when batched expects List<SomethingGroup> somethingGroups, only receives a single SomethingGroup.

Is there something about batching and @Batched I'm not understanding? 😸

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