Queries with Arguments
Overview
Now that you can write simple queries, lets explore how to use arguments. In GraphQL arguments are very easy to use, simply provide the argument name and value in smooth brackets after declaring the Query to use.
Sequelize Query
The SequelizeQuery argument is very complex with further documentation available at Sequelize Query Info. For the purposes of this example we will only be using the where
field. As detailed in the documentation, the where
field needs to be provided as a JSON
string. For this example lets define the where
field such that the id
field of the Post must be c4cf717d-a638-49d8-bbcd-fafd2142d284
. This should look like:
where: "{\"id\": \"c4cf717d-a638-49d8-bbcd-fafd2142d284\"}"
For logical operations such as greaterThan
, like
, overlap
, etc. please refer to the further documentation for SequelizeQuery available at Sequelize Query Info
When using JSON strings make sure to use escape characters where appropriate
Static arguments
Starting with the exampleOperation
from Simple Queries lets specify the sequelizeQuery argument of getPosts such that only c4cf717d-a638-49d8-bbcd-fafd2142d284
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 where
, with with the JSON
type. This will match the where
field of SequelizeQuery. We will then use this variable inside the SequelizeQuery argument of getPosts. Finally we will make sure to provide a value for our where
variable in the variables section.
Full passthrough
In this example we will declare an argument on the Operation called sequelizeQuery
with the SequelizeQuery
type. This will match the SequelizeQuery argument of getPosts. We will then directly apply this to getPosts. Again we must make sure to provide a value for our sequelizeQuery
variable in the variables section.
This allows us with minimal syntax, to expose all functionality (such as limit
, offset
, etc.) of SequelizeQuery through our operation. Below you can see we have also applied a limit
of 1.