我创建了一个Java应用程序来实现GraphQL服务器。工作正常。现在,我要添加订阅。我读过我必须实现一个Websocket,但我不明白如何使用SimpleGraphQLServlet。
这是我到目前为止的代码:
@WebServlet("/graphql") public class GraphQLEndpoint extends HttpServlet {
SimpleGraphQLServlet graphQLServlet;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
graphQLServlet.service(req, resp);
}
@Override
public void init() throws ServletException {
graphQLServlet = SimpleGraphQLServlet.builder(buildSchema()).build();
}
private GraphQLSchema buildSchema() {
AllRepositories reps = new AllRepositories();
Queries queries = new Queries(reps);
return new GraphQLSchemaGenerator()
.withOperationsFromSingletons(queries)
.generate();
}}
这些是我的依赖项:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.4</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-servlet</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>io.leangen.graphql</groupId>
<artifactId>spqr</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>