GraphQL-SPQR - Error with List without generics

453 Views Asked by At

My GraphQL schema is:

//Schema generated from query classes
GraphQLSchema schema = new GraphQLSchemaGenerator()
        .withBasePackages("com.blah.blah")
        .withOperationBuilder(new DefaultOperationBuilder(DefaultOperationBuilder.TypeInference.UNLIMITED))
        .withOutputConverters((config, defaults) -> defaults.drop(ObjectScalarAdapter.class))
        .withResolverBuilders(new AnnotatedResolverBuilder())
        .withOperationsFromSingleton(myObjectQuery)
        .withTypeTransformer(new DefaultTypeTransformer(true, true))
        .withValueMapperFactory(new JacksonValueMapperFactory())
        .generate();

I have a GraphQLQuery defined:

@GraphQLQuery(name = "getMyObject")
public MyObject getMyObject() {}

which returns a "MyObject" class

the MyObject class does not use generics and is defined as follows (I cannot change it), although I know that myList contains objects of type MyListType:

public class MyObject {
    private List myList;
    public List getMyList() {
        return myList;
    }
}

Using GraphiQL (assume the fields are valid):

{
  getMyObject() {
    id
    myList{
      myListTypeId
    }
  }
}

Results in:

{
  "errors": [
    {
      "message": "Validation error of type SubSelectionNotAllowed: Sub selection not allowed on leaf type null of field myList@ 'getMyObject/myList'",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ]
    }
  ]
}

How do I resolve this error? I added an aspect to coerce the return result from getMylist() to be a List but obviously that had no effect on the validation step.

Question is: How do I configure the schema to allow for getters that return non-generics Lists or Maps?

1

There are 1 best solutions below

0
On

As I understand it @GraphQLQuery needs to be added to the getter.

public class MyObject {
    private List myList;
    @GraphQLQuery(name = "myList")
    public List getMyList() {
        return myList;
    }
}

As you mentioned you might not be able to change this. One way may be to create a wrapper object, so rather than returning MyObject, you return MyWrapperObject, which takes a MyObject in constructor and has SPQR compatible getters to access the varaibles. Alternatively there is @GraphQLContext, which you can use to add (without changing the class) new methods. These methods can be SPQR compatible.

Hope that helps