So far, we've seen how the client GraphQL query works. It describes the fields regarding the data that should be supplied. But what if we need data about certain criteria? Can we query with some values? Yes! GraphQL is built on querying and it provides a variety of options we can use while querying.
Let's go back to the initial example we showcased, that is, a user's API with name and address. The client query looks like this:
{
user {
name
address {
street
}
}
}
The preceding query fetches all the users from the GraphQL server. We can also query a record by using the name. The query syntax uses curved brackets and parenthesis (:) on a field. Let's retrieve users whose name is "alice". The preceding client query should be modified using parenthesis, like this:
{
user {
name(name: "alice")
address {
city
street
}
}
}
This query only fetches a record/records whose name is "alice". This construct is similar to a function call in programming languages. name: "alice" is called an argument for the query.
The GraphQL client can query using query arguments. The values for arguments can be scalar values, custom types, or even enumerations. We can also query on multiple levels. For example, let's search for a user with name "alice" that comes from city: "Munich":
{
user(name: "alice"){
name
address(city: "Munich") {
city
street
}
}
Querying on multiple levels avoids the concept of multiple API endpoint fetches. The same API endpoint can flexibly change the data that is returned.