GraphQL

Tutorial 3 - Filtering the Results

17min
you shall not pass! this time, we'll look at how you can use filters to only return results based on specified rules prerequisites 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 introduction 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 returning the gallery webapp the starter site comes packaged with a ready built webapp with the id of 1 and the name webapp 1 step 1 load the previous query 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 query get webapp 1 { records( page 1 per page 20 ) { current page total pages results { table properties } } } explorer step 2 add the filter argument next, we'll add a filter argument code query get webapp 1 { records( page 1 per page 20 filter { } ) { current page total pages results { table properties } } } 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 step 3 filter by the table 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 query get webapp 1 { records( page 1 per page 20 filter { table { } } ) { current page total pages results { table properties } } } 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 step 4 define the filtering rule 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 query get webapp 1 { records( page 1 per page 20 filter { table { value "webapp 1" } } ) { total pages results { table properties } } } 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 returning items from the blog module 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 query get blog module { records( page 1 per page 20 filter { table { value "module 3" } } ) { total pages results { table properties } } } explorer this should return blog posts from the blog module returning form submissions 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 query get newsletter signups { records( page 1 per page 20 filter { table { value "form 1" } } ) { total pages results { table properties } } } explorer challenge! introduction to graphql challenges 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 your challenge is to write a query which returns items from all webapps but not module items 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 next time 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