Getting started with GraphQL (Java)
Tags: java, graphql, tutorial, beginners
Published: 2019-07-25 07:04:53

---
title: Getting started with GraphQL (Java)
published: true
tags: Java,GRAPHQL, tutorial, beginners
canonical_url: https://jsblogs.github.io/blogs/2019/07/23/getting-started-with-graphql
---

GraphQL is query language for your Rest APIs and gives client power to select exactly what they need. It also provides server side runtime to execute the queries.

GraphQL has 3 major parts

1. GraphQL server
2. GraphQL Schema
3. Client

### GraphQL Server

GraphQL server is responsible to validate & execute the queries and returns the response as per client’s need. The client can select the fields as per need and server will return only those selected fields.

### GraphQL Schema

GraphQL has a specific language called **GraphQL Schema language** to define schema. GraphQL schema consist of the queries and types. As a API owner we can decide what queries & types will be available to the consumer. The schema has default root object type called **Query** which contains all the queries. An example schema is

```graphql
#The root object
type Query {
    hello: String
    greet(name: String): String
}
 #Defined Type
type User{
    name: String
    age: Int
}
```

In above schema root object is Query and it has one field called _hello_ and a method _greet_ and both are returning String type. The _greet_ has an input argument _name_ which is a type of String. This means client can pass any string type argument while querying _greet_.

The User type is defined type which has 2 fields name and age. GraphQL supports several built-in types like String, Int, Float, Boolean, etc. [Click here](https://graphql.org/learn/schema/) to read more about graphql schema and types.

> For every field or query in the Query Type will have corresponding resolver which will be running at GraphQL Server.

### Client

The clients are the consumer. Since GraphQL works over Rest so any rest client can query the graphql. There are few tools which are great for testing. These tools gives the auto-completion features and we can view all possible queries and types. Some of the tools are **GraphiQL** , **insomnia**.

### GraphQL-Java in Action

Below code will demonstrate you how to integrate GraphQL in Spring application. So let’s start with the GraphQL schema file.

The code structure will look like

![GraphQL code structure](http://blogs.jbisht.com/images/graphql/graphql-code-structure.png)

##### Define schema file users.graphql

```graphql
type Query {
    hello: String
    users(name: String): User
}

type User {
    name: String
    age: Int
    address: String
}
```

Now well start configuring the GraphQL Java objects along with resolvers for the queries (hello & user).

##### Step 1: Read & parse schema file

Filename: GraphQLSchema.java

```java
private TypeDefinitionRegistry readAndParseSchemaFile() throws Exception {
        String schemaString = ResourceUtils.readClasspathResourceContent("users.graphql");

        SchemaParser schemaParser = new SchemaParser();
        
        // parsing schema file and creating typeDefRegistery
        return schemaParser.parse(schemaString);
    }
```

##### Step 2: Configure Resolvers

```java
private RuntimeWiring buildRuntimeWiring() {
        return RuntimeWiring.newRuntimeWiring()
                .type("Query", builder -> builder.dataFetchers(buildDataFetchers()))
                .build();
    }

    private Map<String, DataFetcher> buildDataFetchers() {
        Map<String, DataFetcher> dataFetchers = new HashMap<>();
        dataFetchers.put("hello", new StaticDataFetcher("Welcome to GraphQL world."));
        dataFetchers.put("users", env -> User.of("John", 28, "India"));
        return dataFetchers;
    }
```

##### Step 3: Prepare GraphQL object and execute query

```java
private void setup() throws Exception {
        // We need to create GraphQLSchema Object but before that we need to configure resolvers 
        GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(readAndParseSchemaFile(), buildRuntimeWiring());
        graphQL = GraphQL.newGraphQL(graphQLSchema).build();
    }
    
    public ExecutionResult executeQuery(String graphQLQuery) {
        return graphQL.execute(graphQLQuery);
    }
```

##### Step 4: Serve user queries on the endpoint (/graphql)

```java
@RestController
    public class GraphQLController {
    
        private final GraphQLService graphQLService;
    
        @Autowired
        public GraphQLController(GraphQLService graphQLService) {
            this.graphQLService = graphQLService;
        }
    
        @PostMapping("/graphql")
        public Map<String, Object> executeQuery(@RequestBody GraphQLRequest request) {
            return graphQLService.executeQuery(request.getQuery()).toSpecification();
        }
    }
```

##### Step 5: User GraphiQL or Rest to execute graphql query

GraphiQL resource links

- [Download Link](https://github.com/graphql/graphiql)
- [Source Code](https://github.com/graphql/graphiql)

Add your /graphql controller’s endpoint in GraphiQL and start executing query

![Query hello](http://blogs.jbisht.com/images/graphql/gqlq1.png)

We can ask the response fields and graphql will serve only selected fields.

1. Query users for name only

![Name only](http://blogs.jbisht.com/images/graphql/gqlq2.png)

1. Query users for all fields

![All fields](http://blogs.jbisht.com/images/graphql/gqlq3.png)

We can ask for multiple queries in single request

![Multiple queries](http://blogs.jbisht.com/images/graphql/gqlq4.png)

Even same query with multiple times with alias

![Duplicate queries](http://blogs.jbisht.com/images/graphql/gqlq5.png)

You can find complete code [here](https://github.com/jeetmp3/tutorials/tree/master/graphql-java-intro)

Happy Coding 😀😀😀 !!!