Skip to content

Commit e198afa

Browse files
authored
fix(graphql) make graphql accept GETs (dotCMS#31396)
This pull request introduces several changes to the `DotGraphQLHttpServlet` class in order to improve the handling of GraphQL requests. The key modifications include adding a new request wrapper class and updating the `doGet` method to enforce the presence of a query id `?qid=abc123` parameter that can be used to enforce uniqueness in the responses. ref: dotCMS#31395
1 parent 3ae0421 commit e198afa

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

dotCMS/src/main/java/com/dotcms/graphql/DotGraphQLHttpServlet.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletRequestWrapper;
1415
import javax.servlet.http.HttpServletResponse;
1516

1617
public class DotGraphQLHttpServlet extends AbstractGraphQLHttpServlet {
@@ -19,6 +20,21 @@ public class DotGraphQLHttpServlet extends AbstractGraphQLHttpServlet {
1920

2021
private static final String CORS_GRAPHQL = CorsFilter.CORS_PREFIX + ".graphql";
2122

23+
/**
24+
* Wrapper to force the request to be a POST
25+
*/
26+
static class PostRequestWrapper extends HttpServletRequestWrapper {
27+
public PostRequestWrapper(HttpServletRequest request) {
28+
super(request);
29+
}
30+
31+
@Override
32+
public String getMethod() {
33+
return "POST";
34+
}
35+
}
36+
37+
2238
@Override
2339
protected GraphQLConfiguration getConfiguration() {
2440
return GraphQLConfiguration
@@ -30,7 +46,16 @@ protected GraphQLConfiguration getConfiguration() {
3046

3147
@Override
3248
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) {
33-
handleRequest(request, response);
49+
50+
if(request.getParameter("qid") == null) {
51+
Logger.warn(DotGraphQLHttpServlet.class, "No query id (qid) provided in graphql GET . This can result in invalid cached data by both browsers and CDNs. Please provide a distinguishing query id (qid) parameter to execute a graphql query via GET, e.g. /api/v1/graphql?qid=123abc");
52+
Try.run(()->response.sendError(500, "No query id (qid) provided. This can result in invalid cached data by both browsers and CDNs. Please provide a distinguishing query id (qid) parameter to execute a graphql query via GET, e.g. /api/v1/graphql?qid=123abc"));
53+
return;
54+
}
55+
56+
HttpServletRequest wrapper = new PostRequestWrapper(request);
57+
58+
handleRequest(wrapper, response);
3459
}
3560

3661
@Override

dotcms-integration/src/test/java/com/dotcms/graphql/DotGraphQLHttpServletTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testing_cors_headers() {
3939
public void testing_GETRequestToGraphQLServer_returnResponseWithExpectedHeaders()
4040
throws ServletException, IOException {
4141

42-
MockHttpRequestIntegrationTest request = new MockHttpRequestIntegrationTest("localhost", "/");
42+
MockHttpRequestIntegrationTest request = new MockHttpRequestIntegrationTest("localhost", "/?qid=123abc");
4343
MockHeaderResponse response = new MockHeaderResponse(new MockHttpResponse());
4444
DotGraphQLHttpServlet graphQLHttpServlet = new DotGraphQLHttpServlet();
4545
graphQLHttpServlet.init(null);

0 commit comments

Comments
 (0)