I am using graphql-java with graphql-java-annotations in my Spring application (spring-graphql-common seemed very limited and looks like it's not maintained anymore), and I'm struggling with subfields manipulation.
I have two classes A and B that I want to access using GraphQL, here they are:
class A {
@GraphQLField
Integer id;
@GraphQLField
String name;
@GraphQLField
List<B> listOfB;
}
class B {
@GraphQLField
Integer id;
@GraphQLField
String code;
}
I can successfully query all the fields in A and also the fields in B using the following schema:
@Component
public class Schema
{
public GraphQLObjectType aType;
public GraphQLSchema aSchema;
public GraphQLObjectType queryType;
@Autowired
public Schema(DataFetcher dataFetcher) throws IllegalAccessException, InstantiationException, NoSuchMethodException
{
aType = GraphQLAnnotations.object(A.class);
queryType =
GraphQLObjectType
.newObject()
.name("QueryType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("a")
.type(aType)
.argument(GraphQLArgument.newArgument()
.name("id")
.type(new GraphQLNonNull(Scalars.GraphQLInt))
.build())
.dataFetcher(dataFetcher)
.build())
.build();
aSchema = GraphQLSchema.newSchema()
.query(queryType)
.build();
}
}
I can successfully filter on a with the following request:
{
a(id:5)
{
id,
name,
listOfB
{
code
}
}
}
But when I try to filter on listOfB, for example with a take to select only X records, I have the error Validation error of type UnknownArgument: Unknown argument take:
{
a(id:5)
{
id,
name,
listOfB(take:3)
{
code
}
}
}
I understand what the error means, as I haven't declared any possible argument for the listOfB field on A, but I don't know how I could possibly do it using graphql-java-annotations.
I have added a specific DataFetcher for listOfB tho, using the following code in class A:
@GraphQLDataFetcher(BDataFetcher.class)
private List<B> listOfB;
and it is indeed getting called when I retrieve listOfB.
Do you have any idea on how I could add arguments to fields when using graphql-java-annotations? If not, is there any workaround?
Defining the schema without annotations and using the "classic" way of graphql-java is not an option as my schema is really huge :/
Thanks :)
GraphQLFieldwith the DataFetcher. But I'm still getting the same error. I managed to make it work by defininglistOfBas a method instead of a field, but it ended up being way to tedious so I'm currently writing custom annotations to allow filtering automatically on any field just by adding a@GraphQLAnnotationannotation on it. - AntoineB