Interrogare i dati di WordPress
Interrogare i dati di WordPressDirettive

Direttive

Le direttive sono fornite tramite le estensioni di Gato GraphQL. Ecco solo alcuni esempi.

Direttive di operazione

Crea una pipeline di operazioni tramite @depends ed esegui una delle sue operazioni in modo condizionale, in base a un valore dinamico, tramite @skip e @include:

query CheckIfPostExists($id: ID!) {
  # Initialize the dynamic variable to `false`
  postExists: _echo(value: false)
    @export(as: "postExists")
 
  post(by: { id: $id }) {
    # Found the Post => Set dynamic variable to `true`
    postExists: _echo(value: true)
      @export(as: "postExists")
  }
}
 
mutation ExecuteOnlyIfPostExists
  @depends(on: "CheckIfPostExists")
  @include(if: $postExists)
{
  # Do something...
}

Direttive di campo

Trasforma un campo in minuscolo tramite @strLowerCase:

{
  posts(pagination: { limit: 3 }) {
    id
    title @strLowerCase
  }
}

Fornisce un valore predefinito per il campo tramite @default:

query GetFeaturedImages {
  posts(pagination: { limit: 10 }) {
    id
    title
    hasFeaturedImage
    featuredImage @default(value: 1505) {
      id
      src
    }
  }
}

Rimuove l'output del campo dalla risposta tramite @remove:

query GetFeaturedImages {
  posts(pagination: { limit: 10 }) {
    id
    title
    hasFeaturedImage
    featuredImage @remove(condition: IS_NULL) {
      src
    }
    sourceFeaturedImage: featuredImage {
      src
    }
  }
}

Applica un function field su un valore di campo, tramite @passOnwards e @applyFunction:

{
  posts {
    id
    hasComments
    notHasComments: hasComments
      @passOnwards(as: "postHasComments")
      @applyFunction(
        name: "_not"
        arguments: {
          value: $postHasComments
        },
        setResultInResponse: true
      )
  }
}