Build Client Side GraphQL React Application using GitHub GraphQL API (Part-2)
Tags: react, graphql, github, redux
Published: 2020-03-30 11:24:02

In this series of articles, we will build a React Client App which will interact with GitHub GraphQL API.
 You can find the Part-1 code repository here: {% github Heet1996/ReactApp%}Full code repository:
{% github Heet1996/GitHubApi%}
You can find the final app here: [GitHub Repo Finder](https://githubrepo-cc932.firebaseapp.com/). 
*Before reading further I would highly recommend you to have a tour of this app.* 
*** 
##Note: This is the second part of the ongoing series. You can also read the first part [here]( https://dev.to/heet1996/build-client-side-graphql-react-application-using-github-graphql-api-part-1-12gj)  

***
Till now we have covered below points:
1.	Created our React App with nice looking landing page
2.	Generated a valid token to interact with GitHub API
3.	 Created a Token Validator Page so that user can verify whether token is valid or not

In this article we will look into [GitHub GraphQL API v4]( https://developer.github.com/v4/)  and build our Queries and Mutation for:
```
1.	Searching the repository which are on GitHub
2.	Able to Star and Watch your favourite repository
3.	Show Star, Watch and Total repository count for a search.
```


## Interact with GitHub GraphQL API using GraphIQL Application
 
In this section we will interact to GitHub API using GraphIQL application by developing queries to build our Application features. This article assumes that you know basics of GraphQL, have already install GraphIQL application and also have a valid access token to interact with GitHub(All the pre-requisites are cover in [previous post]( https://dev.to/heet1996/build-client-side-graphql-react-application-using-github-graphql-api-part-1-12gj)).


### GRAPHQL QUERY WITH GITHUB'S GRAPHQL API
So, one of our tasks is to search a repository on GitHub for a given input.
Copy paste the below query on your `GraphIQL` application and I will explain you later what it does.
```
query repoSearch($repoQuery:String!)
  {
    search(query:$repoQuery,type:REPOSITORY,first:5) {
       repositoryCount
       edges {
         node {
           ... on Repository {
             name
             id
             description
             url
           }
         }
       }
     }
  }
```
![alt GraphIQL](https://i.ibb.co/BGSgy4K/Graph-IQL-Repo-Search.png)

Paste below object in your Query Variable tab in GraphIQL. We will search all the repository which has/contains `React JS` as a keyword in it. 
```
{
  "repoQuery": "React JS"
}
```

![alt GraphIQL]( https://i.ibb.co/BTz3h19/Graph-IQL-Repo-Search-2.png)

Click on run button on you will see below results:
![alt Result] (https://i.ibb.co/YDDf3Kv/Graph-IQL-Repo-Search-3.png)
`Note: Extract your favourite repository id from the result we have obtain. We will use this ID in our tutorial future to star and watch the repository.   
`
![alt Query Variable]( https://i.ibb.co/b5wmGcj/Graph-IQL-Repo-Search-4.png)
Now let’s discover what is there in the query. I will break it down and explain it you step by step

``` 
  query repoSearch($repoQuery:String!)
  {
    search(query:$repoQuery,type:REPOSITORY,first:5) {
	       repositoryCount
}
}
```
Here we are shooting up a query and asking GitHub Api to give us the total repositories which has a Type of REPOSITORY and repository contains keyword as **React JS ** .
We also have object call as `edges and node` in our query. Edges are collection of nodes which are connected to each other and each node is a repository with fields (name, id, description and Url). This structure is very much similar to Graph Data Structure. 
As you can see, although repository count is much larger than the actual return result. This is because we have asked GitHub to fetch only first five repositories from the result.  
We are done with repository fetching and now all we need is to fetch star and watch list count of respective repository.

Look into below query:
```
query repoSearch($repoQuery:String!)
  {
    search(query:$repoQuery,type:REPOSITORY,first:5) {
      	repositoryCount
       edges {
         node {
           ... on Repository {
             name
             id
             description
             url
              watchers{
               totalCount
             }
             stargazers{
               totalCount
             }
           }
         }
       }
     }
  }
```

Above query is same as we had discussed earlier, the only object we have added now is `watchers` and `stargazers` which belongs to star and watch details of a repository. 
That’s it, now we are done with fetching details next step we will look, how can we store and watch the repository.
### GRAPHQL MUTATION WITH GITHUB'S GRAPHQL API

#### Star Your Favourite Repository 
Paste below query in your GraphIQL application
```
mutation starRepo($repositoryId: ID!) {
    addStar(input:{starrableId:$repositoryId}) {
      starrable {
        viewerHasStarred
      }
    }
  }
```

In order to Star a repository, we would require the repository id which we had extracted earlier. You can use this repository id as well ` MDEwOlJlcG9zaXRvcnk3MDEwNzc4Ng==`
Paste your Query Variable:
```
{
  "repositoryId":"MDEwOlJlcG9zaXRvcnk3MDEwNzc4Ng=="
}
```
Run the query and you will see below result: 

![alt Result]( https://i.ibb.co/LhmJ5gM/Graph-IQL-Repo-Search-5.png)

#### UnStar the Repository

To remove star below is the query:

 ```
mutation starRepo($repositoryId: ID!) {
    removeStar(input:{starrableId:$repositoryId}) {
      starrable {
        viewerHasStarred
      }
    }
  }

```

#### Watch Your Favourite Repository

Paste below query in your GraphIQL application

```
mutation UpdateWatcher($repositoryId: ID!, $subscribeState: SubscriptionState!){
  updateSubscription(input:{subscribableId:$repositoryId,state:$subscribeState}){
    subscribable{
      id
      viewerSubscription
    }
  }
  
}
```
We are passing two parameter’s Repository ID and Subscribe State. In order to watch the repository the value of subscribeState should be ‘SUBSCRIBED’ and to stop watching the repository we should pass ‘UNSUBSCRIBED’ as a value. 
Let us start watching the repository. Paste the below Query Variable in GraphQLI.
```
{
  "repositoryId":"MDEwOlJlcG9zaXRvcnk3MDEwNzc4Ng==",
  "subscribeState":"SUBSCRIBED"
}
```
Run the Query and you will see below Output:

![alt Result] (https://i.ibb.co/Fmqd9j0/Graph-IQL-Repo-Search-6.png)

Below are the points we have covered in this article till now:

1.	Created a GraphQL Query to Search a Repository which are on GitHub
2.	Created a GraphQL Query to Star/UnStar a Repository which are on GitHub
3.	Created a GraphQL Query to Watch/Unwatched  the Repository.

Above all will help us to build our client app to interact with GitHub API and achieve the desired results. If you want to learn more, have a look into their [documents]( https://developer.github.com/v4/) and explore all the endpoints which they have provided to build some cool stuff.
In next article we will see how can we add Pagination and Setup our GraphQL Client with our React Application.

Stay Home, Stay Safe