You shall not pass! This time, we'll look at how you can use filters to only return results based on specified rules.
You have completed the Learning GraphQL tutorials 1 - 2. You can find the previous tutorial here
About GraphQL- optional- Read more about GraphQL and when it might be best used.
records
in the database have a table
which tells us which WebApp or Module they belong to.
This time, we'll look at how you can use filters to only return results with a particular table
, or with a table
which matches a certain pattern.
The Starter Site comes packaged with a ready-built WebApp with the id of 1
and the name webapp_1
.
We'll return to our query from the previous tutorial, but this time, we'll rename it from get_all_records
to get_webapp_1
to reflect the different purpose we intend for it. We'll also be wanting to look at page 1
again.
Code:
Explorer:
Next, we'll add a filter argument:
Code:
Notes:
As an argument for the records
query, this goes inside the round brackets after records
.
Like the other arguments, filter
is followed by a colon :
We have more settings to choose next (filter
is an object) so we add curly braces { }
Explorer:
In this tutorial we'll choose the table
to apply the filter to, because we're looking for items with the table
of webapp_1
.
Code:
Notes:
We've chosen table
as the only filter. In the next tutorial we'll explore using other fields, and filtering by more than one field at once.
This field also contains further options, so we use a colon :
followed by curly braces { }
to contain the next set of options.
Explorer:
We now have a choice about:
How closely our value should match with the contents of a field before a match is returned. We'll use value
(the exact value).
The value we are matching against. We'll use webapp_1
.
Notes:
value
is a key and is followed by a colon :
Our value "webapp_1"
must be a String, so we wrap it in double quotes.
Documentation panel:
Selecting RecordsFilterInput gives you options for different filtering rules you can apply:
After the colons :
you can see the type of value expected for each of these keys. They are mostly strings String
or arrays of strings [String]
. This topic will be covered in more detail in later tutorials. Keep an eye out for the different data types expected by GraphQL in the meantime.
Explorer: When implementing this using the Explorer, the wizard will help you get the type of value correct. In this case, it provides you with quotes so that you can enter the value as a String:
You can adjust the filter to return items from a specific Module item only. In this example, we'll specify module_3
which is the Blog Module.
Code:
Explorer:
This should return Blog Posts from the Blog Module.
You can adjust the filter to return Form Submissions from a specific Form only. In this example, we'll specify form_1 which is the Newsletter Sign Up Form.
Code:
Explorer:
In order to learn GraphQL, you'll need to start experimenting with what you've picked up from this tutorials.
To help you do this, we'll now start to set you some challenges. These will ask you to tweak the examples we've given you so far and see if you can achieve the desired results.
We'll always give you the answers to the challenge in the following Article, so don't worry if you get stuck.
To carry out this challenge, you will need to create a second WebApp and add a couple of items in the Admin. By experimenting with the options in the documentation panel, see if you can filter the results so that:
your query returns all items with the table
of webapp_1
your query returns all items with the table
of webapp_2
your query does not return items which start with module_
your query does not return Form submissions which start with form_
We'll go over the answer to this challenge in the next Article.
We'll look at a possible solution to our challenge. After that, we'll continue to look at filtering queries in more detail, including:
filtering by different fields, or properties
filtering with different kinds of rules
using more than one filter at once