3232public class GraphiQLController {
3333
3434 private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS_GRAPHIQL = "//cdnjs.cloudflare.com/ajax/libs/graphiql/" ;
35- @ Value ("${graphiql.endpoint:/graphql}" )
35+ @ Value ("${graphiql.endpoint.graphql :/graphql}" )
3636 private String graphqlEndpoint ;
3737
38+ @ Value ("${graphiql.endpoint.subscriptions:/subscriptions}" )
39+ private String subscriptionsEndpoint ;
40+
41+ @ Value ("${graphiql.static.basePath:/}" )
42+ private String staticBasePath ;
43+
3844 @ Value ("${graphiql.pageTitle:GraphiQL}" )
3945 private String pageTitle ;
4046
@@ -86,17 +92,21 @@ private void addIfAbsent(Properties headerProperties, String header) {
8692 public void graphiql (HttpServletRequest request , HttpServletResponse response , @ PathVariable Map <String , String > params ) throws IOException {
8793 response .setContentType ("text/html; charset=UTF-8" );
8894
89- String endpoint = constructGraphQlEndpoint (request , params );
90- Map <String , String > replacements = getReplacements (endpoint );
95+ Map <String , String > replacements = getReplacements (
96+ constructGraphQlEndpoint (request , params ),
97+ request .getContextPath () + subscriptionsEndpoint ,
98+ request .getContextPath () + staticBasePath
99+ );
91100
92101 String populatedTemplate = StrSubstitutor .replace (template , replacements );
93- populatedTemplate = addContextPathIfEnabled (request , populatedTemplate );
94102 response .getOutputStream ().write (populatedTemplate .getBytes (Charset .defaultCharset ()));
95103 }
96104
97- private Map <String , String > getReplacements (String endpoint ) {
105+ private Map <String , String > getReplacements (String graphqlEndpoint , String subscriptionsEndpoint , String staticBasePath ) {
98106 Map <String , String > replacements = new HashMap <>();
99- replacements .put ("graphqlEndpoint" , endpoint );
107+ replacements .put ("graphqlEndpoint" , graphqlEndpoint );
108+ replacements .put ("subscriptionsEndpoint" , subscriptionsEndpoint );
109+ replacements .put ("staticBasePath" , staticBasePath );
100110 replacements .put ("pageTitle" , pageTitle );
101111 replacements .put ("graphiqlCssUrl" , graphiqlUrl ("graphiql.min.css" ));
102112 replacements .put ("graphiqlJsUrl" , graphiqlUrl ("graphiql.min.js" ));
@@ -109,17 +119,7 @@ private String graphiqlUrl(String filename) {
109119 if (graphiqlCdnEnabled && StringUtils .isNotBlank (graphiqlCdnVersion )) {
110120 return CDNJS_CLOUDFLARE_COM_AJAX_LIBS_GRAPHIQL + graphiqlCdnVersion + "/" + filename ;
111121 }
112- return "/vendor/" + filename ;
113- }
114-
115- private String addContextPathIfEnabled (HttpServletRequest request , String populatedTemplate ) {
116- if (StringUtils .isNotBlank (request .getContextPath ())) {
117- String vendorPathWithContext = String .format ("%s/vendor" , request .getContextPath ());
118- populatedTemplate = populatedTemplate
119- .replaceAll ("src=\" /vendor" , "src=\" " + vendorPathWithContext )
120- .replaceAll ("href=\" /vendor" , "href=\" " + vendorPathWithContext );
121- }
122- return populatedTemplate ;
122+ return staticBasePath + "vendor/" + filename ;
123123 }
124124
125125 private String constructGraphQlEndpoint (HttpServletRequest request , @ RequestParam Map <String , String > params ) {
0 commit comments