0

I am trying to get all the pages to meet certain criteria using GraphQL query as below. But this query returns all versions of the page. (We need this in Preview mode of the page)

How can we restrict the query to consider only the latest version of the page? (There is latestVersion parameter for the search query tried using this in the Search query parameter. But it gives an error. Probably it is not the correct way of using this parameter).

    query GetBlogs(
      $after: String
      $language: String!
      $numberOfPages: Int
      $pageName: String!
      $rootFolderId: String!
      $tagFilter: ItemSearchPredicateInput
    ) {
      pageResults: search(
        where: {
          AND: [
            { name: "_language", operator: EQ, value: $language }
            { name: "_hasLayout", value: "true" }
            {
              name: "_templates"
              operator: CONTAINS
              value: "acd3828a-d5b4-41fa-8a1e-1030e28b0c9a"
            }
            $tagFilter
            { name: "_name", operator: NEQ, value: $pageName }
            { name: "_path", operator: CONTAINS, value: $rootFolderId }
          ]
        }
        orderBy: { name: "date", direction: DESC }
    
        first: $numberOfPages
    
        after: $after
      ) {
        total
    
        pageInfo {
          endCursor
    
          hasNext
        }
    
        results {
          ...SearchBlogs
    
       
        }
      }
    }
    
    fragment SearchBlogs on C__AppRoute {
      id
    
      language {
        name
      }
    
      metaDescription {
        jsonValue
      }
    
      file {
        jsonValue
      }
    
      picture {
        jsonValue
      }
    
      url {
        path
      }
    
      pageTitle {
        jsonValue
      }
    
      tags {
        id
    
        jsonValue
      }
    }

Thanks,

Shrutika

2
  • The Search Query in experience edge will return only the latest version. Are you getting all versions details in Edge also? Commented Jun 14 at 4:54
  • We need this latest version in Preview mode. I have updated the question as well Commented Jun 14 at 5:35

1 Answer 1

0

You can try latestVersion parameter like

$tagFilter
        { name: "_name", operator: NEQ, value: $pageName }
        { name: "_path", operator: CONTAINS, value: $rootFolderId }
        { name: "_latestVersion", value: "true" }

Or you can try filter in code like

results.forEach(item => {
    if (!latestVersions[item.id] || latestVersions[item.id].version < item.version) {
      latestVersions[item.id] = item;
    }
  });

Hope this helps!!!

3
  • Thanks Ajay.! It gives error "message": "The field '_latestVersion' is not supported. The list of available predefined fields: '_name', '_path', '_parent', '_templates', '_hasLayout' and '_language'.", Commented Jun 14 at 9:46
  • Then I guess it should be filter in code!
    – Ajay Gour
    Commented Jun 14 at 9:52
  • yes thanks @Ajay Gour Commented Jun 14 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.