Graphql : Fetch single post in prismic

author

Devmnj • Wed Nov 02 2022

0 min read

Posted Under : Prismic How to

Prismic CMS

Prismic is a headless CMS that help developers and content managers alike. It is a cloud based service, which allows users to create powerful backend for web and mobile apps with out touching the database.

Querying document

Unlike a REST API Graphql API allow us to minimize the load by only fetch the information we needed. Let's jump into the topic now. How do we query a single document in Prismic. ?

The following code will fetch all documents which is marked as a sticky Post, for this blog which is hosted on prismic and vercel.

Source code


export const STICKY_POST=`query  {
    allPost_types(sortBy: meta_lastPublicationDate_ASC, where: {sticky_post: true}) {
      totalCount
      edges {
        node {
          _meta {
            uid
            firstPublicationDate
          }
          title
          post_excerpt
          featured_img_link {
            ... on _ExternalLink {
              url
            }
          }
        }
      }
    }
  }`;

 const sticky= client.request(STICKY_POST).then(res=>res.allPost_types.edges)

Querying a single document

The above code is fetching a all documents, bu how do we fetch single one ? We can achieve this goal by providing first parameter as follows.

Source code

    allPost_types(first:1,sortBy: meta_lastPublicationDate_ASC, where: {sticky_post: true}) {
....

For more guides please visit my WordPress blog

Tags : PrismicgraphqlSvelteKit