簡體   English   中英

如何訪問java程序中觸發的graphql查詢?

[英]How to access the graphql query fired in the java program?

我正在使用graphql創建一個rest api。 我想在 java 程序中記錄用戶觸發的查詢。 我無法訪問程序中的查詢。 下面是我寫的一段代碼:

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

}

在這里我想在函數創建員工中記錄查詢。 任何幫助將不勝感激。

小提示:您不能使用 GraphQL 創建 REST API,因為它們是並發 API 模式。 但這只是一個小提示(例如,請參閱此資源以獲得更好的理解)。

來到您的問題:我不知道您使用的 GraphQL 庫。 也許有更好的可能性,但使用以下解決方案,無論圖書館如何,您都應該能夠實現您想要的。

您始終可以通過 spring 或其他注入方法(取決於您的 Spring 版本)將請求對象檢索為javax.servlet.http.HttpServletRequest 有關注入示例,請參閱此問題

示例:假設您使用 @Autowired Annotation 在類中注入 HttpServletRequest bean。 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM