GraphQL for 6 year Old
Tags: graphql, javascript, webdev, beginners
Published: 2019-02-06 10:09:20

---
title: GraphQL for 6 year Old
published: true
description: Basic concept of Graph in GraphQL
tags: #GraphQL #Javascript #webdev #beginners
---

<h3>Hey, my name is <a href="https://twitter.com/thevenicelive">Prakash</a> and today we will learn about Basic Concept of Graph in GraphQL</h3>

<img src="https://thepracticaldev.s3.amazonaws.com/i/xnuafbpcu3j239ku035z.png"/>

Topics we will Discuss:

1. <a href='#point1'>Graph</a>
2. <a href='#point2'>Directed / Undirected Graph</a>
3. <a href='#point3'>Connected /DisConnected Graph</a>
4. <a href='#point4'>Tree and GraphQL Queries</a>
   <div>
   <h2 ><a id='point1'> What is a Graph ?</a></h2>
   <img src="https://thepracticaldev.s3.amazonaws.com/i/jnx1o59yrri2mcs09i8u.png"/>

<h3>Graph is a Combination of Multiple Vertices and Edges. </h3>
<p>
<b>Vertex</b>: <span>A Node or a Block that stores the data that user had provided.
It is a searchable component that contains data that we can access anytime.
</span>
</p>
<p>
<b>Edge</b>: <span>An Edge is a route or relation between two Vertices.
Edge can show as relationship between data of vertices, and what one data means to another one. 
While searching for specific data in the graph, multiple edges provides us exact path to reach that data.If we have multiple paths, an edge can help us find the smallest path available. But we are not going to discuss about multiple paths today.
</span>
</p>
</div>
<!-- Topic 1 End -->
<div>
<h3>Example we will follow in this post</h3>
<h4>Here I have created a family graph to explain you in GraphQL in simple and understandable form. </h4>
<img src="https://thepracticaldev.s3.amazonaws.com/i/7qe6vwz05odrxekhaqm3.png"/>

<p>
Suppose you are creating a database of your family, you have included your 5 family members with you in your database. Now the graph of your database will look like the image above. Vertices will represent your family member and the edges will represent the relationship of each of them with one another. This is how graphQL visualise your database. Now lets discuss about edges in little bit more detail.   
</p>

<div >
<h2><a id="point2">Directed / Undirected Graph :</a></h2>
<img src="https://thepracticaldev.s3.amazonaws.com/i/ldi7cpz6s3xuypb3sqzn.png"/>
Now take above image as a reference and observe each side of your family member. For undirected and directed graph i will take Mother's side in the graph as an example.
<h3>1) Undirected graph</h3>
<img src="https://thepracticaldev.s3.amazonaws.com/i/0fd52pi1ozgtg1o5flv8.png" alt="image of undirected graph"/>

<p>
<i>* image above is for undirected graph *</i><br/>
Edges of Undirected graph represents same relationship from both sides perspective. For example, take Mother = X and Mother's Brother = Y and their relationship = siblings.
so in undirected graph we can say that (X,Y)=(Y,X) because edges doesn't pointed specific direction. So they are representing the same edge.
</p>

<h3>2) Directed graph</h3>
<img src="https://thepracticaldev.s3.amazonaws.com/i/do84p67xns4ts29x5djh.png"/>

<p>But in directed graph Edges represents Different point of views / perspective.Edges has direction means (X,Y) and (Y,X) defines two different stories as shown in above image we have an edge from mother to son which represents Mothers relation perspective on son and between same two vertex we have another edge that represents sons relation perspective on mother. That's how Directed Graph work you can get data from the directed side but can't get data in reverse.
</p>
</div>

<div >
<h2><a id="point3">Connected / Disconnected Graph</a></h2>
<p>Now we know how directed and undirected graph works lets talk about connected and disconnected graph using Father's side in the graph.</p>
<h3>1) Connected graph</h3>
<img src='https://thepracticaldev.s3.amazonaws.com/i/r4eb0754obwm0lioedcn.png'/>
<p>
Above us a connected graph that also creates a cycle because of Father-Fathers Mother - Fathers Sister are all directly connected each other.
Connected graphs has all vertices/nodes connected.
we can see that you are connected with your Father, and your Father is connected with his Mother and Sister.
</p>

<h3>2) Disconnected graph</h3>
<img src='https://thepracticaldev.s3.amazonaws.com/i/48lfre4qo64883zgfrwm.png'/>
<p>
In Disconnected graph we have few vertices/nodes disconnected with other nodes of the graph as shown in example if we remove father node we can see that connection between you and your Fathers family is not connected.
Same in Graph if we get disconnected with any node that have only one path to it, we can lose the data in the node.
</p>
</div>

<div>
<h2><a id="point4">Tree and Graph Queries</a></h2>
<img src='https://thepracticaldev.s3.amazonaws.com/i/t9lm25i08s7xs3ycg4af.png'/>

<p>
Tree is a connected non-cyclic graph. GraphQL do sort your graph data into a tree like above, and provides you exactly what type of data you have asked.
GraphQL provides the result of the query while there is a reason it is non-cyclic because it can create faster path to the data vertices we are searching for inside the graph. The below code snippet is a query that you provide to GraphQL, and GraphQL will return you exact data you asked.</p>
Example:
<pre>
query{
  myFamily{
    me{
      name
      location
    }
    mom{
      ...
      momsBrother{
        name
        location
      }
    },
    dad{
      
      ...
      dadsMother{
        name
        location
      },
      dadsSister{
        name
        location
      }
    }
  }
}
</pre>
</div>
<div>
<p>
That's it the basic Concept of the Graph in GraphQL i hope it will help you to visualize the process inside of the GraphQL. If you have any Query regarding this post let me know in comment or <a herf="https://twitter.com/thevenicelive">Tweet</a> me. thank you for reading this.
</p>
</div>