How to allow conditional exclusion using @skip directive

n the previous post, we have discussed the @computed directive to calculate field values.

Now, we will see @skip directive to allow conditional exclusion for fields, fragment spreads, and inline fragments to get appropriate results…

directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
  • Use @skip directive to exclude results during execution of query
  • if argument sets the condition for exclusion
  • @skip may be used for fields, fragment spreads, and inline fragments
  • @skip should be used while executing query in GraphQL editor and not in the schema editor like other directives

We will work with the below schema.

type SkipDirective {
    str: String
}

Insert few values in the ‘str’ field.

Sample Query

In the below example, str field values will be retrieved only if the variables $test1 and $test2 are false. If the value is true in any of the variables,the field will not be included in the query execution.

query($test1: Boolean!,$test2: Boolean! ){
  find(type: SkipDirective, arcql: "*") {
    edges {
      node {
        ... on SkipDirective @skip(if:$test1) {
          str @skip(if:$test2)
        }
      }
      cursor
    }
  }
}
#input1
{
  "test1": false,
  "test2": false
}
#result1
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "str": "hypi's low code backend"
          },
          "cursor": "01FPEF18T5D9ZZG9TWBF3GSVW5"
        },
        {
          "node": {
            "str": "easy to use"
          },
          "cursor": "01FPEG3AA0K67KSJEJKV60A1DW"
        }
      ]
    }
  }
}
=================================================
#input2
{
  "test1": true,
  "test2": false
}
#result2
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {},
          "cursor": "01FPEF18T5D9ZZG9TWBF3GSVW5"
        },
        {
          "node": {},
          "cursor": "01FPEG3AA0K67KSJEJKV60A1DW"
        }
      ]
    }
  }