BAEL-1027: Introduction to GraphQL - initial commit (#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
This commit is contained in:
parent
904a740643
commit
8da9bee6aa
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"query": "mutation($name: String! $email: String! $age: String! ){ createUser ( name: $name email: $email age: $age) { id name email age } }",
|
||||
"parameters": {
|
||||
"name": "John",
|
||||
"email": "john@email.com",
|
||||
"age": 34
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"query": "mutation($name: String! ){ deleteUser ( id: $id) { id name email age} }",
|
||||
"parameters": {
|
||||
"id": 2
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"query": "{ listUsers{ id name email age}}",
|
||||
"parameters": {}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"query": "query($id: String!){ retrieveUser ( id: $id) { id name email} }",
|
||||
"parameters": {
|
||||
"id": 1
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"query": "query($id: String!){ searchName ( id: $id) { id name email} }",
|
||||
"parameters": {
|
||||
"id": 2
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"query": "mutation($id: String! $name: String! $email: String! $age: String! ){ updateUser ( id: $id name: $name email: $email age: $age) { id name email age} }",
|
||||
"parameters": {
|
||||
"id": 1,
|
||||
"name":"John updated",
|
||||
"email": "johnupdate@email.com",
|
||||
"age": 50
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.graphql</groupId>
|
||||
<artifactId>graphql-java</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<name>graphql-java</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-annotations</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-core</artifactId>
|
||||
<version>1.4.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.graphql;
|
||||
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
import com.baeldung.graphql.handler.UserHandler;
|
||||
|
||||
public class Application {
|
||||
public static void main(String[] args) throws Exception {
|
||||
new Application();
|
||||
}
|
||||
|
||||
private Application() throws Exception {
|
||||
final RatpackServer server = RatpackServer.of(s -> s.handlers(chain -> chain.post("users", new UserHandler())));
|
||||
server.start();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.baeldung.graphql.entity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.graphql.handler.UserHandler;
|
||||
import com.baeldung.graphql.utils.SchemaUtils;
|
||||
|
||||
import graphql.annotations.GraphQLField;
|
||||
import graphql.annotations.GraphQLName;
|
||||
|
||||
@GraphQLName(SchemaUtils.USER)
|
||||
public class User {
|
||||
|
||||
@GraphQLField
|
||||
private Long id;
|
||||
@GraphQLField
|
||||
private String name;
|
||||
@GraphQLField
|
||||
private String email;
|
||||
@GraphQLField
|
||||
private Integer age;
|
||||
|
||||
public User(String name, String email, Integer age) {
|
||||
this.id = genId();
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public static Long genId() {
|
||||
Long id = 1L;
|
||||
try {
|
||||
List<User> users = new UserHandler().getUsers();
|
||||
for (User user : users)
|
||||
id = (user.getId() > id ? user.getId() : id) + 1;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "[id=" + id + ", name=" + name + ", email="+email+ ", age="+ age +"]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.graphql.handler;
|
||||
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import graphql.schema.DataFetchingEnvironment;
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.baeldung.graphql.entity.User;
|
||||
import com.baeldung.graphql.schema.UserSchema;
|
||||
import com.baeldung.graphql.utils.SchemaUtils;
|
||||
|
||||
import static ratpack.jackson.Jackson.json;
|
||||
|
||||
public class UserHandler implements Handler {
|
||||
private static final Logger LOGGER = Logger.getLogger(UserHandler.class.getSimpleName());
|
||||
private GraphQL graphql;
|
||||
private static List<User> users = new ArrayList<>();
|
||||
|
||||
public UserHandler() throws Exception {
|
||||
graphql = new GraphQL(new UserSchema().getSchema());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(Context context) throws Exception {
|
||||
context.parse(Map.class)
|
||||
.then(payload -> {
|
||||
Map<String, Object> parameters = (Map<String, Object>) payload.get("parameters");
|
||||
ExecutionResult executionResult = graphql.execute(payload.get(SchemaUtils.QUERY)
|
||||
.toString(), null, this, parameters);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
if (executionResult.getErrors()
|
||||
.isEmpty()) {
|
||||
result.put(SchemaUtils.DATA, executionResult.getData());
|
||||
} else {
|
||||
result.put(SchemaUtils.ERRORS, executionResult.getErrors());
|
||||
LOGGER.warning("Errors: " + executionResult.getErrors());
|
||||
}
|
||||
context.render(json(result));
|
||||
});
|
||||
}
|
||||
|
||||
public static List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public static List<User> getUsers(DataFetchingEnvironment env) {
|
||||
return ((UserHandler) env.getSource()).getUsers();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.graphql.mutation;
|
||||
|
||||
import graphql.annotations.GraphQLField;
|
||||
import graphql.annotations.GraphQLName;
|
||||
import graphql.schema.DataFetchingEnvironment;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import com.baeldung.graphql.entity.User;
|
||||
import com.baeldung.graphql.utils.SchemaUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.baeldung.graphql.handler.UserHandler.getUsers;
|
||||
|
||||
@GraphQLName(SchemaUtils.MUTATION)
|
||||
public class UserMutation {
|
||||
@GraphQLField
|
||||
public static User createUser(final DataFetchingEnvironment env, @NotNull @GraphQLName(SchemaUtils.NAME) final String name, @NotNull @GraphQLName(SchemaUtils.EMAIL) final String email, @NotNull @GraphQLName(SchemaUtils.AGE) final String age) {
|
||||
List<User> users = getUsers(env);
|
||||
final User user = new User(name, email, Integer.valueOf(age));
|
||||
users.add(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@GraphQLField
|
||||
public static User updateUser(final DataFetchingEnvironment env, @NotNull @GraphQLName(SchemaUtils.ID) final String id, @NotNull @GraphQLName(SchemaUtils.NAME) final String name, @NotNull @GraphQLName(SchemaUtils.EMAIL) final String email,
|
||||
@NotNull @GraphQLName(SchemaUtils.AGE) final String age) {
|
||||
final Optional<User> user = getUsers(env).stream()
|
||||
.filter(c -> c.getId() == Long.parseLong(id))
|
||||
.findFirst();
|
||||
if (!user.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
user.get()
|
||||
.setName(name);
|
||||
user.get()
|
||||
.setEmail(email);
|
||||
user.get()
|
||||
.setAge(Integer.valueOf(age));
|
||||
return user.get();
|
||||
}
|
||||
|
||||
@GraphQLField
|
||||
public static User deleteUser(final DataFetchingEnvironment env, @NotNull @GraphQLName(SchemaUtils.ID) final String id) {
|
||||
final List<User> users = getUsers(env);
|
||||
final Optional<User> user = users.stream()
|
||||
.filter(c -> c.getId() == Long.parseLong(id))
|
||||
.findFirst();
|
||||
if (!user.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
users.removeIf(c -> c.getId() == Long.parseLong(id));
|
||||
return user.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.graphql.query;
|
||||
|
||||
import graphql.annotations.GraphQLField;
|
||||
import graphql.annotations.GraphQLName;
|
||||
import graphql.schema.DataFetchingEnvironment;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import com.baeldung.graphql.entity.User;
|
||||
import com.baeldung.graphql.utils.SchemaUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.baeldung.graphql.handler.UserHandler.getUsers;
|
||||
|
||||
@GraphQLName(SchemaUtils.QUERY)
|
||||
public class UserQuery {
|
||||
|
||||
@GraphQLField
|
||||
public static User retrieveUser(final DataFetchingEnvironment env, @NotNull @GraphQLName(SchemaUtils.ID) final String id) {
|
||||
final Optional<User> any = getUsers(env).stream()
|
||||
.filter(c -> c.getId() == Long.parseLong(id))
|
||||
.findFirst();
|
||||
return any.orElse(null);
|
||||
}
|
||||
|
||||
@GraphQLField
|
||||
public static List<User> searchName(final DataFetchingEnvironment env, @NotNull @GraphQLName(SchemaUtils.NAME) final String name) {
|
||||
return getUsers(env).stream()
|
||||
.filter(c -> c.getName()
|
||||
.contains(name))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@GraphQLField
|
||||
public static List<User> listUsers(final DataFetchingEnvironment env) {
|
||||
return getUsers(env);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.graphql.schema;
|
||||
|
||||
import graphql.annotations.GraphQLAnnotations;
|
||||
import graphql.schema.GraphQLSchema;
|
||||
|
||||
import static graphql.schema.GraphQLSchema.newSchema;
|
||||
|
||||
import com.baeldung.graphql.mutation.UserMutation;
|
||||
import com.baeldung.graphql.query.UserQuery;
|
||||
|
||||
public class UserSchema {
|
||||
|
||||
private final GraphQLSchema schema;
|
||||
|
||||
public UserSchema() throws IllegalAccessException, NoSuchMethodException, InstantiationException {
|
||||
schema = newSchema().query(GraphQLAnnotations.object(UserQuery.class))
|
||||
.mutation(GraphQLAnnotations.object(UserMutation.class))
|
||||
.build();
|
||||
}
|
||||
|
||||
public GraphQLSchema getSchema() {
|
||||
return schema;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.graphql.utils;
|
||||
|
||||
public class SchemaUtils {
|
||||
public static final String USER = "user";
|
||||
public static final String ID = "id";
|
||||
public static final String NAME = "name";
|
||||
public static final String EMAIL = "email";
|
||||
public static final String AGE = "age";
|
||||
|
||||
public static final String MUTATION = "mutation";
|
||||
public static final String QUERY = "query";
|
||||
public static final String ERRORS = "errors";
|
||||
public static final String DATA = "data";
|
||||
}
|
Loading…
Reference in New Issue