GraphQL in 7 Minutes

GoalQuery Turbot Guardrails using GraphQL and learn how to explore the GraphQL API.
Time7 minutes

GraphQL is the native query language for the Turbot Guardrails API.

In this exercise, you will explore the Turbot Guardrails GraphQL API via the GraphiQL interface built into the Turbot Guardrails Console.

By the end of this lab, you will be able to query the Turbot Guardrails API with GraphQL, filter and page through results, and leverage the basic features of the GraphiQL editor.

Open GraphiQL for Your Turbot Guardrails Workspace

GraphiQL is a built-in tool to explore, build and run GraphQL queries in Turbot Guardrails.

To open the explorer, login to your Turbot Guardrails Workspace and click on Developers in the sidebar.

Run Your First GraphQL Query

Copy and paste this query into GraphiQL and click run:

{
  resources {
    items {
      title
    }
  }
}

The JSON results will match your requested format:

{
  "data": {
    "resources": {
      "items": [
        {
          "title": "my first resource"
        },
        {
          "title": "my second resource"
        }
      ]
    }
  }
}

Expand Your Query with Autocomplete

Edit the query to request extra turbot metadata about the resources:

{
  resources {
    items {
      title
      turbot {
        id
        createTimestamp
      }
    }
  }
}

While you are editing, you'll see autocompletion options appearing in the editor. Use this to try adding other fields to the results.

Discover Inline Documentation

In addition to autocomplete, GraphiQL comes with extensive inline documentation. Click the < Docs link in the top right hand corner to start exploring.

Search for the resources query in the documentation. You'll see it produces a list of the resource type [resource].

Clicking through to the resource type, you can see all the available fields.

Use Filters to Target Your Query

Add a filter to the resources query to target the results. In this case, restrict results to the Turbot Guardrails Profile resource type:

{
  resources(filter:"resourceType:profile") {
    items {
      turbot {
        id
        createTimestamp
        title
      }
    }
  }
}

Find resources created in the last 6 hours:

{
  resources(filter:"createTimestamp:>=T-6h") {
    items {
      turbot {
        id
        createTimestamp
        title
      }
    }
  }
}

Paging Through Results

Get the first page of results, including the next token. Notice the filter support for sorting and limiting results.

{
  resources(filter:"resourceType:profile sort:title limit:3") {
    items {
      turbot {
        id
        createTimestamp
        title
      }
    }
    paging {
      previous
      next
    }
  }
}

The result includes paging metadata that can be used in the next request. Query the second page of results by including the paging parameter. You have reached the end of the results set when next is returned as null.

Mutations

Mutations allow modifications to existing Turbot resource metadata using GraphQL.

Start with a query to ensure the correct resource is being acted upon. For example, query all installed mods:

query InstalledMods {
  resources(
    filter: "resource:'tmod:@turbot/turbot#/' resourceType:tmod:@turbot/turbot#/resource/types/mod resourceTypeLevel:self sort:title limit:200"
  ) {
    items {
      akas
      version: get(path: "version")
    }
  }
}

Mutations can be used to install mods, create or change policy settings, update local user passwords, and create various Turbot resources such as policy packs.

Further Reading