Deep references
You can use the following short hand syntax to access properties nested deep within objects.
Let's assume your context has the following data:
{
"a": {
"b": {
"c":1
}
}
}
With normal form syntax, you'd use:
{
a {
b {
c
}
}
}
The equivalent short hand syntax is:
{
a.b.c
}
Note that the retuned data will used the same syntax, though:
{
"a.b.c":1
}
Usage in parameters
Short hand syntax may also be used - and is often useful - when using context references in function parameters. For example, imagine the following context:
{
"post":
{
"id": "post_123",
"likedBy": [
{
"userName": "iddo",
"id": "user_123"
}
]
}
}
Now lets assume that for each user liking each post, we want to perform a certain query requiring the user's ID and the post ID (maybe write into a database?). Such query would ideally look like that:
{
post {
likedBy: {
Query(user: id, post: id) {}
}
}
}
However, this obviously poses a problem: the innermost context always has the biggest priority when accessing variables, and thus "id" will resolve to the user id, and not post id. Thus, the user's ID "shadows" the post's ID. This can be overcome using deep references:
{
post {
likedBy: {
Query(user: id, post: post.id) {}
}
}
}
Here, 'user: id' will access the user's ID, being the innermost value, but post.id will access the post.id (if user had a property called post, it would've gone there...).
Updated about 6 years ago