How to implement query filters and sorting using Graphql SPQR?
I'm looking for a solution with Graphql SPQR for schema which looks something like this.
schema {
query: Query
mutation: Mutation
}
enumSortOrder {
ASC
DESC
}
type Article {
id: String
name: String
createdByUserId: String
createdOn: String
lastUpdatedOn: String
}
type Feedback {
id: String
feedbackText: String
articleId: String
createdByUserId: String
createdOn: String
lastUpdatedOn: String
}
type Query {
getAllArticles(pageNumber: Int!, pageSize : Int!, sortOrder: SortOrder!, sortBy: String!): [Article]
getFeedBacksForArticle(articleId: String!): [Feedback]
}
type Mutation {
createArticle(name: String!, createdByUserId: String!): Article
createNewFeedback(feedbackText: String!, articleId: String!, createdByUserId: String!): Feedback
}
GraphQL SPQR relies on a code-first approach. You need to create your java classes and resolver and annotate them properly:
public enum SortOrder {
@GraphQLEnumValue(name = "ASC") ASC,
@GraphQLEnumValue(name = "DESC") DESC
}
public class Article {
//Article implementation here
}
public class Feedback {
//Feedback implementation here
}
public class GraphQLResolver {
@GraphQLQuery(name = "getAllArticles", description = "Search articles")
public List<Article> getAllArticles(
@NotNull @GraphQLArgument(name = "pageNumber") int pageNumber,
@NotNull @GraphQLArgument(name = "pageSize") int pageSize,
@NotNull @GraphQLArgument(name = "sortOrder") SortOrder sortOrder,
@NotNull @GraphQLArgument(name = "sortBy") String sortBy) {
//Query implementation here
}
//implement other queries and mutation
}
Follow the readme at https://github.com/leangen/graphql-spqr to expose your graphql resolver.