如何在Spring Boot的GraphQL突变中从服务器端API设置Cookie?

时间:2019-07-18 13:13:37

标签: spring graphql-java

我尝试开发服务器端API。我正在使用GraphQL,并希望从服务器端设置cookie,但是当我尝试使用HttpServletResponse设置cookie时,它给了我NullPointerException吗?

这是我的“响应视图”类:

package org.jembi.appstore.service.graphql.api.response.view;

public class ResponseView {

    private int code;
    private String message;

    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

在这里设置cookie:

@GraphQLMutation(name="login")
public ResponseView login(@GraphQLArgument(name="msisdn") String msisdn, @GraphQLArgument(name="password")String password, HttpServletResponse response) {

    ResponseView responseView = new ResponseView();
    User user = new User();


    System.out.println(msisdn.length());
    Cookie cookie = null;
    if(msisdn.length()==10) {
        user.setPassword(password);
        user.setMsisdn(msisdn);

        int code = userDao.getUser(user);
        responseView.setCode(code);
        if(code==100) {
            responseView.setMessage("User Logged In Successfully");

            cookie = new Cookie("token", UUID.randomUUID().toString());
            //responseView.doPost(req, resp);
            int hour = 3600000;
            int exp = 14 * 24 * hour; //2 weeks

            cookie.setMaxAge(exp);
            //cookie.setPath("/");
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            //responseView.setCookie(cookie);

        }   
        else 
            responseView.setMessage("Invalid credentials");

    }else {
        responseView.setCode(400);
        responseView.setMessage("Bad Request");
    }
    return  responseView;
}

2 个答案:

答案 0 :(得分:0)

从代码来看,您似乎正在使用GraphQL SPQR。我不知道您是否也正在使用SPQR Spring Starter,所以我只需要假设您是,因为您的问题没有提及。

在最新版本中,DefaultGlobalContext<NativeWebRequest>的实例会自动作为GraphQL上下文提供,因此可以通过@GraphQLRootContext注入它:

@GraphQLMutation(name="login")
public ResponseView login(
     @GraphQLArgument(name="msisdn") String msisdn, 
     @GraphQLArgument(name="password")String password,
     @GraphQLRootContext DefaultGlobalContext<NativeWebRequest> context) {

    HttpServletResponse response = context.getNativeRequest().getNativeResponse(HttpServletResponse.class);
    ... //do what you need to with the raw HTTP response
}

如果要提供其他上下文,请注册ServletContextFactory类型的自定义bean。

如果您不使用SPQR Spring Starter,则必须确保自己通过ExecutionInput提供适用的上下文:

ExecutionInput input = ExecutionInput.newExecutionInput()
        .query(graphQLRequest.getQuery())
        .operationName(graphQLRequest.getOperationName())
        .variables(graphQLRequest.getVariables())
        .context(response) //HttpServletResponse or whatever you need
        .build();

答案 1 :(得分:0)

如果您使用的是 graphql-java-kickstart / graphql-java-servlet

假设您可以获得

DataFetchingEnvironment env

然后您可以执行以下操作:

GraphQLServletContext context = env.getContext();
context.getHttpServletResponse().addCookie(cookie);