11package graphql .kickstart .autoconfigure .editor .graphiql ;
22
3+ import static java .util .Objects .nonNull ;
4+
35import com .fasterxml .jackson .core .JsonProcessingException ;
46import com .fasterxml .jackson .databind .ObjectMapper ;
5- import graphql .kickstart .autoconfigure .editor .PropertyGroupReader ;
6- import graphql .kickstart .autoconfigure .editor .PropsLoader ;
7+ import graphql .kickstart .autoconfigure .editor .graphiql . GraphiQLProperties . Props . GraphiQLVariables ;
8+ import graphql .kickstart .autoconfigure .editor .graphiql . GraphiQLProperties . Resources ;
79import java .io .IOException ;
810import java .io .InputStream ;
911import java .nio .charset .Charset ;
12+ import java .nio .charset .StandardCharsets ;
13+ import java .nio .file .Files ;
14+ import java .util .Collections ;
1015import java .util .HashMap ;
1116import java .util .Map ;
12- import java .util .Properties ;
17+ import java .util .Optional ;
18+ import lombok .RequiredArgsConstructor ;
1319import lombok .extern .slf4j .Slf4j ;
1420import org .apache .commons .lang3 .StringUtils ;
1521import org .apache .commons .text .StringSubstitutor ;
16- import org .springframework .beans .factory .annotation .Autowired ;
17- import org .springframework .core .env .Environment ;
1822import org .springframework .core .io .ClassPathResource ;
1923import org .springframework .security .web .csrf .CsrfToken ;
2024import org .springframework .util .StreamUtils ;
2327
2428/** @author Andrew Potter */
2529@ Slf4j
30+ @ RequiredArgsConstructor
2631public abstract class GraphiQLController {
2732
2833 private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS = "//cdnjs.cloudflare.com/ajax/libs/" ;
2934 private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/" ;
3035 private static final String GRAPHIQL = "graphiql" ;
3136 private static final String FAVICON_GRAPHQL_ORG = "//graphql.org/img/favicon.png" ;
3237
33- @ Autowired private Environment environment ;
34-
35- @ Autowired private GraphiQLProperties graphiQLProperties ;
38+ private final GraphiQLProperties graphiQLProperties ;
3639
3740 private String template ;
3841 private String props ;
39- private Properties headerProperties ;
4042
4143 public void onceConstructed () throws IOException {
4244 loadTemplate ();
4345 loadProps ();
44- loadHeaders ();
4546 }
4647
4748 private void loadTemplate () throws IOException {
@@ -52,35 +53,45 @@ private void loadTemplate() throws IOException {
5253 }
5354
5455 private void loadProps () throws IOException {
55- props =
56- new PropsLoader (environment , "graphiql.props.resources." , "graphiql.props.variables." )
57- .load ();
58- }
59-
60- private void loadHeaders () {
61- PropertyGroupReader propertyReader = new PropertyGroupReader (environment , "graphiql.headers." );
62- headerProperties = propertyReader .load ();
56+ Resources resources = graphiQLProperties .getProps ().getResources ();
57+ GraphiQLVariables combinedVariables = graphiQLProperties .getProps ().getVariables ();
58+ if (nonNull (resources .getVariables ())) {
59+ combinedVariables = combinedVariables .withVariables (getContent (resources .getVariables ()));
60+ }
61+ if (nonNull (resources .getDefaultQuery ())) {
62+ combinedVariables
63+ = combinedVariables .withDefaultQuery (getContent (resources .getDefaultQuery ()));
64+ }
65+ if (nonNull (resources .getQuery ())) {
66+ combinedVariables = combinedVariables .withQuery (getContent (resources .getQuery ()));
67+ }
68+ this .props = new ObjectMapper ().writeValueAsString (combinedVariables );
6369 }
6470
6571 public byte [] graphiql (
6672 String contextPath , @ PathVariable Map <String , String > params , Object csrf ) {
73+ Map <String , String > finalHeaders = Optional .ofNullable (graphiQLProperties .getHeaders ())
74+ .orElseGet (Collections ::emptyMap );
6775 if (csrf != null ) {
6876 CsrfToken csrfToken = (CsrfToken ) csrf ;
69- headerProperties .setProperty (csrfToken .getHeaderName (), csrfToken .getToken ());
77+ finalHeaders = new HashMap <>(finalHeaders );
78+ finalHeaders .put (csrfToken .getHeaderName (), csrfToken .getToken ());
7079 }
7180
7281 Map <String , String > replacements =
7382 getReplacements (
7483 constructGraphQlEndpoint (contextPath , params ),
7584 contextPath + graphiQLProperties .getEndpoint ().getSubscriptions (),
76- contextPath + graphiQLProperties .getBasePath ());
85+ contextPath + graphiQLProperties .getBasePath (),
86+ finalHeaders );
7787
7888 String populatedTemplate = StringSubstitutor .replace (template , replacements );
7989 return populatedTemplate .getBytes (Charset .defaultCharset ());
8090 }
8191
8292 private Map <String , String > getReplacements (
83- String graphqlEndpoint , String subscriptionsEndpoint , String staticBasePath ) {
93+ String graphqlEndpoint , String subscriptionsEndpoint , String staticBasePath ,
94+ Map <String , String > headers ) {
8495 Map <String , String > replacements = new HashMap <>();
8596 replacements .put ("graphqlEndpoint" , graphqlEndpoint );
8697 replacements .put ("subscriptionsEndpoint" , subscriptionsEndpoint );
@@ -137,7 +148,7 @@ private Map<String, String> getReplacements(
137148 joinJsDelivrPath ("graphiql-subscriptions-fetcher" , "0.0.2" , "browser/client.js" )));
138149 replacements .put ("props" , props );
139150 try {
140- replacements .put ("headers" , new ObjectMapper ().writeValueAsString (headerProperties ));
151+ replacements .put ("headers" , new ObjectMapper ().writeValueAsString (headers ));
141152 } catch (JsonProcessingException e ) {
142153 log .error ("Cannot serialize headers" , e );
143154 }
@@ -191,4 +202,8 @@ private String constructGraphQlEndpoint(
191202 }
192203 return endpoint ;
193204 }
205+
206+ private String getContent (final ClassPathResource resource ) throws IOException {
207+ return new String (Files .readAllBytes (resource .getFile ().toPath ()), StandardCharsets .UTF_8 );
208+ }
194209}
0 commit comments