Primi passi
Primi passiSostituire la WP REST API

Sostituire la WP REST API

Se la tua applicazione usa la WP REST API, è possibile usare Gato GraphQL al suo posto.

Con l'estensione Persisted Queries puoi pubblicare endpoint di tipo REST, composti con GraphQL.

Per ciascuno degli endpoint REST della tua applicazione, puoi creare un endpoint di query persistita corrispondente che recupera gli stessi dati e usare quell'endpoint al suo posto.

Ad esempio, la seguente query GraphQL può sostituire l'endpoint REST /wp-json/wp/v2/posts/:

{
  posts {
    id
    date: dateStr(format: "Y-m-d\\TH:i:s")
    modified: modifiedDateStr(format: "Y-m-d\\TH:i:s")
    slug
    status
    link: url
    title: self {
      rendered: title
    }
    content: self {
      rendered: content
    },
    excerpt: self {
      rendered: excerpt
    }
    author
    featured_media: featuredImage
    sticky: isSticky
    categories
    tags
  }
}

Grazie alla gerarchia di API, la query persistita può essere pubblicata sotto il percorso /graphql-query/wp/v2/posts/, rendendo facile il mappare gli endpoint.

Per replicare l'endpoint REST /wp-json/wp/v2/posts/{id}/, che recupera i dati dell'articolo con l'ID indicato, possiamo fornire l'ID dell'articolo tramite il parametro URL postId.

Ad esempio, la seguente query persistita può essere invocata sotto l'endpoint /graphql-query/wp/v2/posts/single/?postId={id}:

query GetPost($postId: ID!) {
  post(by: { id: $postId }) {
    id
    date: dateStr(format: "Y-m-d\\TH:i:s")
    modified: modifiedDateStr(format: "Y-m-d\\TH:i:s")
    slug
    status
    link: url
    title: self {
      rendered: title
    }
    content: self {
      rendered: content
    },
    excerpt: self {
      rendered: excerpt
    }
    author
    featured_media: featuredImage
    sticky: isSticky
    categories
    tags
  }
}