Baby Steps in GraphQL👶
Tags: beginners, expressjs, graphql, javascript
Published: 2019-03-11 10:21:24

---
title: Baby Steps in GraphQL👶
published: true
description: 
tags: beginners,express js,graphql,javascript
---


</br>
<div>
<h1>Let me explain you how to start learning GraphQL while also understanding every bit of code. </h1>

<p>
Few things i consider that you understand before starting this tutorial because you might need to know the basics of following topics but its not necessary as i will explain every code, still it will help you to build a solid foundation :
</p>
<div>
1) <a href="https://developer.mozilla.org/bm/docs/Web/JavaScript" target="_blank"> JavaScript </a>
2) <a href="https://nodejs.org/en/docs/" target="_blank"> NodeJS </a>
3) <a href="https://expressjs.com/en/api.html" target="_blank">ExpressJS</a>
</div>
</div>

<div><p>
In this tutorial we will learn to create a backend API that can Read your data (yes only Read)from the server side.
Simply the word "Read" means the operation to return the data from the Database location to the Server end or front end of your web application.</p>
<p>Let Start with folder Structure</p>
<pre>
gql-basics
         |
         -- src
         |
         -- package.json  
</pre>

<div>
<h3>Dependencies Installations :</h3>
<p><a href="https://github.com/prisma/graphql-yoga">graphql-yoga</a> (is a server framework based on ExpressJS)</P>
<pre> yarn add graphql-yoga</pre>
</div>

<div>
<h3> Setup</h3>
<p>Inside src folder create index.js which is main setup file. In that file import {GraphQLServer} from graphql-yoga and create its instance.</p>
<pre>
//import
const { GraphQLServer } = require("graphql-yoga");

//instance
const server= new graphQLServer({
//wait for this!
})
</pre>
</div>
<div>
<h3> what is Type Definition ? </h3>
<p>type definition is just like a variable with it's type declaration, for example:  </p>
<pre>
//open browser console

var client = [{
id:1,
name:"Elon Musk"
}]

//now check the type
if(typeof client.name === "string") { true } else {false}
//output = true

if(typeof client.id === "string") { true } else {false}
//output = false
</pre>

<p> same as above we have to create a file and define every data we are expecting with their types. the file name will be schema.graphql and we will create that file inside src</p>
<pre>
gql-basics
         |
         -- src
              |
              -- schema.graphql
</pre>



<p>
Now we will create our first data with the type.
In GraphQL Read only data is always get defined inside a core Query element and rest of the data (Create,Update,Delete) defines inside core element Mutation. 
</p>
<pre>
//inside src/schema.graphql

type Query {
  test: String!}

</pre>
<ul>
<p>Here is the explanation for above code:</p>
<li><b>type</b> : is used for elements that define data types in it.</li>
<li><b>Query</b>: query is read only core element of graphql</li>
<li><b>test</b>: test is called field think like its a variable that could contain anything like "functions()", "String", "Boolean" and "test" is a custom name we gave to our field that we are expecting from database for example it could be anything like "tweets in your feed","emails", "blog post you search for" </li>
<li><b>String!</b>: its define the type of data we are expecting from test field. and the "!" sign at the end defines that it can't be nullable means if you're asking for field called "test" in your console you will always get a data back.</li>
</ul>
</div>


<div>

<h3>Query</h3>
<p>Now Create a file inside a src called Query.js</p>
<pre>
gql-basics
         |
         -- src
              |
              -- schema.graphql
              |
              -- Query.js
</pre>


<p>Now Create a variable with value of empty object</p>
<pre>
//inside src/Query.js

const Query = {
  //
};
</pre>
<p>Inside the object we will define what happen to the "test" field when user will ask for it.
create a "Key:Value" pair inside the Query object and assign key as test and value as a String (sentence inside double quotes) because earlier we defined that the test field will return only a string type value.
</p>

<pre>
//inside src/Query.js

const Query = {
  test:`Hello Dev.to Community :) `
};
//NodeJS style of exporting modules
//same as like: export {Query};
module.exports = {Query}
</pre>

</div>
<div>
<h3>Running the Server</h3>
<p>
Now we will pass arguments inside the GraphQLServer in index.js and then start the server. take a look :
</p>
<pre>
const { GraphQLServer } = require("graphql-yoga");
const { Query } = require("./Query");
const resolvers = {
  Query
 
};


const server = new GraphQLServer({
  typeDefs: "./src/schema.graphql",
  resolvers
});

server.start(_ =>
  console.log(`your server is running on http://localhost:4000`)
);


</pre>
<p> Here the GraphQL Server takes an object as an argument and we have to pass two things : 1) "type definitions" which is inside "schema.graphql" file and 2) "resolvers" which are basically files that takes data from database and return to us. for Example our "Query.js" file which is returning us a hard coded string(because we did't used any database in this post). After that we have started the server using "start()" method(which came from graphql-yoga) and we have wrote a function that will log a template literal that contains a success message.</p>
</div>
<p> Now you can run your first server using following command :</p>

<pre>
//from inside your gql-basics
//type belowcode to start the server file
node src/index.js
</pre>

and it will open a GraphQL playground like image below:
<img src="https://thepracticaldev.s3.amazonaws.com/i/sqm90dvwf7ppe6nb6i2l.JPG" alt="graphql playground"/>
<p>
Hurry 🎉! Thats it we have created and also understood basics of graphql, thank you'all for giving your time to graphql and if you like this tutorial please  <a href="https://twitter.com/Thevenicelive">tweet</a> me about it.
</p>
<p>Next time i will try to explain how Mutation works, till then Happy Coding !!</p>

</div>