Skip to content

Commit 8da9bee

Browse files
felipeazvmaibin
authored andcommitted
BAEL-1027: Introduction to GraphQL - initial commit (eugenp#2515)
* spring beans DI examples * fix-1: shortening examples * List of Rules Engines in Java * BAEL-812: Openl-Tablets example added * BAEL-812: artifacts names changed * BAEL-812: moving rule-engines examples to rule-engines folder * BAEL-812: removing evaluation article files * BAEL-812: folder renamed * BAEL-812: folder renamed * BAEL-812: pom.xml - parent added * BAEL-1027: Introduction to GraphQL - initial commit
1 parent 904a740 commit 8da9bee

File tree

14 files changed

+356
-0
lines changed

14 files changed

+356
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"query": "mutation($name: String! $email: String! $age: String! ){ createUser ( name: $name email: $email age: $age) { id name email age } }",
3+
"parameters": {
4+
"name": "John",
5+
"email": "john@email.com",
6+
"age": 34
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"query": "mutation($name: String! ){ deleteUser ( id: $id) { id name email age} }",
3+
"parameters": {
4+
"id": 2
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"query": "{ listUsers{ id name email age}}",
3+
"parameters": {}
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"query": "query($id: String!){ retrieveUser ( id: $id) { id name email} }",
3+
"parameters": {
4+
"id": 1
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"query": "query($id: String!){ searchName ( id: $id) { id name email} }",
3+
"parameters": {
4+
"id": 2
5+
}
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": "mutation($id: String! $name: String! $email: String! $age: String! ){ updateUser ( id: $id name: $name email: $email age: $age) { id name email age} }",
3+
"parameters": {
4+
"id": 1,
5+
"name":"John updated",
6+
"email": "johnupdate@email.com",
7+
"age": 50
8+
}
9+
}

graphql/graphql-java/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung.graphql</groupId>
6+
<artifactId>graphql-java</artifactId>
7+
<version>1.0</version>
8+
9+
<name>graphql-java</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.graphql-java</groupId>
20+
<artifactId>graphql-java-annotations</artifactId>
21+
<version>3.0.3</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.ratpack</groupId>
25+
<artifactId>ratpack-core</artifactId>
26+
<version>1.4.6</version>
27+
</dependency>
28+
</dependencies>
29+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.graphql;
2+
3+
import ratpack.server.RatpackServer;
4+
5+
import com.baeldung.graphql.handler.UserHandler;
6+
7+
public class Application {
8+
public static void main(String[] args) throws Exception {
9+
new Application();
10+
}
11+
12+
private Application() throws Exception {
13+
final RatpackServer server = RatpackServer.of(s -> s.handlers(chain -> chain.post("users", new UserHandler())));
14+
server.start();
15+
}
16+
17+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.graphql.entity;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.graphql.handler.UserHandler;
6+
import com.baeldung.graphql.utils.SchemaUtils;
7+
8+
import graphql.annotations.GraphQLField;
9+
import graphql.annotations.GraphQLName;
10+
11+
@GraphQLName(SchemaUtils.USER)
12+
public class User {
13+
14+
@GraphQLField
15+
private Long id;
16+
@GraphQLField
17+
private String name;
18+
@GraphQLField
19+
private String email;
20+
@GraphQLField
21+
private Integer age;
22+
23+
public User(String name, String email, Integer age) {
24+
this.id = genId();
25+
this.name = name;
26+
this.email = email;
27+
this.age = age;
28+
}
29+
30+
public Long getId() {
31+
return id;
32+
}
33+
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setEmail(String email) {
43+
this.email = email;
44+
}
45+
46+
public String getEmail() {
47+
return email;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public Integer getAge() {
55+
return age;
56+
}
57+
58+
public void setAge(Integer age) {
59+
this.age = age;
60+
}
61+
62+
public static Long genId() {
63+
Long id = 1L;
64+
try {
65+
List<User> users = new UserHandler().getUsers();
66+
for (User user : users)
67+
id = (user.getId() > id ? user.getId() : id) + 1;
68+
69+
} catch (Exception e) {
70+
e.printStackTrace();
71+
}
72+
return id;
73+
}
74+
75+
public String toString() {
76+
return "[id=" + id + ", name=" + name + ", email="+email+ ", age="+ age +"]";
77+
}
78+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.graphql.handler;
2+
3+
import graphql.ExecutionResult;
4+
import graphql.GraphQL;
5+
import graphql.schema.DataFetchingEnvironment;
6+
import ratpack.handling.Context;
7+
import ratpack.handling.Handler;
8+
9+
import java.util.ArrayList;
10+
import java.util.LinkedHashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.logging.Logger;
14+
15+
import com.baeldung.graphql.entity.User;
16+
import com.baeldung.graphql.schema.UserSchema;
17+
import com.baeldung.graphql.utils.SchemaUtils;
18+
19+
import static ratpack.jackson.Jackson.json;
20+
21+
public class UserHandler implements Handler {
22+
private static final Logger LOGGER = Logger.getLogger(UserHandler.class.getSimpleName());
23+
private GraphQL graphql;
24+
private static List<User> users = new ArrayList<>();
25+
26+
public UserHandler() throws Exception {
27+
graphql = new GraphQL(new UserSchema().getSchema());
28+
}
29+
30+
@Override
31+
public void handle(Context context) throws Exception {
32+
context.parse(Map.class)
33+
.then(payload -> {
34+
Map<String, Object> parameters = (Map<String, Object>) payload.get("parameters");
35+
ExecutionResult executionResult = graphql.execute(payload.get(SchemaUtils.QUERY)
36+
.toString(), null, this, parameters);
37+
Map<String, Object> result = new LinkedHashMap<>();
38+
if (executionResult.getErrors()
39+
.isEmpty()) {
40+
result.put(SchemaUtils.DATA, executionResult.getData());
41+
} else {
42+
result.put(SchemaUtils.ERRORS, executionResult.getErrors());
43+
LOGGER.warning("Errors: " + executionResult.getErrors());
44+
}
45+
context.render(json(result));
46+
});
47+
}
48+
49+
public static List<User> getUsers() {
50+
return users;
51+
}
52+
53+
public static List<User> getUsers(DataFetchingEnvironment env) {
54+
return ((UserHandler) env.getSource()).getUsers();
55+
}
56+
}

0 commit comments

Comments
 (0)