visit
Adding Dependencies
The first step is to add the
graphqlize-java
& the JDBC driver dependencies.Initialising GraphQLizeResolver
The next step is initialising
GraphQLizeResolver
. To do it, let's a create new file GraphQLizeResolverProvider.java and add the following code to expose the GraphQLizeResolver
as a spring-boot bean.package org.graphqlize.java.springboot;
import org.graphqlize.java.GraphQLizeResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@Component
public class GraphQLizeResolverProvider {
private final DataSource dataSource;
private final GraphQLizeResolver graphQLizeResolver;
public GraphQLizeResolverProvider(DataSource dataSource) {
this.dataSource = dataSource;
graphQLizeResolver = new GraphQLizeResolver(dataSource);
}
@Bean
public GraphQLizeResolver graphQLizeResolver() {
return this.graphQLizeResolver;
}
}
During initialisation, the
GraphQLizeResolver
reads the metadata of the database using the JDBC metadata APIs and keeps an in-memory representation of them.Configuring DataSource
To configure the DataSource, let's add the following properties in the application.properties file.
For Postgres,spring.datasource.url=jdbc:postgresql://localhost:5432/sakila
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.url=jdbc:mysql://localhost:3306/sakila
spring.datasource.username=root
spring.datasource.password=mysql
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 .
Adding GraphQL Endpoint
The final step is exposing an API endpoint for handling the GraphQL request. To do it, let's create a new file GraphQLController.java and do the following.
package org.graphqlize.java.springboot;
import org.graphqlize.java.GraphQLResolver;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
class GraphQLRequest {
private String query;
private Map<String, Object> variables;
// ... Getters & Setters are ignored for brevity
}
@RestController
public class GraphQLController {
private final GraphQLResolver graphQLResolver;
public GraphQLController(GraphQLResolver graphQLResolver) {
this.graphQLResolver = graphQLResolver;
}
@PostMapping("/graphql")
public ResponseEntity handle(@RequestBody GraphQLRequest graphQLRequest) {
String result =
graphQLResolver.resolve(
graphQLRequest.getQuery(),
graphQLRequest.getVariables());
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, "application/json")
.body(result);
}
}
Get the query & the variables from the request and invoke the
resolve
method on the initialised instance of GraphQLizeResolver
.It returns the
result
as stringified JSON, and we are sending it as response body with the content type as application/json
.> curl -X POST \
--data '{"query": "query { actorByActorId(actorId: 1){firstName}}"}' \
-H "Content-Type: application/json" \
//localhost:8080/graphql
{
"data": {
"actorByActorId": {
"firstName": "PENELOPE"
}
}
}
All you need to do is download this file and put it under the src/main/resources/static directory.
When you restart the server, the Voyager will be available at . A sample output would look like .Then to interact with the GraphQL API, let's add the . Like Voyager, download this file and put in the static directory.
This GraphQL playground will be available at after server restart.⭐️ If you like GraphQLize, give it a star on ! ⭐️
Previously published at //dev.to/tamizhvendan/graphql-with-java-spring-boot-and-postgres-or-mysql-made-easy-3p9n