【问题标题】:GraphQL with Java - How to seperate resolvers into multiple classesGraphQL with Java - 如何将解析器分成多个类
【发布时间】:2019-05-20 00:25:03
【问题描述】:

首先我要道歉。可能这是一个愚蠢的初学者问题,但是在浏览了几十个教程和问题后,我慢慢地感到沮丧......

有什么问题? 我有一个简单的架构,如下所示:

schema {
    query: Query
}

type Query {
    allVehicles: [Vehicle]!
    allPersons: [Person]!
}

type Vehicle{
    name: String!
}

Person{
    name: String!
}

现在我试图让不同的类解决人员和车辆的查询。所以我为人和车辆建立了一个查询类,都实现了 GraphQLQueryResolver 接口。

比我有一个构建模式的类,它看起来像下面这样:

@WebServlet(urlPatterns = "/graphql")
public class GraphQLEndpoint extends SimpleGraphQLServlet {

public GraphQLEndpoint() {

    super(buildSchema());
}

private static GraphQLSchema buildSchema() {

    final VehicleRepository vehicleRepository = new VehicleRepository();
    final PersonRepository personRepository = new PersonRepository();

    return SchemaParser.newParser()
        .file("schema.graphqls")
        .resolvers(new VehicleQuery(vehicleRepository), 
                   new PersonQuery(personRepository)), 
        .build()
        .makeExecutableSchema();

    }
}

我可以在我的码头服务器上启动 web 应用程序,但由于我从浏览器访问它,我收到错误,这告诉我功能 allVehiclesallPersons找不到。

(在其他教程和问题中,每个人在他们的 schema.graphqls 旁边都有 .js 文件,但我既不明白它们是如何工作的,也不明白为什么它们是必要的。schema.graphqls 有什么意义,如果它不能让我委托哪个类来处理相应的查询。)

所以请,谁能告诉我,我做错了什么?

编辑:也许我应该向您展示其中一个查询类:

public class PersonQuery implements GraphQLRootResolver {

private final PersonRepository _personRepository;

public PersonQuery(
    final PersonRepository pPersonRepository) {

    this._personRepository = pPersonRepository;
}

public List<Person> allPersons() {

    return _personRepository.getAllPersons();
}

}

【问题讨论】:

    标签: java api graphql


    【解决方案1】:

    我使用 Spring Boot Graphql 服务器并将GraphQLResolver 拆分为多个类,如下所示:

    1) 创建一个抽象的Query 类,实现GraphQLQueryResolver

     import com.coxautodev.graphql.tools.GraphQLQueryResolver;
       public abstract class Query implements GraphQLQueryResolver {
       }
    

    2) 通过扩展 Query 创建单独的查询解析器类。就我而言,我有两个课程AuthorQueryBookQuery

    BookQuery

    import com.swissre.graphql.model.Book;
    import com.swissre.graphql.repository.BookRepository;
    
    public class BookQuery extends Query {
       // private BookRepository bookRepository;
        private BookRepository bookRepository;
    
        public BookQuery(BookRepository bookRepository) {
             super();
            this.bookRepository = bookRepository;
        }
    
        public Iterable<Book> findAllBooks() {
            return bookRepository.findAll();
        }
    

    AuthorQuery

    import com.swissre.graphql.model.Author;
    import com.swissre.graphql.repository.AuthorRepository;
    
    public class AuthorQuery extends Query {
    
        private AuthorRepository authorRepository;
    
        public AuthorQuery(AuthorRepository authorRepository) {
             super();
            this.authorRepository = authorRepository;
    
        }
    
        public Iterable<Author> findAllAuthors() {
            return authorRepository.findAll();
        }
    

    您需要提供这些解析器类作为 Graphql 构建模式的一部分。就我而言,我使用 Spring Boot Graphql 服务器,如果您不更改 Graphql 端点,那么它不需要任何额外的配置。我所做的就是在主 Spring 配置文件中提供这些 Bean,如下所示:

    @SpringBootApplication
    public class DemoGraphQLApplication  {
    
    
    
    
        public static void main(String[] args) {
            SpringApplication.run(DemoGraphQLApplication.class, args);
        }
    
    
         @Bean
            public BookResolver bookResolver(AuthorRepository authorRepository) {
                return new BookResolver(authorRepository);
            }
    
         @Bean
            public AuthorResolver authorResolver(BookRepository bookRepository) {
                return new AuthorResolver(bookRepository);
            }
    

    通过此设置,我能够成功运行具有多个解析器类的 Graphql 服务器。 希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 2019-07-14
      • 1970-01-01
      • 2018-06-26
      • 2021-01-23
      • 2023-04-08
      • 2021-09-10
      • 2019-10-14
      • 2019-09-07
      相关资源
      最近更新 更多