What I just Learnt Today #1

zulia
4 min readNov 10, 2020

Simple Implementation Basic Query in GraphQL (Nodejs)

It simply comes to my mind What is Graphql?

What is Graphql?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.

By this definition, we can break down some understandings:
1. Language for API which means you can treat GQL as same as API you usually use.
2. A runtime which means you only need one initiation route.
3. Fulfilling those queries with your existing data which means you can provide data based on the queries you input at the very first place. When using REST API you will return exactly all the data the API provides (sometimes you don’t need all the data) meanwhile in GQL you can return only the data you need. (Calm down, we will explain with the code below)

Implementation in Server

Say that you have this file called index.js where you store all the init the files you will use as a server

index.js

(() => 
{
var schema = schemaGQL.buildSchema(`
type Query {
user(name: String): User
},
type User {
name: String
age: Int
occupation: Occupation
},
type Occupation {
role: String,
yoe: Int
}`);
var root = {
user: (data) => {
const user = business.getUser(data.name)
return {
name: user.name,
age: user.age,
occupation: {
role: user.occupation.role,
yoe: user.occupation.yoe,
}
}
},
};
const app = express(); app.use('/graphql', graphql.graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
})()

then you have one file to do the business. Let’s call it with business.js

business.js

const users = [
{
name: 'Zulia',
age: 23,
occupation: {
role: 'Software Engineer',
yoe: 5
}
},
{
name: 'Hendra',
age: 25,
occupation: {
role: 'Software Engineer',
yoe: 10
}
}
]
export const getUser = (name) => {
return users.find(item => item.name.toLowerCase() === name.toLowerCase())
}

That’s my simple gql’s implementation. So what’s so interisting?

So, let’s see the result if we execute the gql definition by point 1

Point 1: Language for API
After you make sure you run your service in local, you can make a request to your server by hit the endpoint given in graphiQL

curl get http://localhost:4000/graphql\?query\=%7B%0A%20%20user\(name%3A%20%22zulia%22\)%20%7B%0A%20%20%20%20name%0A%20%20%20%20age%0A%20%20%20%20occupation%20%7B%0A%20%20%20%20%20%20role%0A%20%20%20%20%20%20yoe%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A
{"data":{"user":{"name":"Zulia","age":23,"occupation":{"role":"Software Engineer","yoe":10}}}}%

See? You can retrieve data using plug in graphQL (expressgql, apollo, etc) or you can simply curl it.

Point 2 and 3: Just A runtime and Fulfilling those queries with your existing data
In index.js file, you can find we initiate route just once.

app.use('/graphql', graphql.graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));

By accessing only /graphql, you can get all the data you need and forget about the data that you don’t need.
Example:

Oh well, in this session I just need user’s name and age, I no longer need the occupation (keep in mind how efficient it gets when you don’t need to query the data you don’t actually need from DB). And what if I only need occupation data?

Oh we dont need to make new route! We just simply need exactly one route to get massive amount of data that we want! Isn’t it a little bit tricky and also useful?

So I guess GQL is so useful when you don’t want to spend a lot of time construct all the data that you don’t want to use and you can focus about your goal which means you can track current feature you develop and what it needs. It’s also efficient and sufficient because instead of making so many routers, you can simply make one route that can return all the data that you want. You also can control which things you get and you don’t get. Simple user case:

You have 2 table:

User {
id
name
occupation
}
Occupation {
idUser: FK to User
role
yoe
}

When you only need User, you can simple do single query that returns user’s information. So I thought it’d be more effective and efficient since we no longer need to query everything from database but still using the same route.

That’s all for my explanation, if there’s something I could revise, you could email me: zulia171@gmail.com and let’s discuss about things😆

--

--

zulia
0 Followers

a Software Engineer, but can't talk about tech. Mostly talk about myself.