An open API service indexing awesome lists of open source software.

https://github.com/ani-sha/hackernews

This is a starter application in GraphQL and Java.
https://github.com/ani-sha/hackernews

graphql graphql-java

Last synced: 5 months ago
JSON representation

This is a starter application in GraphQL and Java.

Awesome Lists containing this project

README

          

# hackernews
This is a starter application in GraphQL and Java.

### Initialize the project

Since you'll be using [Maven](https://maven.apache.org/) (still the most widely used build tool for Java) in this tutorial, make sure you have a reasonably fresh version installed.

To bootstrap a simple web application project execute the following commands in a terminal (and confirm with `Y` when prompted):

```bash(path=".../")
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.howtographql.sample -DartifactId=hackernews-graphql-java -Dversion=1.0-SNAPSHOT
```

Next you'll setup the project structure.

Immediately create a directory called `java` under `src/main`. This is where all the Java sources will go.

Additionally, make sure you also delete `WEB-INF/web.xml` (or simply the entire `WEB-INF`) from `src/main/webapp`.
Otherwise, Servlet 3.x style configuration that you'll use will be ignored.

### Defining the schema

It is important to note that the resolver functions are an integral part of the field definitions, and thus a part of the schema. This means the schema isn't just a document, but a runtime object instance (for the purposes of this track, that would mean a Java object).

The schema can be defined in two ways:

* programmatically - where type definitions are assembled manually in code
* using the [Schema Definition Language](http://graphql.org/learn/schema/#type-language) (SDL) - where the schema is generated from a textual language-independent description you've seen in the previous chapters with the resolver functions then wired dynamically

Both approaches have merit, and come down to a matter of preference. The former collocates the fields and their associated resolves, while the latter makes a clear cut between data and behavior. We'll use SDL for the most part of this track as it allows for succinct examples.

The SDL definition for a simple type representing a link might look like this:

```graphql(path=".../hackernews-graphql-java/src/main/resources/schema.graphqls")
type Link {
url: String!
description: String!
}
```

And a query to fetch all links could be defined as:

```graphql(path=".../hackernews-graphql-java/src/main/resources/schema.graphqls")
type Query {
allLinks: [Link]
}
```

Finally, the schema containing this query would be defined as:

```graphql(path=".../hackernews-graphql-java/src/main/resources/schema.graphqls")
schema {
query: Query
}
```

Save these definitions in a file called `schema.graphqls` inside `src/main/resources`.

### Install dependencies

To build a GraphQL-enabled application, only `graphql-java` (the GraphQL implementation itself) is strictly required. Still, to make dynamic resolver wiring easy, you'll also want to use `graphql-java-tools`, the library inspired by Apollo's `graphql-tools`. Additionally, because the goal is to expose the API over the web, you'll also make use of `graphql-java-servlet` (a simple helper library containing a ready-made servlet for accepting GraphQL queries) and `javax.servlet-api` (the servlet specification implementation).

Add all the dependencies to your `pom.xml` (under ``):

```xml(path=".../hackernews-graphql-java/pom.xml")

com.graphql-java
graphql-java
3.0.0

com.graphql-java
graphql-java-tools
3.2.0

com.graphql-java
graphql-java-servlet
4.0.0

javax.servlet
javax.servlet-api
3.0.1
provided

```

The versions listed above were the latest at the time of writing, but they change quickly as bugs are fixed and features are added. Make sure you always check for updates before going further.

### Setup server

Any servlet container will do here, and the simplest way to use one during development is via a Maven plugin.

For Jetty, add the plugin to the `build` section as follows:

```xml(path=".../hackernews-graphql-java/pom.xml")

hackernews


org.eclipse.jetty
jetty-maven-plugin
9.4.6.v20170531

```

This also a good opportunity to configure some basics.

Add the following plugin configuration (just below the Jetty plugin) to set Java version to 8 and servlet spec version to 3.1:

```xml(path=".../hackernews-graphql-java/pom.xml")

org.apache.maven.plugins
maven-compiler-plugin
3.5.1

1.8
1.8

maven-war-plugin
3.1.0

```

> You can run the app just by executing `mvn jetty:run` in the directory where `pom.xml` is located, and Jetty will start on port 8080.

But opening it at this moment won't bring you much joy, as the server still isn't configured to *do* anything.

To remedy this, start by creating a class called `GraphQLEndpoint`, this will be the servlet exposing the API.
All classes in [the sample project](https://github.com/howtographql/graphql-java) are placed in the package called `com.howtographql.hackernews`, so you may wish to create such a package inside `src/main/java` at this moment.
`GraphQLEndpoint` should look as follows:

```java(path=".../hackernews-graphql-java/src/main/java/com/howtographql/hackernews/GraphQLEndpoint.java")
import com.coxautodev.graphql.tools.SchemaParser;
import javax.servlet.annotation.WebServlet;
import graphql.servlet.SimpleGraphQLServlet;
@WebServlet(urlPatterns = "/graphql")
public class GraphQLEndpoint extends SimpleGraphQLServlet {
public GraphQLEndpoint() {
super(SchemaParser.newParser()
.file("schema.graphqls") //parse the schema file created earlier
.build()
.makeExecutableSchema());
}
}
```

Starting the server now and accessing http://localhost:8080/graphql will still result in an error because no resolver functions have been wired in (so the defined `allLinks` query has no way to execute).