JAVA-12421 Renamed graphql to graphql-modules
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
## GraphQL Java
|
||||
|
||||
This module contains articles about GraphQL with Java
|
||||
|
||||
## Relevant articles:
|
||||
|
||||
- [Introduction to GraphQL](https://www.baeldung.com/graphql)
|
||||
- [Make a Call to a GraphQL Service from a Java Application](https://www.baeldung.com/java-call-graphql-service)
|
||||
- [Return Map from GraphQL](https://www.baeldung.com/java-graphql-return-map)
|
||||
@@ -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": "[email protected]",
|
||||
"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": "[email protected]",
|
||||
"age": 50
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>graphql-java</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>graphql-java</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.graphql</groupId>
|
||||
<artifactId>graphql-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<id>central</id>
|
||||
<name>Central Repository</name>
|
||||
<url>https://repo.maven.apache.org/maven2</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-annotations</artifactId>
|
||||
<version>${graphql-java-annotations.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-core</artifactId>
|
||||
<version>${ratpack-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.americanexpress.nodes</groupId>
|
||||
<artifactId>nodes</artifactId>
|
||||
<version>${nodes.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java</artifactId>
|
||||
<version>${graphql-java.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-tools</artifactId>
|
||||
<version>${graphql-java-tools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-servlet</artifactId>
|
||||
<version>${graphql-java-servlet.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java-generator</groupId>
|
||||
<artifactId>graphql-java-runtime</artifactId>
|
||||
<version>${graphql.java.generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-netty</artifactId>
|
||||
<version>${mockserver-netty.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-client-java</artifactId>
|
||||
<version>${mockserver-client-java.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-extended-scalars</artifactId>
|
||||
<version>${graphql-java-extended-scalars.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>${jetty-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.graphql-java-generator</groupId>
|
||||
<artifactId>graphql-maven-plugin</artifactId>
|
||||
<version>${graphql.java.generator.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>generateClientCode</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<packageName>com.baeldung.graphql.generated</packageName>
|
||||
<copyRuntimeSources>false</copyRuntimeSources>
|
||||
<generateDeprecatedRequestResponse>false</generateDeprecatedRequestResponse>
|
||||
<separateUtilityClasses>true</separateUtilityClasses>
|
||||
<customScalars>
|
||||
<customScalar>
|
||||
<graphQLTypeName>JSON</graphQLTypeName>
|
||||
<javaType>java.util.Map</javaType>
|
||||
<graphQLScalarTypeClass>com.baeldung.graphqlreturnmap.ExtendedGraphQLScalarType</graphQLScalarTypeClass>
|
||||
</customScalar>
|
||||
</customScalars>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<graphql-java.version>11.0</graphql-java.version>
|
||||
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
|
||||
<graphql-java-servlet.version>6.1.3</graphql-java-servlet.version>
|
||||
<graphql-java-annotations.version>3.0.3</graphql-java-annotations.version>
|
||||
<graphql.java.generator.version>1.18</graphql.java.generator.version>
|
||||
<ratpack-core.version>1.9.0</ratpack-core.version>
|
||||
<nodes.version>0.5.0</nodes.version>
|
||||
<httpclient.version>4.5.13</httpclient.version>
|
||||
<mockserver-netty.version>5.13.2</mockserver-netty.version>
|
||||
<mockserver-client-java.version>5.13.2</mockserver-client-java.version>
|
||||
<jetty-maven-plugin.version>10.0.7</jetty-maven-plugin.version>
|
||||
<graphql.java.generator.version>1.18</graphql.java.generator.version>
|
||||
<graphql-java-extended-scalars.version>2022-04-06T00-10-27-a70541e</graphql-java-extended-scalars.version>
|
||||
</properties>
|
||||
|
||||
</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();
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.graphql.clients;
|
||||
|
||||
import com.baeldung.graphql.data.Data;
|
||||
import io.aexp.nodes.graphql.GraphQLRequestEntity;
|
||||
import io.aexp.nodes.graphql.GraphQLResponseEntity;
|
||||
import io.aexp.nodes.graphql.GraphQLTemplate;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class AmericanExpressNodes {
|
||||
|
||||
public static GraphQLResponseEntity<Data> callGraphQLService(String url, String query) throws IOException {
|
||||
GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
|
||||
|
||||
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
|
||||
.url(StringUtils.join(url, "?query=", query))
|
||||
.request(Data.class)
|
||||
.build();
|
||||
|
||||
return graphQLTemplate.query(requestEntity, Data.class);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.graphql.clients;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class ApacheHttpClient {
|
||||
|
||||
public static HttpResponse callGraphQLService(String url, String query) throws URISyntaxException, IOException {
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(url);
|
||||
URI uri = new URIBuilder(request.getURI())
|
||||
.addParameter("query", query)
|
||||
.build();
|
||||
request.setURI(uri);
|
||||
return client.execute(request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.graphql.data;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class Author {
|
||||
|
||||
private String name;
|
||||
private String surname;
|
||||
|
||||
public Author() {
|
||||
|
||||
}
|
||||
|
||||
public Author(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return StringUtils.join(getName(), " ", getSurname());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.graphql.data;
|
||||
|
||||
public class Book {
|
||||
|
||||
private String title;
|
||||
private Author author;
|
||||
|
||||
public Book() {
|
||||
|
||||
}
|
||||
|
||||
public Book(String title, Author author) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.graphql.data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class BookRepository {
|
||||
|
||||
private static final List<Book> books = Stream.of(
|
||||
new Book("Title 1", new Author("Pero", "Peric")),
|
||||
new Book("Title 2", new Author("Marko", "Maric"))
|
||||
).collect(Collectors.toList());
|
||||
|
||||
public List<Book> getAllBooks() {
|
||||
return Collections.unmodifiableList(books);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.graphql.data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Data {
|
||||
|
||||
private List<Book> allBooks;
|
||||
|
||||
public Data() {
|
||||
|
||||
}
|
||||
|
||||
public Data(List<Book> allBooks) {
|
||||
this.allBooks = allBooks;
|
||||
}
|
||||
|
||||
public List<Book> getAllBooks() {
|
||||
return Collections.unmodifiableList(allBooks);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.graphql.data;
|
||||
|
||||
public class Response {
|
||||
|
||||
private Data data;
|
||||
|
||||
public Response() {
|
||||
|
||||
}
|
||||
|
||||
public Response(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Data getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 +"]";
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
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 static final List<User> USERS = new ArrayList<>();
|
||||
|
||||
private final GraphQL graphql;
|
||||
|
||||
public UserHandler() throws Exception {
|
||||
graphql = GraphQL.newGraphQL(new UserSchema().getSchema()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(Context context) {
|
||||
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 List<User> getUsers() {
|
||||
return USERS;
|
||||
}
|
||||
|
||||
public static List<User> getUsers(DataFetchingEnvironment env) {
|
||||
return ((UserHandler) env.getSource()).getUsers();
|
||||
}
|
||||
}
|
||||
+57
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -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;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.graphql.server;
|
||||
|
||||
import com.baeldung.graphql.data.BookRepository;
|
||||
import com.coxautodev.graphql.tools.SchemaParser;
|
||||
import graphql.schema.GraphQLSchema;
|
||||
import graphql.servlet.SimpleGraphQLHttpServlet;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(urlPatterns = "/graphql")
|
||||
public class GraphQLEndpoint extends HttpServlet {
|
||||
|
||||
private SimpleGraphQLHttpServlet graphQLServlet;
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
graphQLServlet.service(req, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
GraphQLSchema schema = SchemaParser.newParser()
|
||||
.resolvers(new GraphQLQuery(new BookRepository()))
|
||||
.file("schema.graphqls")
|
||||
.build()
|
||||
.makeExecutableSchema();
|
||||
graphQLServlet = SimpleGraphQLHttpServlet
|
||||
.newBuilder(schema)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.graphql.server;
|
||||
|
||||
import com.baeldung.graphql.data.Book;
|
||||
import com.baeldung.graphql.data.BookRepository;
|
||||
import com.baeldung.graphqlreturnmap.entity.Product;
|
||||
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GraphQLQuery implements GraphQLQueryResolver {
|
||||
|
||||
private final BookRepository repository;
|
||||
|
||||
public GraphQLQuery(BookRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<Book> allBooks() {
|
||||
return repository.getAllBooks();
|
||||
}
|
||||
|
||||
public List<Product> getProducts(int pageSize, int pageNumber) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Product getProduct(int id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -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";
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.graphqlreturnmap;
|
||||
|
||||
import com.baeldung.graphql.utils.SchemaUtils;
|
||||
import com.baeldung.graphqlreturnmap.resolver.ProductResolver;
|
||||
import com.baeldung.graphqlreturnmap.resolver.Query;
|
||||
import com.coxautodev.graphql.tools.SchemaParser;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import graphql.scalars.ExtendedScalars;
|
||||
import graphql.schema.GraphQLSchema;
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static ratpack.jackson.Jackson.json;
|
||||
|
||||
public class AppHandler implements Handler {
|
||||
private static final Logger LOGGER = Logger.getLogger(AppHandler.class.getSimpleName());
|
||||
private GraphQL graphql;
|
||||
|
||||
public AppHandler() throws Exception {
|
||||
GraphQLSchema schema = SchemaParser.newParser()
|
||||
.resolvers(new Query(), new ProductResolver())
|
||||
.scalars(ExtendedScalars.Json)
|
||||
.file("schema.graphqls")
|
||||
.build()
|
||||
.makeExecutableSchema();
|
||||
graphql = GraphQL.newGraphQL(schema).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(Context context) throws Exception {
|
||||
context.parse(Map.class)
|
||||
.then(payload -> {
|
||||
ExecutionResult executionResult = graphql.execute(payload.get(SchemaUtils.QUERY)
|
||||
.toString(), null, this, Collections.emptyMap());
|
||||
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));
|
||||
});
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.graphqlreturnmap;
|
||||
|
||||
import graphql.language.ScalarTypeDefinition;
|
||||
import graphql.schema.Coercing;
|
||||
import graphql.schema.GraphQLDirective;
|
||||
import graphql.schema.GraphQLScalarType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ExtendedGraphQLScalarType extends GraphQLScalarType {
|
||||
|
||||
public ExtendedGraphQLScalarType(){
|
||||
super("","",null);
|
||||
|
||||
}
|
||||
|
||||
public ExtendedGraphQLScalarType(String name, String description, Coercing coercing) {
|
||||
super(name, description, coercing);
|
||||
}
|
||||
|
||||
public ExtendedGraphQLScalarType(String name, String description, Coercing coercing, List<GraphQLDirective> directives, ScalarTypeDefinition definition) {
|
||||
super(name, description, coercing, directives, definition);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.graphqlreturnmap;
|
||||
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
public class GraphqlReturnMap {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final RatpackServer server = RatpackServer.of(s -> s.handlers(chain -> chain.post("product", new AppHandler())));
|
||||
server.start();
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.graphqlreturnmap.entity;
|
||||
|
||||
public class Attribute {
|
||||
private String name;
|
||||
private String description;
|
||||
private String unit;
|
||||
|
||||
public Attribute(String name, String description, String unit){
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Attribute{" +
|
||||
"name='" + name + '\'' +
|
||||
", description='" + description + '\'' +
|
||||
", unit='" + unit + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.graphqlreturnmap.entity;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Product {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String description;
|
||||
private Map<String, Attribute> attributes;
|
||||
|
||||
|
||||
public Product(){
|
||||
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Map<String, Attribute> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public void setAttributes(Map<String, Attribute> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.graphqlreturnmap.model;
|
||||
|
||||
|
||||
import com.baeldung.graphqlreturnmap.entity.Attribute;
|
||||
|
||||
public class AttributeKeyValueModel {
|
||||
private String key;
|
||||
private Attribute value;
|
||||
|
||||
public AttributeKeyValueModel(String key, Attribute value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public Attribute getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Attribute value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user