Skip to main content
Version: s5

Queries with Arguments

Overview

Now that you can write simple queries, lets explore how to use arguments. In GraphQL arguments can come in two forms, as a query argument (argument on the main query being run) or as a field argument (argument for fetching a specific field). In general these arguments are very easy to use, simply provide the argument name and value in smooth brackets after declaring the Query or Field. Furthermore with the Sanctuary Health API these arguments are fully typed so auto complete will be able to help here.

Filtering arguments

There are several optional filtering arguments available for both Queries and Fields, with full documentation in the Reference section however here we will focus on the where argument as it is the most popular and complex.

Because each data type has different fields, and different field types allow for different where operations the where argument will be different for each data type. As such lets look at an example of the PostDetails object and break it down.

"where": {
"id": {
"_eq": "64437043-d962-4402-9389-7ca4ade821b4"
}
}

The above example indicates the where argument is an object, with the id attribute specified to fulfill the condition where the _eq (Equals operator) is used to match 64437043-d962-4402-9389-7ca4ade821b4.

To see what attributes are available for each data type, and what operators are available for each data type please check the Reference section. For example full information about the PostDetails object on what attributes and operators for those attributes can be found here.

info

When building where objects, any conditions specified at the same level must both be true as this is interpreted as an implicit AND.

info

When building where objects, the _and and _or operators can be used recursively for complex requirements

Static arguments

Starting with the exampleOperation from Simple Queries lets specify the where argument of getPostDetails such that only 64437043-d962-4402-9389-7ca4ade821b4 is returned as discussed above.

Variable arguments

While static arguments are useful, the real power comes when arguments can be given as variables. This allows you to call the same Operation with different variables to get different results in the same structure.

To achieve this we must define an argument in the Operation that will be supplied by the Variables. Then we must use the Operation Argument to create the Query argument.

Partial passthrough

In this example we will declare an argument on the Operation called id, with with the String type. We will then use this variable inside the where argument of getPostDetails. Finally we will make sure to provide a value for our id variable in the variables section.

Full passthrough

In this example we will declare an argument on the Operation called where with the PostDetailsWhere type. We will then directly apply this to getPostDetails. Again we must make sure to provide a value for our where variable in the variables section.

This allows us with minimal syntax, to expose the where functionality.