To learn GraphQL, we'll start by using the GitHub API.
-
Sign in to GitHub (get a GitHub username if you need one). Go to the GitHub GraphQL Explorer.
-
You can now execute GraphQL queries in this tool, called GraphiQL.
-
Queries look like one of:
Operation Description query { foo }
An unnamed query { foo }
An unnamed query - shorthand form query MyQuery { foo }
A named query -
We'll just use the shorthand form for now. In the left pane type an open brace ('
{
'). You'll see GraphiQL complete the query by adding the closing brace. Hit return. -
With the cursor between the braces, type CTRL-SPACE to view a list of completions. Select
viewer
. This reflects data about the current viewer of the graph (you). -
Add an open brace and hit RETURN. Add the following fields:
- login
- avatarURL
- createdAt
- id
- isViewer
Execute the query (shortcut: CTRL-RETURN or CMD-RETURN).
Your query should look like this:
viewer { login createdAt id isViewer }
-
A hint: Hovering over a red underline presents an error message.
-
A brief look at query syntax. Investigate the following:
- Do the fields need to be on separate lines, or is a space enough to separate them?
- Can you add commas between fields if necessary?
- Does ordering of fields matter?
- What happens if you repeat a field name?
- Does adding the word
query
before the top-level opening brace change anything? - Make it a named query. Can you specify multiple named queries, and if so, which one gets executed? Does the "Execute Query" button let you choose?
- In these exercises (and typically), you'll send just one query at a time, so when sending a new query, just overwrite a previous one in the GraphiQL tool.
-
- Fields may take parameters (depending on how the schema was defined). Perform a new query for the
repository
owned bygraphql
and identified by the namegraphql-js
. To do this, afterrepository
, type a(
and use command completion to see the options. Then enter a:
and the parameter value in double quotes. retrieve thedescription
,id
andcreatedAt
fields.
In GraphQL, most queries are made up of a set of fields that form a selection set. A field represents a simple value such as a string or an integer.
Fields can also represent more complex data, or relationships to other data, in which case they too will have subfields. This nesting can continue indefinitely.
Ultimately, all selections must resolve to simple fields which return scalar values.
-
Add in the following to your repository query, underneath the
createdAt
field:commitComments(first: 5) { edges { node { author { login } body } } }
What does this query find?
-
Use the top-level field
organization
to retrieve in one response, for "facebook":- its login
- the total count of its "
membersWithRole
" - when the repository called "relay" was created
-
What are the top level queries that may be run? To find out, remove everything except
{ }
and type CTRL-SPACE between the braces. -
Examine the "Docs" button to the right. It enables you to browse the schema.
- Under "Docs", navigate to the ROOT TYPE:
query: Root
. - You'll see a list of fields.
How do types relate to fields?
- Under "Docs", navigate to the ROOT TYPE: