如何访问java程序中激发的graphql查询?
我正在使用graphql创建一个RESTAPI。我想在java程序中记录用户触发的查询。我无法访问程序中的查询。 以下是我编写的一段代码:如何访问java程序中激发的graphql查询?,java,spring,graphql,Java,Spring,Graphql,我正在使用graphql创建一个RESTAPI。我想在java程序中记录用户触发的查询。我无法访问程序中的查询。 以下是我编写的一段代码: package com.x.demo.GraphQL.Resolver; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.coxautodev.graphql
package com.x.demo.GraphQL.Resolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import com.x.demo.GraphQL.Model.Employee;
import com.x.demo.GraphQL.Services.EmployeeService;
@Component
public class MutationResolver implements GraphQLMutationResolver{
@Autowired
private EmployeeService employeeService;
public Employee createEmployee(String name, String profile, String DOJ, String salary) {
return this.employeeService.createEmployee(name, profile, DOJ, salary);
}
}
这里我想在函数createemployee中记录查询。
任何帮助都将不胜感激。小提示:您无法使用GraphQL创建REST API,因为它们是并发API模式。但这只是一个小小的提示(例如,为了更好地理解,请参见此) 关于您的问题:我不知道您使用的GraphQL库。也许有更好的可能性,但使用以下解决方案,无论库是什么,您都应该能够实现您想要的 您始终可以通过SpringBE或其他注入方法(取决于您的spring版本)以
javax.servlet.http.HttpServletRequest
的形式检索请求对象。有关注入示例,请参见
示例:假设在类中注入带有@Autowired注释的HttpServletRequestbean。Spring将在运行时注入适当的对象。然后,您可以使用请求获取请求的正文:
@Autowired
private HttpServletRequest request;
public Employee createEmployee(String name, String profile, String DOJ, String salary) {
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
return this.employeeService.createEmployee(name, profile, DOJ, salary);
}