One of the core design goals of GraphQLize is not to tie to any web development framework and remain as a drop-in JVM library.
Getting started with GraphQLize in vanilla Java is straight-forward and involves only three steps.
Adding Dependencies#
Let's start with creating a new Java Project using Gradle (or Maven) and add the graphqlize-java
& the JDBC driver dependencies.
// ...
repositories {
// ...
maven { url "https://clojars.org/repo" }
}
// ...
dependencies {
// For Postgres
implementation 'org.postgresql:postgresql:42.2.10'
// For MySQL
implementation 'mysql:mysql-connector-java:8.0.19'
implementation 'org.graphqlize:graphqlize-java:0.1.0-alpha20'
// DB Connection Pooling
implementation 'com.zaxxer:HikariCP:3.4.2'
// ...
}
Initializing GraphQLizeResolver#
To initialize GraphQLizeResolver
, we need a DataSource.
Configuring DataSource#
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
public class Program {
private static DataSource getDataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/sakila");
config.setUsername("postgres");
config.setPassword("postgres");
return new HikariDataSource(config);
}
public static void main(String[] args) {
DataSource dataSource = getDataSource();
}
}
Make sure you are changing the above values to refer your database connection. The above example assumes that you are using the sakila database created from this .
The next step is using this data source object to create a new instance of GraphQLizeResolver
.
import org.graphqlize.java.GraphQLResolver;
import org.graphqlize.java.GraphQLizeResolver;
public class Program {
public static void main(String[] args) {
DataSource dataSource = getDataSource();
GraphQLResolver graphQLResolver =
new GraphQLizeResolver(dataSource);
}
}
Currently, it takes around 8 to 12 seconds to initialize. I am planning to in a future release.
Querying Database#
With the GraphQLize resolver in place, we can query the underlying database using GraphQL.
public class Program {
public static void main(String[] args) {
String result =
graphQLResolver.resolve("query { actorByActorId(actorId: 1) { firstName, lastName } }");
System.out.println(result);
}
}
{ "data": { "actorByActorId": { "firstName": "PENELOPE", "lastName": "GUINESS" } } }
The resolve
method has an overload to support GraphQL variables as well. The above example can be re-written using variables as below.
public class Program {
public static void main(String[] args) {
String result =
graphQLResolver.resolve(
"query($actorId: Int!) { actorByActorId(actorId: $actorId) { firstName, lastName } }",
variables
);
System.out.println(result);
}
}
We can also run the introspection queries to know what types and queries the GraphQLizeResolver
supports.
public class Program {
public static void main(String[] args) {
String result =
graphQLResolver.resolve("{__schema {types {name}}}");
System.out.println(result);
}
}
This would print all the types generated by the GraphQLize.
{
"data": {
"__schema": {
"types": [
{
"name": "Actor"
},
{
"name": "ActorInfo"
},
{
"name": "Address"
},
...
]
}
}
}
To know more the fields of a given type, we can run the following introspection query.
public class Program {
public static void main(String[] args) {
String result =
graphQLResolver.resolve(
"{__type(name: \"Actor\") { fields { name type { name kind ofType { name kind }}}}}"
);
System.out.println(result);
}
}
We'll get the following as output (prettified for readability)
{
"data": {
"__type": {
"fields": [
{
"name": "actorId",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "Int",
"kind": "SCALAR"
}
}
},
{
"name": "filmActors",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": null,
"kind": "LIST"
}
}
},
{
"name": "films",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": null,
"kind": "LIST"
}
}
},
{
"name": "firstName",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "String",
"kind": "SCALAR"
}
}
},
{
"name": "lastName",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "String",
"kind": "SCALAR"
}
}
},
{
"name": "lastUpdate",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "String",
"kind": "SCALAR"
}
}
}
]
}
}
}
Next Steps#
Congrats! You are on course to build impressive applications using GraphQLize in less time. To save yourself some more time, do refer this documentation to know more about how GraphQLize generates the GraphQL schema and the queries.
The sample code is available in .