Basic query

In this section, we'll use the GitHub GraphQL API explorer to get information about our GitHub user account:

  1. Let's open the following URL in a browser to open the tool:

https://developer.github.com/v4/explorer/.

   We will need to be signed in to our GitHub account if we aren't already.

  1. In the panel in the top-left corner, let's enter the following and click the Execute Query button:
query { 
viewer {
name
}
}

This is our first GraphQL query. Here are some key points:

  • We prefix a query with the query keyword. This is actually optional.
  • viewer is the name of the object we want to get.
  • name is a field within viewer that we want to return. 

The query result will appear on the right-hand side:

The data we requested is returned as a JSON object. The JSON contains a data object that contains a viewer object containing the name field. The name value should be our name, since this is the name stored in our GitHub account.

  1. On the right-hand side of the results pane there is a Docs link. If we click this link, a Documentation Explorer appears:

If we then click on the Query link, all the objects are shown that can be queried, including viewer, which is the one we just queried. If we click into this, we see all the fields that are available within viewer.

  1. Let's add avatarUrl to our query, as this is an additional field available to us:
query { 
viewer {
name
avatarUrl
}
}

So, we simply add the avatarUrl field inside the viewer object with a carriage return between the name and avatarUrl fields. If we execute the query, we see avatarUrl added to the JSON result. This should be a path to an image of us.

So, we are already seeing how flexible GraphQL is with being able to specify exactly which fields we want returned in the response. In the next section, we'll take this further by specifying the nested objects we want to return.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.109.30