0

I tried developing a server side API. I am using GraphQL and want to set a cookie from the server side, but when i try to set the cookie using HttpServletResponse it gives me a NullPointerException?

Here is my Response View class:

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

And here I am setting the 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;
}
kaqqao
  • 12,984
  • 10
  • 64
  • 118

2 Answers2

0

From the code it seems you're using GraphQL SPQR. I can't tell if you're using SPQR Spring Starter as well or not, so I'll just have to assume you are, since your question mentions nothing.

In recent versions, an instance of DefaultGlobalContext<NativeWebRequest> is automatically provided as GraphQL context, so it can be inject by @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
}

If you want to provide a different context, register a custom bean of ServletContextFactory type.

In case you're not using SPQR Spring Starter, you'll have to ensure you provide the applicable context yourself via ExecutionInput:

ExecutionInput input = ExecutionInput.newExecutionInput()
        .query(graphQLRequest.getQuery())
        .operationName(graphQLRequest.getOperationName())
        .variables(graphQLRequest.getVariables())
        .context(response) //HttpServletResponse or whatever you need
        .build();
kaqqao
  • 12,984
  • 10
  • 64
  • 118
0

If you are using graphql-java-kickstart / graphql-java-servlet

Assuming that you can get the

DataFetchingEnvironment env

then you can do this:

GraphQLServletContext context = env.getContext();
context.getHttpServletResponse().addCookie(cookie);
lost in binary
  • 544
  • 1
  • 4
  • 11