query published content only

Created on 5 May 2025, 26 days ago

Problem/Motivation

Need to query published nodes only. Didnt find anything specific here: https://graphql-core-schema.netlify.app/schema-extensions/entity-query.h...

Example query:

query queryClubGeneralEntitlements($langcode: Langcode!) {
  entityQuery(
    entityType: NODE
    limit: 4
    filter: {conditions: [{field: "type", value: "club_general_entitlement"}]}
    sort: {field: "nid", direction: ASC}
    revisions: DEFAULT
  ) {
    total
    items {
      ... on NodeClubGeneralEntitlement {
        status
        moderationState
        fieldDataAnalyticsTag
        fieldImage {
          mediaFileUrl {
            path
          }
        }
        translation(langcode: $langcode) {
          id
          title
          fieldSubtitle
          body
          fieldClubEntitlement {
            value
          }
          fieldCtaLink {
            title
            uri {
              ... on DefaultUrl {
                path
              }
              path
            }
          }
        }
      }
    }
  }
}

Result includes both status and moderation_state to illustrate:

{
  "data": {
    "entityQuery": {
      "total": 2,
      "items": [
        {
          "status": true,
          "moderationState": "published",
          "id": "251",
          "title": "Some Entitlement 001"
        },
        {
          "status": false,
          "moderationState": "draft",
          "id": "252",
          "title": "Some Entitlement 002 (Draft test)"
        }
      ]
    }
  }
}

Steps to reproduce

1. create node type.
2. Create published and unpublished (draft) nodes.
3. Try to query only published nodes either by "revisions", "status" field or "moderation_state" field.

If I use "revisions", I will always get both published and unpublished regardless if set to ALL, DEFAULT, or LATEST.

I cant filter by "status" field because value must be a string but status is boolean.

If I filter by "moderation_state" like:
{field: "moderation_state", value: "published", operator: EQUAL}
I get internal error
For error #0: Drupal\Core\Entity\Query\QueryException: 'moderation_state' not found in Drupal\Core\Entity\Query\Sql\Tables->ensureEntityTable

What is the correct syntax to query only published nodes?

Thanks!

πŸ’¬ Support request
Status

Active

Version

1.0

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States justclint

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024