Directives

Directives can be used in query or mutation

@inlcude(if: Boolean)

Include a field if it's true or false. For example, we have the query:

query QueryUser($withFriends: Boolean! = false) { 
  users { 
    username, 
    friends @include(if: $withFriends) {
      name
    }
  }  
} 

By declaring as Boolean!, we delcare that this argument is required. And providing the default value as false.

Later on, we can put in our variable object:

var variableValues = {
  withFriends: true
}

It will then select the field friends

{
    "data": {
        "users": [
            {
                "username": "Test",
                "friends": [
                    {
                        "name": "Teddy"
                    },
                    {
                        "name": "Long"
                    }
                ]
            },
            {
                "username": "Test2",
                "friends": [
                    {
                        "name": "Vu"
                    }
                ]
            }
        ]
    }
}

@skip(if: Boolean)

Skip this field if the argument is true. Similar to the above. For example

var variableValues = {
  withFriends: true,
  skipUsername: true
}
query QueryUser($withFriends: Boolean! = false, $skipUsername: Boolean! = false) { 
  users { 
    username @skip(if: $skipUsername), 
    friends @include(if: $withFriends) {
      name
    }
  }  
} 

will result into

{
    "data": {
        "users": [
            {
                "friends": [
                    {
                        "name": "Teddy"
                    },
                    {
                        "name": "Long"
                    }
                ]
            },
            {
                "friends": [
                    {
                        "name": "Vu"
                    }
                ]
            }
        ]
    }
}