BAEL-5307: Move code from new module to existing (#11868)
This commit is contained in:
+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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.graphql.server;
|
||||
|
||||
import com.baeldung.graphql.data.Book;
|
||||
import com.baeldung.graphql.data.BookRepository;
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
type Book {
|
||||
title: String!
|
||||
author: Author
|
||||
}
|
||||
|
||||
type Author {
|
||||
name: String!
|
||||
surname: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
allBooks: [Book]
|
||||
}
|
||||
|
||||
schema {
|
||||
query: Query
|
||||
}
|
||||
Reference in New Issue
Block a user