Skip to content

Commit 164de4e

Browse files
committed
init commit
0 parents  commit 164de4e

File tree

11 files changed

+351
-0
lines changed

11 files changed

+351
-0
lines changed

pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.1.2</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.graphql</groupId>
12+
<artifactId>demo</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>demo</name>
15+
<description>Demo project for GraphQl Spring Boot</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-graphql</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-jdbc</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.postgresql</groupId>
35+
<artifactId>postgresql</artifactId>
36+
37+
</dependency>
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.graphql-java-kickstart</groupId>
50+
<artifactId>graphql-spring-boot-starter</artifactId>
51+
<version>15.0.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.graphql-java-kickstart</groupId>
55+
<artifactId>graphql-java-tools</artifactId>
56+
<version>13.0.3</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.postgresql</groupId>
60+
<artifactId>postgresql</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-jdbc</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework.graphql</groupId>
68+
<artifactId>spring-graphql-test</artifactId>
69+
<scope>test</scope>
70+
</dependency>
71+
</dependencies>
72+
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-maven-plugin</artifactId>
78+
<configuration>
79+
<excludes>
80+
<exclude>
81+
<groupId>org.projectlombok</groupId>
82+
<artifactId>lombok</artifactId>
83+
</exclude>
84+
</excludes>
85+
</configuration>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
90+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.graphql.demo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class Book {
9+
10+
private Long id;
11+
private String title;
12+
private String author;
13+
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.graphql.demo;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.stereotype.Component;
8+
9+
import graphql.kickstart.tools.GraphQLQueryResolver;
10+
import graphql.schema.DataFetchingEnvironment;
11+
12+
@Component
13+
public class BookQueryResolver implements GraphQLQueryResolver {
14+
15+
@Autowired
16+
private JdbcTemplate jdbcTemplate;
17+
18+
public List<Book> books(DataFetchingEnvironment dataFetchingEnvironment)
19+
{
20+
try{
21+
return jdbcTemplate.query("SELECT * FROM public.books",(rs,rowNum) -> new Book(
22+
rs.getLong("id"),
23+
rs.getString("title"),
24+
rs.getString("author")
25+
));
26+
}
27+
catch(Exception e )
28+
{
29+
System.out.println("Error in books");
30+
throw new CustomGraphQLException("An error occured while fetching books");
31+
}
32+
}
33+
34+
35+
36+
37+
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.graphql.demo;
2+
3+
public class CustomGraphQLException extends RuntimeException {
4+
5+
public CustomGraphQLException(String message)
6+
{
7+
super(message);
8+
}
9+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.graphql.demo;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpMethod;
7+
8+
import graphql.ExecutionResult;
9+
import graphql.GraphQL;
10+
import graphql.kickstart.servlet.GraphQLConfiguration;
11+
import graphql.kickstart.servlet.GraphQLHttpServlet;
12+
import graphql.schema.GraphQLSchema;
13+
import jakarta.servlet.http.HttpServletRequest;
14+
import jakarta.servlet.http.HttpServletResponse;
15+
16+
public class CustomGraphQLHttpServlet extends GraphQLHttpServlet {
17+
18+
@Autowired
19+
private GraphQLSchema graphQLSchema;
20+
21+
22+
@Override
23+
protected GraphQLConfiguration getConfiguration() {
24+
// TODO Auto-generated method stub
25+
System.out.println("printing graphql schema:"+graphQLSchema.getQueryType().getName());
26+
return GraphQLConfiguration.with(graphQLSchema).build();
27+
}
28+
29+
@Override
30+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
31+
CustomGraphQLResponseWrapper responseWrapper = new CustomGraphQLResponseWrapper(resp);
32+
super.doGet(req, responseWrapper);
33+
34+
String responseBody = responseWrapper.getCapturedResponse();
35+
if (responseContainsError(responseBody)) {
36+
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // Or any other status code you deem appropriate
37+
}
38+
39+
try {
40+
resp.getWriter().write(responseBody);
41+
} catch (IOException e) {
42+
// TODO Auto-generated catch block
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
@Override
48+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
49+
CustomGraphQLResponseWrapper responseWrapper = new CustomGraphQLResponseWrapper(resp);
50+
super.doPost(req, responseWrapper);
51+
52+
String responseBody = responseWrapper.getCapturedResponse();
53+
if (responseContainsError(responseBody)) {
54+
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // Or any other status code you deem appropriate
55+
}
56+
57+
try {
58+
resp.getWriter().write(responseBody);
59+
} catch (IOException e) {
60+
// TODO Auto-generated catch block
61+
e.printStackTrace();
62+
}
63+
}
64+
65+
private boolean responseContainsError(String responseBody) {
66+
67+
68+
// Here, you can inspect the response and set the status code based on the GraphQL response.
69+
// For example, if you want to set a 400 status code for a specific error:
70+
return responseBody.contains("\"errors\"");
71+
72+
73+
74+
}
75+
76+
77+
// ... other necessary overrides, if any
78+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.graphql.demo;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStreamWriter;
6+
import java.io.PrintWriter;
7+
8+
import jakarta.servlet.ServletOutputStream;
9+
import jakarta.servlet.WriteListener;
10+
import jakarta.servlet.http.HttpServletResponse;
11+
import jakarta.servlet.http.HttpServletResponseWrapper;
12+
13+
public class CustomGraphQLResponseWrapper extends HttpServletResponseWrapper {
14+
15+
public CustomGraphQLResponseWrapper(HttpServletResponse response) {
16+
super(response);
17+
//TODO Auto-generated constructor stub
18+
}
19+
20+
private final ByteArrayOutputStream capture = new ByteArrayOutputStream();
21+
private final ServletOutputStream output = new CaptureServletOutputStream(capture);
22+
private PrintWriter writer;
23+
24+
25+
@Override
26+
public ServletOutputStream getOutputStream() throws IOException {
27+
return output;
28+
}
29+
30+
@Override
31+
public PrintWriter getWriter() throws IOException {
32+
if (writer == null) {
33+
writer = new PrintWriter(new OutputStreamWriter(capture, getCharacterEncoding()));
34+
}
35+
return writer;
36+
}
37+
38+
public String getCapturedResponse() {
39+
return capture.toString();
40+
}
41+
42+
private static class CaptureServletOutputStream extends ServletOutputStream {
43+
private final ByteArrayOutputStream capture;
44+
45+
public CaptureServletOutputStream(ByteArrayOutputStream capture) {
46+
this.capture = capture;
47+
}
48+
49+
@Override
50+
public boolean isReady() {
51+
return true;
52+
}
53+
54+
@Override
55+
public void setWriteListener(WriteListener listener) {
56+
// No-op
57+
}
58+
59+
@Override
60+
public void write(int b) throws IOException {
61+
capture.write(b);
62+
}
63+
}
64+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.graphql.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.graphql.demo;
2+
3+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class GraphQLConfig {
9+
10+
@Bean
11+
public ServletRegistrationBean<CustomGraphQLHttpServlet> graphQLServletRegistrationBean() {
12+
ServletRegistrationBean<CustomGraphQLHttpServlet> registrationBean = new ServletRegistrationBean<>(new CustomGraphQLHttpServlet(), "/graphql");
13+
return registrationBean;
14+
}
15+
16+
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.datasource.url=jdbc:postgresql://localhost:5332/postgres
2+
spring.datssource.username=baeldung
3+
spring.datasource.password=baeldung
4+
graphql.servlet.enabled=false
5+
server.port=8080
6+
graphql.tools.schema-location-pattern=**/graphql/*.graphqls
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Query{
2+
books: [Book]
3+
}
4+
5+
type Book {
6+
id: ID!
7+
title: String!
8+
author: String!
9+
}

0 commit comments

Comments
 (0)