Skip to content

fix: Support asPreview by URI/SLUG Id Type#2937

Merged
jasonbahl merged 26 commits intowp-graphql:developfrom
blakewilson:fix-aspreview-slug
Oct 16, 2023
Merged

fix: Support asPreview by URI/SLUG Id Type#2937
jasonbahl merged 26 commits intowp-graphql:developfrom
blakewilson:fix-aspreview-slug

Conversation

@blakewilson
Copy link
Copy Markdown
Contributor

@blakewilson blakewilson commented Sep 25, 2023

What does this implement/fix? Explain your changes.

This PR fixes previews when querying by URI ID Type in a ContentNode with AsPreview set to true or by a post type (eg. post, page) with asPreview set to true and with ID Types of URI or SLUG.

The following queries can be used for testing:

query GetPreview($slug: ID!, $asPreview: Boolean!) {
  post(id: $slug, idType: SLUG, asPreview: $asPreview) {
    ... on NodeWithTitle {
      title
    }
    ... on NodeWithContentEditor {
      content
    }
    date
  }
}
query GetPreview($uri: ID!, $asPreview: Boolean!) {
  contentNode(id: $uri, idType: URI, asPreview: $asPreview) {
    ... on NodeWithTitle {
      title
    }
    ... on NodeWithContentEditor {
      content
    }
    date
  }
}

Does this close any currently open issues?

Closes #1673

Any relevant logs, error output, GraphiQL screenshots, etc?

contentNode test with asPreview set to false
Screenshot 2023-09-25 at 2 32 45 PM

contentNode test with asPreview set to true
Screenshot 2023-09-25 at 2 32 56 PM

post test using URI for ID Type with asPreview set to false
Screenshot 2023-09-25 at 2 33 08 PM

post test using URI for ID Type with asPreview set to true
Screenshot 2023-09-25 at 2 33 17 PM

post test using SLUG for ID Type with asPreview set to false
Screenshot 2023-09-25 at 2 34 04 PM

post test using SLUG for ID Type with asPreview set to true
Screenshot 2023-09-25 at 2 34 13 PM

Any other comments?

Where has this been tested?

Operating System: MacOS 13.1

WordPress Version: 6.3.1

WPGraphQL Version: 1.16.0

$post_id = ! empty( $revisions ) ? array_values( $revisions )[0] : $post_id;
}

return ! empty( $post_id ) ? $this->context->get_loader( 'post' )->load_deferred( $post_id ) : null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid too many return statements within this method.

@jasonbahl jasonbahl changed the title Support asPreview by URI Id Type fix: Support asPreview by URI Id Type Sep 25, 2023
@blakewilson blakewilson changed the title fix: Support asPreview by URI Id Type fix: Support asPreview by URI Id Type in ContentNode Sep 25, 2023
@blakewilson blakewilson changed the title fix: Support asPreview by URI Id Type in ContentNode fix: Support asPreview by URI Id Type Sep 25, 2023
@blakewilson blakewilson marked this pull request as ready for review September 25, 2023 19:39
@blakewilson
Copy link
Copy Markdown
Contributor Author

Tests will be created soon 🤞

@jasonbahl jasonbahl added the needs: author response Additional Pending information from the author has been requested from the author label Sep 25, 2023
blakewilson and others added 4 commits September 25, 2023 15:14
Co-authored-by: Jason Bahl <jasonbahl@mac.com>
Co-authored-by: Jason Bahl <jasonbahl@mac.com>
Co-authored-by: Jason Bahl <jasonbahl@mac.com>
Co-authored-by: Jason Bahl <jasonbahl@mac.com>
@jasonbahl
Copy link
Copy Markdown
Collaborator

Tests will be created soon 🤞

@blakewilson were you waiting on me for anything before you write tests? let me know 🙏🏻

@blakewilson
Copy link
Copy Markdown
Contributor Author

blakewilson commented Oct 2, 2023

Tests will be created soon 🤞

@blakewilson were you waiting on me for anything before you write tests? let me know 🙏🏻

@jasonbahl No, I'm currently trying to resolve some failing tests. It looks like the changes in this PR have some unintended side effects, so trying to get those resolved before writing new tests.

@blakewilson blakewilson changed the title fix: Support asPreview by URI Id Type fix: Support asPreview by URI/SLUG Id Type Oct 3, 2023
@blakewilson blakewilson requested a review from jasonbahl October 3, 2023 20:59
blakewilson and others added 4 commits October 3, 2023 17:07
Co-authored-by: Dovid Levine <justlevine@gmail.com>
Co-authored-by: Dovid Levine <justlevine@gmail.com>
…eview-slug

- refactor PreviewTest to use WPGraphQLTestCase
@jasonbahl
Copy link
Copy Markdown
Collaborator

uh oh. I was working on updating tests to use WPGraphQLTestCase, and now a bunch of things are broken. Probably my fault 🤦🏻‍♂️

@justlevine
Copy link
Copy Markdown
Collaborator

uh oh. I was working on updating tests to use WPGraphQLTestCase, and now a bunch of things are broken. Probably my fault 🤦🏻‍♂️

The last action to run before your latest commits also wasnt working (https://github.com/wp-graphql/wp-graphql/actions/runs/6398962072/job/17370176424) , so the cause might still be outside that diff.

@blakewilson
Copy link
Copy Markdown
Contributor Author

blakewilson commented Oct 3, 2023

uh oh. I was working on updating tests to use WPGraphQLTestCase, and now a bunch of things are broken. Probably my fault 🤦🏻‍♂️

The last action to run before your latest commits also wasnt working (https://github.com/wp-graphql/wp-graphql/actions/runs/6398962072/job/17370176424) , so the cause might still be outside that diff.

Weird. When I ran these independently they worked, wonder if something is conflicting. I can look into this

Screenshot 2023-10-03 at 6 20 32 PM

- continue refactoring tests
@jasonbahl
Copy link
Copy Markdown
Collaborator

@blakewilson I am working with @josephfusco to debug the test suite issues. We believe it's something to do with the setup/tear down of some other suite(s)

@jasonbahl
Copy link
Copy Markdown
Collaborator

@blakewilson

I added this to both of Blakes tests:

$this->set_permalink_structure( '' );

(and copied that method from here: https://github.com/wp-graphql/wp-graphql/blob/develop/tests/wpunit/PostObjectQueriesTest.php#L88-L93)

and they pass.

So it's a conflict with permalinks. Probably need to manually investigate why it works with default permalinks but not other pretty permalinks.

@blakewilson
Copy link
Copy Markdown
Contributor Author

@blakewilson

I added this to both of Blakes tests:

$this->set_permalink_structure( '' );

(and copied that method from here: https://github.com/wp-graphql/wp-graphql/blob/develop/tests/wpunit/PostObjectQueriesTest.php#L88-L93)

and they pass.

So it's a conflict with permalinks. Probably need to manually investigate why it works with default permalinks but not other pretty permalinks.

Awesome work @jasonbahl @josephfusco! Thanks for looking into this.

So does this mean that there is something else in WPGraphQL that needs to be looked at to fix the permalinks issue? Does this affect all "query by URI" things, or just things relating to this PR? Any suggestions on places to look first?

@jasonbahl
Copy link
Copy Markdown
Collaborator

@blakewilson im not sure yet. We diagnosed the issue but didn't have a chance to manually test further with different permalinks.

My hunch is that the resolve_by_uri() needs some work to resolve properly when asPreview is true and various permalink structures are in play

@jasonbahl
Copy link
Copy Markdown
Collaborator

@blakewilson ok, so I manually tested and it looks like the issue is related to draft posts. Since draft posts aren't public, they don't have a "pretty permalink" issued (WordPress doesn't make them publicly visible by URI) so I think idType: URI won't work with draft posts.

We might be able to fix it under the hood, and I'll keep digging, but here's the gist of what's happening:

Published Post

If I click "preview" on a published post, I get a URL like so: /blog/2023/10/09/published/?preview_id=6866&preview_nonce=1e626c85ac&preview=true&_thumbnail_id=3390

I can take that URI and pass it to the query like so:

{
  post(id: "/blog/2023/10/09/published/?preview_id=6866&preview_nonce=1e626c85ac&preview=true&_thumbnail_id=3390", asPreview: true, idType: URI) {
    id
    isPreview
    databaseId
    uri
  }
}

And I get the preview node as expected:

CleanShot 2023-10-11 at 12 45 34

Draft Posts

If I click "preview" on a draft post, I get a link like so (no pretty permalink, even though I have pretty permalinks enabled): /?p=6874&preview=true

If I pass this URI to the query:

{
  post(id: "/?p=6874&preview=true", asPreview: true, idType: URI) {
    id
    isPreview
    databaseId
    uri
  }
}

I get null:

CleanShot 2023-10-11 at 12 47 34


The tests are making assertions with pretty permalinks enabled (carried over from the PostObjectQueriesTest and never reset), which I think has exposed that this feature isn't fully working as expected.

That said, I might argue that instead of passing the following args to the query: id: "/?p=6874&preview=true", asPreview: true, idType: URI the client might be better off parsing the query params and passing the following query:

{
  post(id: "6874", asPreview: true, idType: DATABASE_ID ) {
    id
    isPreview
    databaseId
    uri
  }
}

Which does work:

CleanShot 2023-10-11 at 12 51 17

So, as a user I could click "preview" on a draft post.

The client app (Faust, etc) could parse query params, if there are any, and decide what query to send.

For example, Faust could determine that preview=true exists and set the asPreview: true in the query then determine that p=${id} or preview_id=${id} exists and use those as the values for id: $p or id: $preview_id, and set idType: DATABASE_ID.

…ical to the parsed_url['query'] value

- upate PreviewContentNodesTest
@jasonbahl
Copy link
Copy Markdown
Collaborator

jasonbahl commented Oct 11, 2023

@blakewilson ok, I think I might have figured it out.

2 things:

  1. The tests were using get_permalink( $draft_id ) which doesn't match the link you get when you click preview on a draft, so I updated to get_preview_post_link( $draft_id ).

  2. There was a bug where the WP_Query was receiving both 'p' => $id and 'pagename' => '' and this was causing WP_Query to generate SQL queries like:

SELECT
    wp_posts.*
FROM
    wp_posts
WHERE
    1=1
    AND (wp_posts.ID = '0')
    AND wp_posts.ID = 6874
    AND wp_posts.post_type = 'post'
ORDER BY
    wp_posts.post_date DESC;

There's no way we'll find a post where wp_posts.ID = '0' AND wp_posts.ID = 6874.

I added some logic that determines if the parsed_url has query params, and if so, we check if the query_params are equal to the "pagename" and if so, we unset "pagename" because that means the path is only query params and nothing else.

This leads to the proper query being generated without the AND (wp_posts.ID = '0') line, allowing the resolver to continue and return the node, or the preview node if asPreview => true

@josephfusco
Copy link
Copy Markdown
Member

I tested this locally, both idType: SLUG & idType: URI are working as expected.

…review` arg if `asPreview` isnt explicitly set

- add comment to test where `asPreview` is set to false.
- update test to leave `asPreview` undefined when using get_preview_post_link()
- update `asPreview` field description
@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit 8d252f1 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2

View more on Code Climate.

@jasonbahl jasonbahl merged commit 4da227f into wp-graphql:develop Oct 16, 2023
@blakewilson blakewilson deleted the fix-aspreview-slug branch October 16, 2023 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs: author response Additional Pending information from the author has been requested from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Querying for preview via asPreview argument doesn't work when querying by slug, uri idTypes

4 participants