Ergonode docs
Search…
Basic query tutorial
Queries in GraphQL are created by opening curly brackets.
1
{
2
3
}
Copied!
If we have more than one query, we need to name them and specify that this is the query, we do it as follows.
1
query queryName {
2
3
}
Copied!
An example of a simple query that will return the value of "pageInfo" from the "productStream" branch, and from there provide the value of "startCursor". Additionally, from the "productStream" branch, it will return the value "totalCount". Think of it as a tree, a branch, and a leaf. To get information from a leaf, you must first reach it by going through all the steps one by one. Each subsequent indentation must be called using the next curly bracket. Until it's closed, we are constantly working within the same space.
For better understanding, I've stretched the code below to reflect these indents.
1
query queryName {
2
productStream {
3
pageInfo {
4
endCursor
5
}
6
}
7
totalCount
8
9
}
Copied!
The query can also use arguments. The list of available parameters can be found in the API schema(documentation). Here, we will use the "first" argument.
Without using this argument, the system would return all the data it finds, here we want to limit ourselves to the first 2 results.
1
query queryName {
2
productStream (first: 2) {
3
pageInfo {
4
endCursor
5
}
6
totalCount
7
}
8
}
Copied!
It is also possible, to refer to the same place with several arguments, in this example we wanted to get the first 2 results and the then results after something.
1
query queryName {
2
productStream(first: 2) {
3
pageInfo {
4
endCursor
5
}
6
totalCount
7
}
8
productStream(after: "YXJyYXljb25uZWN0aW9uOjM0Mw==") {
9
pageInfo {
10
endCursor
11
}
12
totalCount
13
}
14
}
Copied!
However, the above query will not work without a slight modification. If to a given object we refer more than once, we must use aliases. Below I used two aliases: "firstTwo" and "dataAfter". Remember to use a colon after the alias.
1
query queryName {
2
firstTwo: productStream(first: 2) {
3
pageInfo {
4
endCursor
5
}
6
totalCount
7
}
8
dataAfter: productStream(after: "YXJyYXljb25uZWN0aW9uOjM0Mw==") {
9
pageInfo {
10
endCursor
11
}
12
totalCount
13
}
14
}
Copied!
Remember that in this case, the "after" filter refers to the cursor, which in this case is "YXJyYXljb25uZWN0aW9uOjM0Mw==". In your case, it will be a completely different string.
Check available cursors with a query:
1
query x {
2
productStream {
3
pageInfo {
4
endCursor
5
}
6
}
7
}
Copied!
Note that in the query (the previous one, not the one about available cursors) we call the same "totalCount" data 2 times unnecessarily. We can simplify this by using fragments.
The following query will return us exactly the same data as the previous one.
Of course, if I'm querying only one thing, there's no point in throwing it into a fragment.
However, if there were more objects or the query was more complicated, fragments allow us to optimize it.
1
query queryName {
2
firstTwo: productStream(first: 2) {
3
pageInfo {
4
endCursor
5
}
6
...fragmentName
7
}
8
dataAfter: productStream(after: "YXJyYXljb25uZWN0aW9uOjM0Mw==") {
9
pageInfo {
10
endCursor
11
}
12
...fragmentName
13
}
14
}
15
16
fragment fragmentName on ProductConnection {
17
totalCount
18
}
Copied!
In GraphQL it is also possible to use variables, variables are usually passed on by programming languages, but for the sake of completeness, I will show you how to use them.
Before the first curly bracket, I open a round bracket and define ( with the $ sign ) a variable named x in it, then I make a colon and a space and define the type of the variable, in this example, it will be Int (Integer). I could be done here, but I still would like to define a default value, so I put an equals sign followed by "2".
Then in the place where the variable should be passed I simply call it bt $x:
1
query queryName ($x: Int = 2) {
2
firstTwo: productStream(first: $x) {
3
pageInfo {
4
endCursor
5
}
6
...fragmentName
7
}
8
dataAfter: productStream(after: "YXJyYXljb25uZWN0aW9uOjM0Mw==") {
9
pageInfo {
10
endCursor
11
}
12
...fragmentName
13
}
14
}
15
16
fragment fragmentName on ProductConnection {
17
totalCount
18
}
Copied!
Queries can also use directives. In other words, conditional statements.
I'm going to add a second variable to my query named condition and set its type to Boolean, with the default value true.
Then after pageInfo I placed @include(if: $condition) directive.
1
query queryName ($x: Int = 2, $condition: Boolean = true) {
2
firstTwo: productStream(first: $x) {
3
pageInfo @include(if: $condition) {
4
endCursor
5
}
6
...fragmentName
7
}
8
dataAfter: productStream(after: "YXJyYXljb25uZWN0aW9uOjM0Mw==") {
9
pageInfo {
10
endCursor
11
}
12
...fragmentName
13
}
14
}
15
16
fragment fragmentName on ProductConnection {
17
totalCount
18
}
Copied!
Since I have previously set the default value of the $condition variable to true, the condition will be met and the data will show up, but if I change it to false, the data will not.
1
query queryName ($x: Int = 2, $condition: Boolean = true) {
2
firstTwo: productStream(first: $x) {
3
pageInfo @include(if: $condition) {
4
endCursor
5
}
6
...fragmentName
7
}
8
dataAfter: productStream(after: "YXJyYXljb25uZWN0aW9uOjM0Mw==") {
9
pageInfo {
10
endCursor
11
}
12
...fragmentName
13
}
14
}
15
16
fragment fragmentName on ProductConnection {
17
totalCount
18
}
Copied!
Copy link