0

如何使用 Graphql SPQR 实现查询过滤器和排序?

我正在寻找一个使用 Graphql SPQR 的解决方案,它看起来像这样。

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
}
4

1 回答 1

1

GraphQL SPQR 依赖于代码优先的方法。您需要创建您的 java 类和解析器并正确注释它们:

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 
}

按照https://github.com/leangen/graphql-spqr上的自述文件公开您的 graphql 解析器。

于 2020-09-25T01:14:21.000 回答