【发布时间】: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 应用程序,但由于我从浏览器访问它,我收到错误,这告诉我功能 allVehicles 和 allPersons找不到。
(在其他教程和问题中,每个人在他们的 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();
}
}
【问题讨论】: