GraphQL
GraphQL using GraphQL Java library.
Usage
1) Add the dependency:
<!-- GraphQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphql</artifactId>
<version>4.1.0</version>
</dependency>
<!-- JSON library-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>4.1.0</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())
);
}
-
Install Jackson
-
Install GraphQL, uses the
schema.graphqland provide aRuntimeWiring
The GraphQL module generates a POST /graphql route.
Options
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)
);
}
-
Set async false
Please refer to Asynchronous execution for more information.
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)
);
}
Path
To change the default path: /graphql set the graphql.path property in your application configuration file.
IDE
Jooby comes with supports for two IDE:
They are provided as optional dependencies.
GraphIQL
1) Add the dependencies:
<!-- JSON library-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>4.1.0</version>
</dependency>
<!-- GraphQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphql</artifactId>
<version>4.1.0</version>
</dependency>
<!-- GraphIQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphiql</artifactId>
<version>4.1.0</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)
}
-
Install Jackson
-
Install GraphQL
-
Install GraphiQL
GraphiQL should be up and running at /graphql.
GraphQL Playground
1) Add the dependencies:
<!-- JSON library-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>4.1.0</version>
</dependency>
<!-- GraphQL-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphql</artifactId>
<version>4.1.0</version>
</dependency>
<!-- GraphQL Playground-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-graphiql-playground</artifactId>
<version>4.1.0</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)
}
-
Install Jackson
-
Install GraphQL
-
Install GraphQL Playground
GraphQL Playground should be up and running at /graphql.