简体   繁体   English

GraphQL with Java - 如何将解析器分成多个类

[英]GraphQL with Java - How to seperate resolvers into multiple classes

first of all i have to apologize. 首先,我要道歉。 Probably this is a stupid beginner question, but slowly i get frustrated after paging thorugh dozens of tutorials and issues... 可能这是一个愚蠢的初学者问题,但是在阅读了几十个教程和问题之后我慢慢感到沮丧......

Whats the Problem? 有什么问题? I have a simple schema, looking like the following: 我有一个简单的架构,如下所示:

schema {
    query: Query
}

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

type Vehicle{
    name: String!
}

Person{
    name: String!
}

Now im trying to let different classes resolve the queries for person and vehicle. 现在我试图让不同的类解决人和车辆的查询。 So i build a query class for person and one for vehicle, both implement the GraphQLQueryResolver interface. 所以我为person构建了一个查询类,为车辆构建了一个查询类,它们都实现了GraphQLQueryResolver接口。

Than i have a class, which builds the schema, and it looks like the following: 比我有一个构建模式的类,它看起来如下所示:

@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();

    }
}

I can start the webapplication on my jetty server, but since i visit it from the browser, i get errors, which tell me, that the functions allVehicles and allPersons cannot be found. 我可以在我的jetty服务器上启动webapplication,但是因为我从浏览器访问它,我得到错误,告诉我,无法找到allVehiclesallPersons函数。

(In other tutorials and issues, everybody has .js files beside their schema.graphqls but i neither don't understand, how they work nor why they are necessary. What's the point of the schema.graphqls, if it's not able to let me delegate which class has to deal the respective queries.) (在其他教程和问题中,每个人都有他们的schema.graphqls旁边的.js文件,但我不明白,它们是如何工作的,也不是为什么它们是必要的。如果它不能让我,那么schema.graphqls有什么意义呢?委托哪个类必须处理相应的查询。)

So please, could anyone tell me, what i am doing wrong? 所以,请问,有谁能告诉我,我做错了什么?

Edit: Maybe i should show you one of the query classes: 编辑:也许我应该向您展示一个查询类:

public class PersonQuery implements GraphQLRootResolver {

private final PersonRepository _personRepository;

public PersonQuery(
    final PersonRepository pPersonRepository) {

    this._personRepository = pPersonRepository;
}

public List<Person> allPersons() {

    return _personRepository.getAllPersons();
}

}

I use the Spring Boot Graphql server and I split the GraphQLResolver into multiple classes as follows: 我使用Spring Boot Graphql服务器,我将GraphQLResolver分成多个类,如下所示:

1) Create an abstract Query class which implements GraphQLQueryResolver 1)创建一个实现GraphQLQueryResolver的抽象Query

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

2) Create individual query resolver classes by extending Query . 2)通过扩展Query创建单独的查询解析器类。 In my case I have two classes AuthorQuery and BookQuery : 在我的例子中,我有两个类AuthorQueryBookQuery

The BookQuery class 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();
    }

The AuthorQuery Class 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();
    }

And you need to provide those resolver classes as a part of Graphql build schema. 并且您需要将这些解析器类作为Graphql构建模式的一部分提供。 In my case, I use Spring Boot Graphql server and if you don't change the Graphql endpoint, then it doesn't need any extra configurations. 在我的例子中,我使用Spring Boot Graphql服务器,如果你不更改Graphql端点,那么它不需要任何额外的配置。 All I do is to provide these Beans in the main Spring config file like below: 我所做的就是在Spring主配置文件中提供这些Beans,如下所示:

@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);
        }

With this set up, I am able to run the Graphql server with multiple resolver classes successfully. 通过这个设置,我能够成功运行具有多个解析器类的Graphql服务器。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM