ashhitch / wp-graphql-yoast-seo

This is an extension to the WPGraphQL plugin for Yoast SEO
GNU General Public License v3.0
216 stars 49 forks source link

Empty SEO fields when asPreview is true #135

Open paolospag opened 2 years ago

paolospag commented 2 years ago

Hello, I'm new to this plugin and WPGraphQL and I would like to know if it's correct that the fields are empty with asPreview or if I rather need to change something.

This is my query:

const GET_POST_BY_ID = gql`
    query PostBySlug($id: ID!, $idType: PostIdType = URI, $asPreview: Boolean = false) {
        post(id: $id, idType: $idType, asPreview: $asPreview) {
            id
            slug
            isPreview
            title(format: RENDERED)
            content(format: RENDERED)
            featuredImage {
                node {
                    uri
                    altText
                }
            }
        seo {
            title
            metaDesc
            metaKeywords
            breadcrumbs {
                text
                url
            }
        }
        }
    }
`

And this is the method I use to run the query:

async function getSinglePost(id, asPreview = false) {
    const idType = asPreview ? 'DATABASE_ID' : 'URI';
    const { data } = await client.query({
        query: GET_POST_BY_ID,
        variables: {
            id,
            idType,
            asPreview,
        },
    });
    return data?.post || {};
}

When the variable asPreview is set to true, some fields related to Yoast SEO are empty. In particular:

seo {
    title
    metaDesc
    metaKeywords
    breadcrumbs {
        text
        url
    }
}

Am I wrong something or is it correct that it behaves like this?