Simple Queries
Overview
Now that you have run your first query lets talk about how to build queries from scratch and getting our first hands on experience with the documentation GraphQL IDE.
What are Operations
When interacting with a GraphQL server we build and send Operations to be executed. GraphQL supports several Operation types however the Sanctuary Health API is exclusively for fetching data. As such only Query type operations are supported. Each Query operation is composed of supported Queries, with each describing the fields to be returned, and called appropriately with any required arguments.
When building an Operation you must first start with the Operation identifier, for Query operations this is query
, followed by an optional Operation name. For this example we will call our operation exampleOperation
. Finally the contents of the Operation is defined within curly brackets, we will populate this later. This should look like the following:
query exampleOperation {
// Supported queries will go here
}
Using Queries
Now that we have the framework for an Operation it is time to chose what supported Queries to run. For this example we will be using getPosts. In the documentation for getPosts we will find that it takes an optional argument named sequelizeQuery of the SequelizeQuery type and it returns an array of the Post type. Furthermore by checking the documentation for Post we will find that id
and title
are available fields.
For this example, we will request the id
and title
fields, and we will chose not to provide the optional sequelizeQuery argument. To do this, inside the framework of the Operation we made before, we simply need to declare that we want to use getPosts and within curly brackets define that we would like to fetch the id
and title
fields. This should look like the following:
query exampleOperation {
getPosts {
id
title
}
}
GraphQL IDE
Now that we have our full Operation we can show it within the documentation's GraphQL IDE. The documentation GraphQL IDE will break down HTTP requests into their three building blocks, operation
, headers
and variables
with the result
shown below. In some instances, headers
or variables
may be omitted for brevity. The operation
, headers
and variables
sections may also be editable (supporting auto complete) so you can get hands on experience, this is denoted with an icon. Furthermore in the operation
section you are able to re-run, copy or pretify the query using the buttons on the right.
Below is the Operation we just stepped through creating in the documentation IDE. Try editing it and providing your organizations API key to get hands on experience. If your not sure what attributes are available auto complete is there to help you or you can read the documentation for getPosts.