11package com .contentstack .gqlspring ;
22
3+ import com .contentstack .gqlspring .models .FooterModel ;
34import com .fasterxml .jackson .core .JsonProcessingException ;
5+ import com .fasterxml .jackson .core .type .TypeReference ;
46import com .fasterxml .jackson .databind .JsonNode ;
7+ import com .fasterxml .jackson .databind .ObjectMapper ;
58import io .github .cdimascio .dotenv .Dotenv ;
6- import lombok .extern .slf4j .Slf4j ;
79import org .springframework .boot .SpringApplication ;
810import org .springframework .boot .autoconfigure .SpringBootApplication ;
9- import org .springframework .validation . annotation . Validated ;
11+ import org .springframework .ui . Model ;
1012import org .springframework .web .bind .annotation .GetMapping ;
11- import org .springframework .web .bind .annotation .RequestMapping ;
1213import org .springframework .web .bind .annotation .RestController ;
1314
15+ import java .util .List ;
16+
1417/**
1518 * The type Gql test application.
1619 */
1720@ SpringBootApplication
1821@ RestController
19- @ Slf4j
20- @ Validated
21- @ RequestMapping (path = "api" )
22- public class GQLTestApplication {
22+ //@Slf4j
23+ //@Validated
24+ public class MainApplication {
2325
2426 private static String BASE_URL ;
2527 private static String DeliverToken ;
2628
29+
30+ // load credentials from from .env
31+ private static void loadEnvVar () {
32+ Dotenv dotenv = Dotenv .load ();
33+ DeliverToken = dotenv .get ("_EVV_DELIVERY_TOKEN" );
34+ String _API_KEY = dotenv .get ("_ENV_API_KEY" );
35+ String _ENV = dotenv .get ("_ENV" );
36+ BASE_URL = "https://graphql.contentstack.com/stacks/" + _API_KEY + "?environment=" + _ENV ;
37+ }
38+
2739 /**
2840 * The entry point of application.
2941 *
3042 * @param args the input arguments
3143 */
3244 public static void main (String [] args ) {
3345 loadEnvVar ();
34- SpringApplication .run (GQLTestApplication .class , args );
46+ SpringApplication .run (MainApplication .class , args );
3547 }
3648
3749
38- // load credentials from from .env
39- private static void loadEnvVar () {
40- Dotenv dotenv = Dotenv .load ();
41- DeliverToken = dotenv .get ("_EVV_DELIVERY_TOKEN" );
42- String _API_KEY = dotenv .get ("_ENV_API_KEY" );
43- String _ENV = dotenv .get ("_ENV" );
44- BASE_URL = "https://graphql.contentstack.com/stacks/" + _API_KEY + "?environment=" + _ENV ;
50+ /**
51+ * loadIndexPage will load default html page
52+ * as its not specifies any name in Get Mapping
53+ *
54+ * @return the loadIndexPage
55+ * @throws JsonProcessingException the json processing exception
56+ */
57+ @ GetMapping ("/" )
58+ public String loadIndexPage (Model model ) throws JsonProcessingException {
59+ GQLWrapper gqlInstance = GQLWrapper .Builder .newInstance ()
60+ .setTag ("all_footer" )
61+ .setUrl (BASE_URL )
62+ .setQueryString ("{\n " +
63+ " all_footer {\n " +
64+ " items {\n " +
65+ " title\n " +
66+ " }\n " +
67+ " }\n " +
68+ "}" )
69+ .setHeader (DeliverToken )
70+ .build ();
71+ JsonNode response = gqlInstance .fetch ().get ("data" ).get ("all_footer" ).get ("items" );
72+ System .out .println (response );
73+ // ObjectMapper mapper = new ObjectMapper();
74+ // List<FooterModel> footerModels = mapper.readValue(response.toString(),
75+ // new TypeReference<List<FooterModel>>() {
76+ // });
77+ // System.out.println(footerModels);
78+ model .addAttribute ("index" , response );
79+ return "index" ;
4580 }
4681
4782
@@ -52,8 +87,8 @@ private static void loadEnvVar() {
5287 * @throws JsonProcessingException the json processing exception
5388 */
5489 @ GetMapping (value = "/all_blog_post" )
55- public JsonNode getProducts () throws JsonProcessingException {
56- GQL gqlInstance = GQL .Builder .newInstance ()
90+ public String getProducts (Model model ) throws JsonProcessingException {
91+ GQLWrapper gqlInstance = GQLWrapper .Builder .newInstance ()
5792 .setUrl (BASE_URL )
5893 .setQueryString ("query{\n " +
5994 " all_blog_post {\n " +
@@ -68,8 +103,8 @@ public JsonNode getProducts() throws JsonProcessingException {
68103 .setHeader (DeliverToken )
69104 .build ();
70105 JsonNode response = gqlInstance .fetch ().get ("data" ).get ("all_blog_post" ).get ("items" );
71- System . out . println ( response );
72- return response ;
106+ model . addAttribute ( "blog" , response );
107+ return "blog" ;
73108 }
74109
75110
@@ -80,8 +115,8 @@ public JsonNode getProducts() throws JsonProcessingException {
80115 * @throws JsonProcessingException the json processing exception
81116 */
82117 @ GetMapping (value = "/all_footer" )
83- public JsonNode getAbout () throws JsonProcessingException {
84- GQL gqlInstance = GQL .Builder .newInstance ()
118+ public JsonNode getAbout (Model model ) throws JsonProcessingException {
119+ GQLWrapper gqlInstance = GQLWrapper .Builder .newInstance ()
85120 .setTag ("all_footer" )
86121 .setUrl (BASE_URL )
87122 .setQueryString ("{\n " +
@@ -94,7 +129,13 @@ public JsonNode getAbout() throws JsonProcessingException {
94129 .setHeader (DeliverToken )
95130 .build ();
96131 JsonNode response = gqlInstance .fetch ();
97- System .out .println (response );
132+
133+ ObjectMapper mapper = new ObjectMapper ();
134+ List <FooterModel > footerModels = mapper .readValue (response .toString (),
135+ new TypeReference <List <FooterModel >>() {
136+ });
137+
138+ model .addAttribute (footerModels );
98139 return response ;
99140 }
100141
@@ -107,13 +148,21 @@ public JsonNode getAbout() throws JsonProcessingException {
107148 */
108149 @ GetMapping (value = "/all_header" )
109150 public JsonNode getBanner () throws JsonProcessingException {
110- GQL gqlInstance = GQL .Builder .newInstance ()
151+ GQLWrapper gqlInstance = GQLWrapper .Builder .newInstance ()
111152 .setTag ("all_header" )
112153 .setUrl (BASE_URL )
113154 .setQueryString ("{\n " +
114155 " all_header {\n " +
115156 " items {\n " +
116157 " title\n " +
158+ " logoConnection {\n " +
159+ " edges {\n " +
160+ " node {\n " +
161+ " title\n " +
162+ " url\n " +
163+ " }\n " +
164+ " }\n " +
165+ " }\n " +
117166 " }\n " +
118167 " }\n " +
119168 "}" )
@@ -133,7 +182,7 @@ public JsonNode getBanner() throws JsonProcessingException {
133182 */
134183 @ GetMapping (value = "/all_page" )
135184 public JsonNode getContact () throws JsonProcessingException {
136- GQL gqlInstance = GQL .Builder .newInstance ()
185+ GQLWrapper gqlInstance = GQLWrapper .Builder .newInstance ()
137186 .setTag ("all_page" )
138187 .setUrl (BASE_URL )
139188 .setQueryString ("{\n " +
0 commit comments