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

Commit 879f91d

Browse files
shaileshmishrashaileshmishra
authored andcommitted
🎉 initial commit
0 parents  commit 879f91d

File tree

3 files changed

+348
-0
lines changed

3 files changed

+348
-0
lines changed

pom.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.4.4</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>com.contentstack</groupId>
13+
<artifactId>springboot-test</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>springboot-test</name>
16+
<description>Demo project for cspring boot</description>
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-rest</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-jersey</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web-services</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-devtools</artifactId>
46+
<scope>runtime</scope>
47+
<optional>true</optional>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.projectlombok</groupId>
51+
<artifactId>lombok</artifactId>
52+
<optional>true</optional>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.hibernate</groupId>
61+
<artifactId>hibernate-core</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.graphql-java</groupId>
65+
<artifactId>graphql-spring-boot-autoconfigure</artifactId>
66+
<version>5.0.2</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.graphql-java</groupId>
70+
<artifactId>graphql-java-servlet</artifactId>
71+
<version>6.1.3</version>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-maven-plugin</artifactId>
80+
<configuration>
81+
<excludes>
82+
<exclude>
83+
<groupId>org.projectlombok</groupId>
84+
<artifactId>lombok</artifactId>
85+
</exclude>
86+
</excludes>
87+
</configuration>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
92+
</project>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.contentstack.gqlspring;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import net.minidev.json.JSONObject;
8+
import org.springframework.http.*;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
final class GQL {
16+
17+
private final int id;
18+
private final String tag;
19+
private final String[] nodeSplitter;
20+
private final String url;
21+
private final JSONObject queryJson;
22+
private final HttpHeaders headers;
23+
24+
public GQL(Builder builder) {
25+
this.id = builder.id;
26+
this.tag = builder.tag;
27+
this.nodeSplitter = builder.nodeSplitter;
28+
this.url = builder.url;
29+
this.queryJson = builder.queryJson;
30+
this.headers = builder.headers;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "GQL{" +
36+
"id=" + id +
37+
", tag='" + tag + '\'' +
38+
", nodeSplitter=" + Arrays.toString(nodeSplitter) +
39+
", url='" + url + '\'' +
40+
", queryJson=" + queryJson +
41+
", headers=" + headers +
42+
'}';
43+
}
44+
45+
public JsonNode fetch() throws JsonProcessingException {
46+
RestTemplate restTemplate = new RestTemplate();
47+
HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(queryJson, this.headers);
48+
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
49+
HttpStatus statusCode = responseEntity.getStatusCode();
50+
if (statusCode == HttpStatus.OK) {
51+
String dataString = responseEntity.getBody();
52+
ObjectMapper mapper = new ObjectMapper();
53+
return mapper.readTree(dataString);
54+
} else {
55+
String dataString = responseEntity.getBody();
56+
ObjectMapper mapper = new ObjectMapper();
57+
return mapper.readTree(dataString);
58+
}
59+
}
60+
61+
public <T> void modelIt(String responseString) {
62+
try {
63+
ObjectMapper mapper = new ObjectMapper();
64+
JsonNode actualObj = mapper.readTree(responseString);
65+
JsonNode items = actualObj.get("data").get("all_product").get("items");
66+
List<Product> listProduct = mapper.readValue(items.toString(),
67+
new TypeReference<List<Product>>() {
68+
});
69+
System.out.println("listProduct => " + listProduct);
70+
} catch (JsonProcessingException e) {
71+
e.printStackTrace();
72+
}
73+
}
74+
75+
public static class Builder {
76+
77+
private int id;
78+
private String tag;
79+
private String url;
80+
private JSONObject queryJson = new JSONObject();
81+
private HttpHeaders headers = new HttpHeaders();
82+
private String[] nodeSplitter = {};
83+
84+
private Builder() {
85+
}
86+
87+
public static Builder newInstance() {
88+
return new Builder();
89+
}
90+
91+
public Builder setId(int id) {
92+
this.id = id;
93+
return this;
94+
}
95+
96+
public Builder setTag(String tag) {
97+
this.tag = tag;
98+
return this;
99+
}
100+
101+
public Builder setNode(String node) {
102+
if (node != null && !node.isEmpty()) {
103+
this.nodeSplitter = node.split(":");
104+
}
105+
List<String> targetList = Arrays.asList(this.nodeSplitter);
106+
for (String nodeIndex : targetList) {
107+
System.out.println("nodes: => " + nodeIndex);
108+
}
109+
return this;
110+
}
111+
112+
public Builder setUrl(String url) {
113+
this.url = url;
114+
return this;
115+
}
116+
117+
public Builder setQueryString(String queryString) {
118+
JSONObject jsonObject = new JSONObject();
119+
jsonObject.appendField("query", queryString);
120+
this.queryJson = jsonObject;
121+
return this;
122+
}
123+
124+
public Builder setHeader(String access_token) {
125+
final HttpHeaders headers = new HttpHeaders();
126+
ArrayList<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
127+
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
128+
headers.setAccept(acceptableMediaTypes);
129+
headers.add("access_token", access_token);
130+
this.headers = headers;
131+
return this;
132+
}
133+
134+
public GQL build() {
135+
// Do some validation part if required values are not provided
136+
// if (url.isEmpty()){
137+
// throw new Exception("Can not work without url");
138+
// }
139+
return new GQL(this);
140+
}
141+
142+
}
143+
144+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.contentstack.gqlspring;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
9+
import org.springframework.validation.annotation.Validated;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@SpringBootApplication
15+
@RestController
16+
@Slf4j
17+
@Validated
18+
@RequestMapping(path = "api")
19+
public class GQLTestApplication {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(GQLTestApplication.class, args);
23+
}
24+
25+
26+
@GetMapping(value = "/product")
27+
public JsonNode getProducts() throws JsonProcessingException {
28+
GQL gqlInstance = GQL.Builder.newInstance()
29+
.setUrl("https://graphql.contentstack.com/stacks/blt02f7b45378b008ee?environment=production")
30+
.setQueryString("{ all_product { items { title description } } }")
31+
.setHeader("cs5b69faf35efdebd91d08bcf4")
32+
.build();
33+
JsonNode response = gqlInstance.fetch().get("data").get("all_product").get("items");;
34+
System.out.println(response);
35+
return response;
36+
}
37+
38+
39+
@GetMapping(value = "/about")
40+
public JsonNode getAbout() throws JsonProcessingException {
41+
GQL gqlInstance = GQL.Builder.newInstance()
42+
.setTag("about")
43+
.setUrl("https://graphql.contentstack.com/stacks/blt398b654a8f2799a0?environment=development")
44+
.setQueryString("query {\n" +
45+
" home(uid: \"blt2fbdb4f2f324d8cb\") {\n" +
46+
" about {\n" +
47+
" title\n" +
48+
" left_description\n" +
49+
" right_description\n" +
50+
" }\n" +
51+
" }\n" +
52+
"}")
53+
.setHeader("cs5af301973f1478fa4eaca0b2")
54+
.build();
55+
JsonNode response = gqlInstance.fetch();
56+
System.out.println(response);
57+
return response;
58+
}
59+
60+
61+
@GetMapping(value = "/banner")
62+
public JsonNode getBanner() throws JsonProcessingException {
63+
GQL gqlInstance = GQL.Builder.newInstance()
64+
.setTag("banner")
65+
.setUrl("https://graphql.contentstack.com/stacks/blt398b654a8f2799a0?environment=development")
66+
.setQueryString("query {\n" +
67+
" home(uid: \"blt2fbdb4f2f324d8cb\") {\n" +
68+
" banner {\n" +
69+
" description\n" +
70+
" title\n" +
71+
" imageConnection {\n" +
72+
" edges {\n" +
73+
" node {\n" +
74+
" url\n" +
75+
" }\n" +
76+
" }\n" +
77+
" }\n" +
78+
" }\n" +
79+
" }\n" +
80+
"}\n")
81+
.setHeader("cs5af301973f1478fa4eaca0b2")
82+
.build();
83+
JsonNode response = gqlInstance.fetch();
84+
System.out.println(response);
85+
return response;
86+
}
87+
88+
89+
@GetMapping(value = "/contact")
90+
public JsonNode getContact() throws JsonProcessingException {
91+
GQL gqlInstance = GQL.Builder.newInstance()
92+
.setTag("banner")
93+
.setUrl("https://graphql.contentstack.com/stacks/blt398b654a8f2799a0?environment=development")
94+
.setQueryString("query {\n" +
95+
" home(uid: \"blt2fbdb4f2f324d8cb\") {\n" +
96+
" contact {\n" +
97+
" address\n" +
98+
" email\n" +
99+
" phone_number\n" +
100+
" title\n" +
101+
" }\n" +
102+
" }\n" +
103+
"}")
104+
.setHeader("cs5af301973f1478fa4eaca0b2")
105+
.build();
106+
JsonNode response = gqlInstance.fetch();
107+
System.out.println(response);
108+
return response;
109+
}
110+
111+
112+
}

0 commit comments

Comments
 (0)