Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit b7324e3

Browse files
shaileshmishrashaileshmishra
authored andcommitted
🔨 refactored credentials
1 parent 49cc2dc commit b7324e3

26 files changed

+10398
-118
lines changed

pom.xml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<relativePath/> <!-- lookup parent from repository -->
1111
</parent>
1212
<groupId>com.contentstack</groupId>
13-
<artifactId>springboot-test</artifactId>
13+
<artifactId>graphql-springboot-api-integration</artifactId>
1414
<version>0.0.1-SNAPSHOT</version>
15-
<name>springboot-test</name>
16-
<description>Demo project for cspring boot</description>
15+
<name>graphql-springboot-api-integration</name>
16+
<description>Demo project for graphql-springboot-api-integration</description>
1717
<properties>
1818
<java.version>1.8</java.version>
1919
</properties>
@@ -31,6 +31,10 @@
3131
<groupId>org.springframework.boot</groupId>
3232
<artifactId>spring-boot-starter-thymeleaf</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-freemarker</artifactId>
37+
</dependency>
3438
<dependency>
3539
<groupId>org.springframework.boot</groupId>
3640
<artifactId>spring-boot-starter-web</artifactId>
@@ -77,6 +81,19 @@
7781
<artifactId>java-dotenv</artifactId>
7882
<version>5.2.2</version>
7983
</dependency>
84+
85+
<!-- <dependency>-->
86+
<!-- <groupId>org.springframework</groupId>-->
87+
<!-- <artifactId>spring-context</artifactId>-->
88+
<!-- <version>5.2.2.RELEASE</version>-->
89+
<!-- </dependency>-->
90+
91+
<!-- <dependency>-->
92+
<!-- <groupId>org.springframework</groupId>-->
93+
<!-- <artifactId>spring-web</artifactId>-->
94+
<!-- <version>5.2.2.RELEASE</version>-->
95+
<!-- </dependency>-->
96+
8097
</dependencies>
8198

8299
<build>

src/main/java/com/contentstack/gqlspring/GQL.java renamed to src/main/java/com/contentstack/gqlspring/GQLWrapper.java

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.contentstack.gqlspring;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.core.type.TypeReference;
54
import com.fasterxml.jackson.databind.JsonNode;
65
import com.fasterxml.jackson.databind.ObjectMapper;
76
import net.minidev.json.JSONObject;
@@ -10,12 +9,11 @@
109

1110
import java.util.ArrayList;
1211
import java.util.Arrays;
13-
import java.util.List;
1412

1513
/**
1614
* The type Gql.
1715
*/
18-
final class GQL {
16+
final class GQLWrapper {
1917

2018
private final int id;
2119
private final String tag;
@@ -29,7 +27,7 @@ final class GQL {
2927
*
3028
* @param builder the builder
3129
*/
32-
public GQL(Builder builder) {
30+
public GQLWrapper(Builder builder) {
3331
this.id = builder.id;
3432
this.tag = builder.tag;
3533
this.nodeSplitter = builder.nodeSplitter;
@@ -58,7 +56,7 @@ public String toString() {
5856
*/
5957
public JsonNode fetch() throws JsonProcessingException {
6058
RestTemplate restTemplate = new RestTemplate();
61-
HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(queryJson, this.headers);
59+
HttpEntity<JSONObject> entity = new HttpEntity<>(queryJson, this.headers);
6260
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
6361
HttpStatus statusCode = responseEntity.getStatusCode();
6462
if (statusCode == HttpStatus.OK) {
@@ -72,25 +70,6 @@ public JsonNode fetch() throws JsonProcessingException {
7270
}
7371
}
7472

75-
/**
76-
* Model it.
77-
*
78-
* @param <T> the type parameter
79-
* @param responseString the response string
80-
*/
81-
public <T> void modelIt(String responseString) {
82-
try {
83-
ObjectMapper mapper = new ObjectMapper();
84-
JsonNode actualObj = mapper.readTree(responseString);
85-
JsonNode items = actualObj.get("data").get("all_product").get("items");
86-
List<Product> listProduct = mapper.readValue(items.toString(),
87-
new TypeReference<List<Product>>() {
88-
});
89-
System.out.println("listProduct => " + listProduct);
90-
} catch (JsonProcessingException e) {
91-
e.printStackTrace();
92-
}
93-
}
9473

9574
/**
9675
* The type Builder.
@@ -102,7 +81,7 @@ public static class Builder {
10281
private String url;
10382
private JSONObject queryJson = new JSONObject();
10483
private HttpHeaders headers = new HttpHeaders();
105-
private String[] nodeSplitter = {};
84+
private final String[] nodeSplitter = {};
10685

10786
private Builder() {
10887
}
@@ -138,22 +117,6 @@ public Builder setTag(String tag) {
138117
return this;
139118
}
140119

141-
/**
142-
* Sets node.
143-
*
144-
* @param node the node
145-
* @return the node
146-
*/
147-
public Builder setNode(String node) {
148-
if (node != null && !node.isEmpty()) {
149-
this.nodeSplitter = node.split(":");
150-
}
151-
List<String> targetList = Arrays.asList(this.nodeSplitter);
152-
for (String nodeIndex : targetList) {
153-
System.out.println("nodes: => " + nodeIndex);
154-
}
155-
return this;
156-
}
157120

158121
/**
159122
* Sets url.
@@ -187,7 +150,7 @@ public Builder setQueryString(String queryString) {
187150
*/
188151
public Builder setHeader(String access_token) {
189152
final HttpHeaders headers = new HttpHeaders();
190-
ArrayList<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
153+
ArrayList<MediaType> acceptableMediaTypes = new ArrayList<>();
191154
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
192155
headers.setAccept(acceptableMediaTypes);
193156
headers.add("access_token", access_token);
@@ -200,12 +163,8 @@ public Builder setHeader(String access_token) {
200163
*
201164
* @return the gql
202165
*/
203-
public GQL build() {
204-
// Do some validation part if required values are not provided
205-
// if (url.isEmpty()){
206-
// throw new Exception("Can not work without url");
207-
// }
208-
return new GQL(this);
166+
public GQLWrapper build() {
167+
return new GQLWrapper(this);
209168
}
210169

211170
}

src/main/java/com/contentstack/gqlspring/GQLTestApplication.java renamed to src/main/java/com/contentstack/gqlspring/MainApplication.java

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,82 @@
11
package com.contentstack.gqlspring;
22

3+
import com.contentstack.gqlspring.models.FooterModel;
34
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.core.type.TypeReference;
46
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
58
import io.github.cdimascio.dotenv.Dotenv;
6-
import lombok.extern.slf4j.Slf4j;
79
import org.springframework.boot.SpringApplication;
810
import org.springframework.boot.autoconfigure.SpringBootApplication;
9-
import org.springframework.validation.annotation.Validated;
11+
import org.springframework.ui.Model;
1012
import org.springframework.web.bind.annotation.GetMapping;
11-
import org.springframework.web.bind.annotation.RequestMapping;
1213
import 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" +

src/main/java/com/contentstack/gqlspring/Product.java renamed to src/main/java/com/contentstack/gqlspring/models/AboutModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.contentstack.gqlspring;
1+
package com.contentstack.gqlspring.models;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import lombok.Data;
@@ -7,7 +7,7 @@
77
* The type Product.
88
*/
99
@Data
10-
public class Product {
10+
public class AboutModel {
1111

1212
/**
1313
* The Title.

0 commit comments

Comments
 (0)