Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying Page by URI doest require correct URI field #3042

Open
3 tasks done
andremendonca03 opened this issue Feb 9, 2024 · 16 comments
Open
3 tasks done

Querying Page by URI doest require correct URI field #3042

andremendonca03 opened this issue Feb 9, 2024 · 16 comments
Assignees
Labels
Component: Query Status: 🚀 Actionable Issues that have been curated, have enough info to take action, and are ready to be worked on Type: Bug Something isn't working

Comments

@andremendonca03
Copy link

Description

When querying for pages or posts by URI, it returns any match of the uri even if it's incomplete. It turns out to be a problem when parent/child relationships are in place.
i.e. I've got a test page which URI is /about/test/ but it can still be queried just with a fraction of the URI like bout/test/ or even just /test/

Screenshot 2024-02-08 at 4 22 43 pm
Screenshot 2024-02-08 at 4 23 23 pm

Steps to reproduce

  1. Query for any page/post/cpt using IdType: URI

Additional context

No response

WPGraphQL Version

1.21.0

WordPress Version

6.4.3

PHP Version

8.3.0

Additional environment details

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have disabled ALL plugins except for WPGraphQL.

  • Yes
  • My issue is with compatibility with a specific WordPress plugin, and I have listed all my installed plugins (and version info) above.
@jasonbahl
Copy link
Collaborator

@andremendonca03 if you visit /test/ in a the php-rendered wordpress front end of the site, what happens?

@justlevine
Copy link
Collaborator

@andremendonca03 if you visit /test/ in a the php-rendered wordpress front end of the site, what happens?

Per slack it 404s on the frontend.

@jasonbahl
Copy link
Collaborator

ah ok. Often times WordPress will attempt to find the best match and return the page still, or redirect to it.

@jasonbahl
Copy link
Collaborator

@andremendonca03 can you query for more fields? like __typename, id, etc?

Do you maybe have something under the /test/ permalink?

When I try and reproduce, I get a null when querying page(id:"/test/" idType:URI) { but successfully get the node returned when querying for /about/test/

CleanShot 2024-02-09 at 08 59 38

@jasonbahl jasonbahl added Needs: Reproduction This issue needs to be reproduced independently. Close Candidate Needs confirmation before closing Status: Awaiting Author Response Additional information has been requested from the author labels Feb 9, 2024
@andremendonca03
Copy link
Author

andremendonca03 commented Feb 11, 2024

@jasonbahl Unfortunately I am still getting the page even if I query for more fields. When visiting the URI /test/ it 404s on wordpress' front-end.
Screenshot 2024-02-12 at 9 46 54 am
Screenshot 2024-02-12 at 9 50 38 am

Btw, when I used a wrong url with 2 slashes it somehow returned a post data (not page) alongside an error.

Screenshot 2024-02-12 at 9 45 39 am

@jasonbahl
Copy link
Collaborator

Are you able to reproduce with no other plugins other than WPGraphQL?

I was not able to reproduce. Curious if there are specific permalink rules at play or something from a plugin possibly at play here.

@andremendonca03
Copy link
Author

@jasonbahl Yes, I just spinned up a brand new wordpress, created the about page and test child page. Installed WPGraphQL and run the queries.

Querying by page /test/ it is still returning the page:

Screenshot 2024-02-12 at 11 25 39 am

And this wrong URI is still retrieving the post:

Screenshot 2024-02-12 at 11 25 00 am

@jasonbahl
Copy link
Collaborator

This seems like maybe a $global post thing. 🤔

What happens if you query for page first then nodeByUri in the same request?

@andremendonca03
Copy link
Author

It's still returning the Page on page query and null for nodeByUri

Screenshot 2024-02-12 at 2 24 54 pm

@jasonbahl jasonbahl self-assigned this Feb 14, 2024
@jasonbahl
Copy link
Collaborator

Attempt to reproduce:

  • using LocalWP, create a fresh install
  • install and activate WPGraphQL v1.21.0 (no other active plugins)

CleanShot 2024-02-14 at 10 34 26

  • delete the pages WordPress is scaffolded with (sample, privacy policy)

  • create the following pages:

    • Home Page (set to front page)
    • About
      • Test
    • Draft (leave as draft)

CleanShot 2024-02-14 at 10 37 09

CleanShot 2024-02-14 at 10 37 16

  • Ensure my permalinks are flushed:

CleanShot 2024-02-14 at 10 40 02

  • Open GraphiQL and execute the following query:
{
  nodeByUri(uri: "/test/") {
    __typename
    id
    uri
  }
  page(id: "/test/", idType: URI) {
    __typename
    id
    uri
  }
}
  • see the following results:

CleanShot 2024-02-14 at 10 40 51

  • Visit the /test/ uri in the non-headless WordPress front-end (WordPress redirects automatically to /about/test/)

test-uri-debugging

  • Change permalinks to "Post Name"

CleanShot 2024-02-14 at 10 43 39

  • Query again:

Now I am able to see the "/about/test" node returned for the "page", but not the "nodeByUri", which is indeed interesting.

CleanShot 2024-02-14 at 10 44 08

However, the node that is returned also returns a uri value of /about/test/ which a client should be able to use to determine that the actual uri of a node is different than the requested URI and then redirect the page to the correct /about/test/ location as we saw the WordPress front-end do in the GIF above.

I think at minimum we've identified an inconsistency with nodeByUri and page( id: $id idType: URI ) behavior.

If WordPress is able to accept a non-existing uri (/test/) and redirect it to another matching node (/about/test/) then WPGraphQL should be able to behave in that same way.

I would think the correct solution here would be to have the nodeByUri also return the /about/test/ node, and the client should be able to use that information to determine if the requesting uri and returned node's uri don't match, redirect to the node's correct uri, as WordPress's template layer does.

@jasonbahl
Copy link
Collaborator

I think the action here is to identify why page( id: $id idType: URI ) behaves differently than nodeByUri() and improve the consistency there.

Without looking, my hunch is that the RootQuery.page field passes some context to the resolver, identifying that the Node Resolver should resolve a page, but the RootQuery.nodeByUri resolver doesn't have that context, because it doesn't know that a page is the uri to look for (the uri can be ANY Node type) where the page field does know that a "Page" is expected and not another type.

@justlevine
Copy link
Collaborator

justlevine commented Feb 14, 2024

I would think the correct solution here would be to have the nodeByUri also return the /about/test/ node, and the client should be able to use that information to determine if the requesting uri and returned node's uri don't match, redirect to the node's correct uri, as WordPress's template layer does.

WP default behavior is to correctly return a 404, so nodeByUri is working correctly. I don't think we should deviate from this.

RootQuery.posts uses some manual args to try and efficiently prevent the NodeResolver from checking irrelevant node types, so that's where things are going wrong and need to be fixed.

@jasonbahl
Copy link
Collaborator

WP default behavior is to correctly return a 404

In a vanilla WordPress install, it redirected not returned a 404 though 🤔

@justlevine
Copy link
Collaborator

justlevine commented Feb 14, 2024

WP default behavior is to correctly return a 404

In a vanilla WordPress install, it redirected not returned a 404 though 🤔

It doesn't for me 🤔 Let me try again when I'm by my desk

@jasonbahl
Copy link
Collaborator

it did for me in a vanilla install with only WPGraphQL and whatever latest default theme.

@jasonbahl jasonbahl removed Close Candidate Needs confirmation before closing Needs: Reproduction This issue needs to be reproduced independently. Status: Awaiting Author Response Additional information has been requested from the author labels Feb 15, 2024
@jasonbahl
Copy link
Collaborator

We should tackle this by handling it the same way as core WordPress.

if WordPress redirects, WPGraphQL should return the node, and the node should have the accurate uri as a field that can be used to redirect to by the client.

If WordPress 404s, WPGraphQL should return a null.

@justlevine justlevine added Type: Bug Something isn't working Status: 🚀 Actionable Issues that have been curated, have enough info to take action, and are ready to be worked on Component: Query labels Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Query Status: 🚀 Actionable Issues that have been curated, have enough info to take action, and are ready to be worked on Type: Bug Something isn't working
Projects
Status: 📍 Confirmed
Development

No branches or pull requests

3 participants