d1fficult
d1fficult

Reputation: 1083

How can we differentiate between include and skip directives in graphql?

Though both directives can hide a field. When include is false it works same like when skip is true then what made them different.

Upvotes: 3

Views: 2193

Answers (2)

Daniel Rearden
Daniel Rearden

Reputation: 84687

According to the spec, there is no real difference -- both directives can be used to prevent a field from being resolved. Under the hood, the only difference is that if both skip and include exist on a field, the skip logic will be evaluated first (i.e. if skip is true, the field will always be omitted regardless of the value of include).

There is no preference between the two. Having both directives allows you to reuse the same variable for both cases where you want to include or exclude different fields. It also makes queries easier to read and reason about.

For example, if you had a schema like this:

type Query {
  pet: Pet
}

type Pet {
  # other fields
  numberLitterBoxes: Int
  numberDogHouses: Int
}

Having both directives allows you to reduce the number of variables that have to be included with your request. For example, you can query:

query ExampleQuery ($isCat: Boolean) {
  pet {
    numberLitterBoxes @include(if: $isCat)
    numberDogHouses @skip(if: $isCat)
  }
}

If you only had one directive or the other, the above query would require you to pass in two variables (isCat and isNotCat).

Upvotes: 4

Boris Sokolov
Boris Sokolov

Reputation: 1793

The only difference should be in case when you apply both directives at the same time. Skip should have higher prio.

The code from both directives looks quite similar

Upvotes: 1

Related Questions