fix: Support asPreview by URI/SLUG Id Type#2937
fix: Support asPreview by URI/SLUG Id Type#2937jasonbahl merged 26 commits intowp-graphql:developfrom blakewilson:fix-aspreview-slug
Conversation
…rent required PHP version is 7.1
|
Tests will be created soon 🤞 |
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>
@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. |
Co-authored-by: Dovid Levine <justlevine@gmail.com>
Co-authored-by: Dovid Levine <justlevine@gmail.com>
…eview-slug - refactor PreviewTest to use WPGraphQLTestCase
|
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 |
- continue refactoring tests
|
@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) |
|
I added this to both of Blakes tests:
(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? |
|
@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 |
|
@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 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 PostIf I click "preview" on a published post, I get a URL like so: 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: Draft PostsIf I click "preview" on a draft post, I get a link like so (no pretty permalink, even though I have pretty permalinks enabled): 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: 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: {
post(id: "6874", asPreview: true, idType: DATABASE_ID ) {
id
isPreview
databaseId
uri
}
}Which does work: 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 |
…ical to the parsed_url['query'] value - upate PreviewContentNodesTest
|
@blakewilson ok, I think I might have figured it out. 2 things:
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 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 |
|
I tested this locally, both |
…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
|
Code Climate has analyzed commit 8d252f1 and detected 2 issues on this pull request. Here's the issue category breakdown:
View more on Code Climate. |




What does this implement/fix? Explain your changes.
This PR fixes previews when querying by
URIID Type in aContentNodewithAsPreviewset totrueor by a post type (eg.post,page) withasPreviewset totrueand with ID Types ofURIorSLUG.The following queries can be used for testing:
Does this close any currently open issues?
Closes #1673
Any relevant logs, error output, GraphiQL screenshots, etc?
contentNodetest withasPreviewset tofalsecontentNodetest withasPreviewset totrueposttest usingURIfor ID Type withasPreviewset tofalseposttest usingURIfor ID Type withasPreviewset totrueposttest usingSLUGfor ID Type withasPreviewset tofalseposttest usingSLUGfor ID Type withasPreviewset totrueAny other comments?
…
Where has this been tested?
Operating System: MacOS 13.1
WordPress Version: 6.3.1
WPGraphQL Version: 1.16.0