1. GraphQL
GraphQL using GraphQL Java library.
1.1. Usage
1) Add the dependency:
<!-- GraphQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphql</artifactId>
<version>3.5.5</version>
</dependency>
<!-- JSON library-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>3.5.5</version>
</dependency>
2) Creates schema.graphql
inside the src/main/resource
type Query {
bookById(id: ID): Book
}
type Book {
id: ID
name: String
pageCount: Int
author: Author
}
type Author {
id: ID
firstName: String
lastName: String
}
3) Install Jackson and GraphQL
import io.jooby.json.JacksonModule;
import io.jooby.graphql.GraphQLModule;
{
install(new JacksonModule()); (1)
install(new GraphQLModule(
RuntimeWiring.newRuntimeWiring() (2)
.type(newTypeWiring("Query")
.dataFetcher("bookById", fetchers.getBookByIdDataFetcher()))
.type(newTypeWiring("Book")
.dataFetcher("author", fetchers.getAuthorDataFetcher()))
.build())
);
}
1 | Install Jackson |
2 | Install GraphQL, uses the schema.graphql and provide a RuntimeWiring |
The GraphQL module generates a POST /graphql
route.
1.2. Options
1.2.1. Async
The GraphQL Java library executes queries in async mode. To turn-off the async mode:
import io.jooby.json.JacksonModule;
import io.jooby.graphql.GraphQLModule;
{
install(new GraphQLModule(...)
.setAsync(false) (1)
);
}
1 | Set async false |
Please refer to Asynchronous execution for more information.
1.2.2. Support HTTP Get
By default the module only install a HTTP POST route. If you want a HTTP GET:
import io.jooby.json.JacksonModule;
import io.jooby.graphql.GraphQLModule;
{
install(new GraphQLModule(...)
.setSupportHttpGet(true)
);
}
1.2.3. Path
To change the default path: /graphql
set the graphql.path
property in your application configuration file.
1.3. IDE
Jooby comes with supports for two IDE:
They are provided as optional dependencies.
1.3.1. GraphIQL
1) Add the dependencies:
<!-- JSON library-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>3.5.5</version>
</dependency>
<!-- GraphQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphql</artifactId>
<version>3.5.5</version>
</dependency>
<!-- GraphIQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphiql</artifactId>
<version>3.5.5</version>
</dependency>
1) Install
import io.jooby.json.JacksonModule;
import io.jooby.graphql.GraphQLModule;
import io.jooby.graphql.GraphiQLModule;
{
install(new JacksonModule()); (1)
install(new GraphQLModule(...)); (2)
install(new GraphiQLModule()); (3)
}
1 | Install Jackson |
2 | Install GraphQL |
3 | Install GraphiQL |
GraphiQL should be up and running at /graphql
.
1.3.2. GraphQL Playground
1) Add the dependencies:
<!-- JSON library-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>3.5.5</version>
</dependency>
<!-- GraphQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphql</artifactId>
<version>3.5.5</version>
</dependency>
<!-- GraphQL Playground-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphiql-playground</artifactId>
<version>3.5.5</version>
</dependency>
1) Install
import io.jooby.json.JacksonModule;
import io.jooby.graphql.GraphQLModule;
import io.jooby.graphql.GraphQLPlaygroundModule;
{
install(new JacksonModule()); (1)
install(new GraphQLModule(...)); (2)
install(new GraphQLPlaygroundModule()); (3)
}
1 | Install Jackson |
2 | Install GraphQL |
3 | Install GraphQL Playground |
GraphQL Playground should be up and running at /graphql
.
1.4. Starter
Checkout the starter/demo project for GraphQL: GraphQL Starter.