Friday, 20 September 2019

GraphiQL

GraphiQL is a query language, it’s expose a single endpoint API and we can fetch data from query as per the needs.
Graph QL can be used with any programming language and with any framework.
Grabh QL is more efficient and flexible in client-server communication.
A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type
Example:-
Create Graph QL using spring boot. Add below dependency.
dependencies {
    compile 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.0.5'
     compile 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.0.5'
     compile 'com.graphql-java-kickstart:graphql-java-tools:5.3.4'
}
Add below mapping in application.yml
graphql:
  servlet:
    mapping: /api/graphql
    enabled: true

graphiql:
  enabled: true
  mapping: /graphiql
  endpoint:
    graphql: /api/graphql
  cdn:
    enabled: true
    version: 0.12.0
Create a graph QL query schema schema.graphqls
type Query {
  firstName: String!
  lastName: String!
}
Now create a query resolver class to handle above query, create one method for each query field
import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;

@Component
public class RootQueryResolver implements GraphQLQueryResolver {

     public String getFirstName() {
           return "Manoj";
     }

     public String getLastName() {
           return "Kumar";
     }
}
Now run spring boot application and run below URL
This will open the graph QL tool in browser now you can pass the query
{
  firstName,
  lastName
}
Output:-
{
  "data": {
    "firstName": "Manoj",
    "lastName": "Kumar"
  }
}
Refer below screenshot-
Defining Object Fields in schema-
type Query {
  employee: Employee!
}
type Employee {
     EmployeeNo: String!
     Name: String!
}
In above example we have defined a field employee of type Employee object and then further declaration Employee type object with its fields. Now create a method in query resolver class getEmployee() which will return employee object.
To declare a list use below scheme-
type Query {
  employee: [Employee!]
}
type Employee {
     EmployeeNo: String!
     Name: String!
}
Defining more complex schema:-
type Query {
  employee: Employee!
}

type Employee {
     empId: String!
     firstName: String
     lastName: String
     age: Int
     addressList: [String]
}
Query resolver class-
import java.util.Arrays;

import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import spring.boot.graphql.model.Employee;

@Component
public class RootQueryResolver implements GraphQLQueryResolver {

     public Employee getEmployee() {
           Employee employee = new Employee();
           employee.setEmpId("123");
           employee.setFirstName("Manoj");
           employee.setLastName("Kumar");
           employee.setAge(30);
           employee.setAddressList(Arrays.asList("Addrs1","addrs2"));
           return employee;
     }
}

Refer below screenshot for output



2 comments: