GraphQL, A Query Language for your rest api.

krishankant singhal
3 min readAug 29, 2019

--

Recently i got a chance to work on GraphQL. When i started initially i did not like it. i felt we have to create schema of everything and then resolver to get those schema value. what advantage are we getting Anyway we have to right so much code. But once i got more and more into GraphQL i got the importance of it.

One Biggest advantage is single endpoint for all your api. You donot require multiple endpoint for various resources. you just need to specify fields you want to get and Volla! you get it.

What is GraphQl

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

Some of Graphql advantages are

1) Ask for what you need, get exactly that
2) Get many resources in a single request
3) Describe what’s possible with a type system
4) less number of network calls and network data save so works well with slow connections.

Lets take the example of simple school management system. lets define two entities

  1. Student (id, name, age, class,school)
  2. School (id,name,student)

So if we have to write crudl (Create, Retrive Update,Delete and List) we require 10 rest endpoints for Student and School. And if we require list of students inside a school or vice-versa or we require different responses like listing of all student, then student detail , we further require more rest points and it keeps going.

but in case of GraphQl we require one endpoints. we just define schema of what we can expect and resolver to resolve that data and done.

So Basically for Writing graphql ,we have to define the data types of data we want to get. and we have to write resolvers to get that data.

To get the data, we call queries and to modify the data we use mutation.

school.graphql

type Student {
id: Int!,
name: String,
age: Int!,
class: String;
school: School
}
type School {
id: Int!,
name: String,
student: Student
}
type Query {
getStudent(id: Int!): Student;
getAllStudent(): [Student];
getSchool(id: Int!):School;
}
type Mutation {
createStudent(intput:Student):
}

school.resolver

import _ from "lodash";
import {
SchoolService,
StudentService
} from "../services";
export const StudentResolver = {
Query: {
getStudent: async (object, params) => {
try {
const result = await StudentService.getStudent(params);
return result;
} catch (error) {
throw new Error(error.message);
}
},
getAllStudent: async (object, params) => {
try {
return await StudentService.getStudentList(params);
} catch (error) {
throw new Error(error.message);
}
},
},
};

here we wrote .graphql file, this file allowed us to define the structure of our data. for example Student will have id as int, name as String data type etc.

then we define resolver corresponding to method define in Query and Mutation section. Resolver are function which actually interacting with backends api, system or other medium, and getting the data for us.

How to query from client

query { 
getStudent(id:225) {
name,
age
}
}

Here we want the detail of a student whose id is 225, but we just want the name and age of the student, so we just specify the field which we want. we need not to consume whole student object, graphql will take care of that and will provide field which are required. this is the power of graphql.

--

--

krishankant singhal
krishankant singhal

Written by krishankant singhal

Angular,Vuejs,Android,Java,Git developer. i am nerd who want to learn new technologies, goes in depth.

No responses yet