First Request

How to build a simple query:

In GraphQL, a query is the string you send to the server to process and request data. It’s a read-only operation so you cannot create or manipulate data with it.

REQUEST
Copy
query {
  courseCatalog {
    nodes {
      title
    }
  }
}
RESPONSE
{
  "data": {
    "courseCatalog": {
      "nodes": [
        {
          "title": "CCSP: Cloud Governance, Data Privacy, and Audit"
        },
        {
          "title": "Building Your First VBA Module"
        },
        {
          "title": "Mastering Object-oriented Programming in Java"
        }
      ]
    }
  }
}

Let's dissect it line by line:

  • A request begins with ‘query’ keyword. Specifying the type of operation ‘query’ is optional, but improves readability.

  • In this example we are requesting for the ‘Course Catalog’ data which gives us a list of all available courses.

  • A node or nodes is just a container for the data and contains fields we might be interested in. As an example: title.

Try this query in the GraphQL Playground (Note: The Playground requires a valid API Key)

One of the nice things about GraphQL is that it allows you to select only the fields you need thus avoiding under fetching or over fetching of data. In the example above we just selected ‘title’ from a subset of fields for demonstration purposes. You can add more fields as needed

REQUEST
Copy
query {
  courseCatalog {
    nodes {
      id
      idNum
      slug
      title
      level
      displayDate
      publishedDate
    }
  }
}
RESPONSE
{
  "data": {
    "courseCatalog": {
      "nodes": [
        {
          "id": "6244496f-ff33-4ce3-8c36-3efa1a142003",
          "idNum": 2136133215,
          "slug": "ccsp-cloud-governance-data-privacy-audit",
          "title": "CCSP: Cloud Governance, Data Privacy, and Audit",
          "level": "Intermediate",
          "displayDate": "2019-11-06T00:00:00.000Z",
          "publishedDate": "2019-11-06T00:00:00.000Z"
        },
        {
          "id": "2bcf249b-fc17-4ad2-83a3-9e7e40e6e203",
          "idNum": 125638784,
          "slug": "building-first-vba-module",
          "title": "Building Your First VBA Module",
          "level": "Advanced",
          "displayDate": "2019-11-06T00:00:00.000Z",
          "publishedDate": "2019-11-06T00:00:00.000Z"
        },
        {
          "id": "154a2b14-1de5-471f-8ac6-ff640943b02f",
          "idNum": 458994181,
          "slug": "object-oriented-programming-java",
          "title": "Mastering Object-oriented Programming in Java",
          "level": "Intermediate",
          "displayDate": "2019-11-06T00:00:00.000Z",
          "publishedDate": "2019-11-06T00:00:00.000Z"
        }
      ]
    }
  }
}

Another keyword is the ‘totalCount” field. This gives us the total count for the number of records returned. To see how this works let’s add totalCount to our Query:

Notice that it is inline with the nodes keyword.

REQUEST
Copy
query {
  courseCatalog {
    totalCount
    nodes {
      id
      idNum
      slug
      title
      level
      displayDate
      publishedDate
    }
  }
}
RESPONSE
{
  "data": {
    "courseCatalog": {
      "totalCount": 9220,
      "nodes": [
        {
          "id": "6244496f-ff33-4ce3-8c36-3efa1a142003",
          "idNum": 2136133215,
          "slug": "ccsp-cloud-governance-data-privacy-audit",
          "title": "CCSP: Cloud Governance, Data Privacy, and Audit",
          "level": "Intermediate",
          "displayDate": "2019-11-06T00:00:00.000Z",
          "publishedDate": "2019-11-06T00:00:00.000Z"
        },
        {
          "id": "2bcf249b-fc17-4ad2-83a3-9e7e40e6e203",
          "idNum": 125638784,
          "slug": "building-first-vba-module",
          "title": "Building Your First VBA Module",
          "level": "Advanced",
          "displayDate": "2019-11-06T00:00:00.000Z",
          "publishedDate": "2019-11-06T00:00:00.000Z"
        },
        {
          "id": "154a2b14-1de5-471f-8ac6-ff640943b02f",
          "idNum": 458994181,
          "slug": "object-oriented-programming-java",
          "title": "Mastering Object-oriented Programming in Java",
          "level": "Intermediate",
          "displayDate": "2019-11-06T00:00:00.000Z",
          "publishedDate": "2019-11-06T00:00:00.000Z"
        }
      ]
    }
  }
}