wpengine / faustjs

Faust.js™ - The Headless WordPress Framework
https://faustjs.org
Other
1.44k stars 134 forks source link

Bug: nodeByUri return null in category page #1613

Closed idmahbub closed 10 months ago

idmahbub commented 1 year ago

Applicable Versions

Captured

Screen Shot 2023-10-17 at 19 00 12
blakewilson commented 1 year ago

Hi @idmahbub,

Thanks for opening an issue.

Have you verified the query and variables you used in your Faust Template don't return null in the WPGraphQL GraphiQL in the WP Admin? We'll want to rule this out before we can confirm this is a Faust specific issue.

Can you please also paste the query and variables you used in your Faust template? I could test it out locally as well.

idmahbub commented 1 year ago

Hi @blakewilson I've only run sample code from this repo, and haven't changed it at all. I have tried Quaery graphql in this file (https://github.com/wpengine/faustjs/blob/canary/examples/next/faustwp-getting-started/wp-templates/category.js) and there are no problems.

I tried to value $uri with the static string category/news, and it worked, the problem boils down to the line nodeByUri(uri: $uri) where the value $uri is null

Console log

arnautg commented 1 year ago

This is caused by the new update of the wp-graphql v1.17.0 downgrading to the v1.16.0 solves the issue, the problem is that the URI that is passed by parameter is now the full path http://localhost:3000/category/news/ when previously was /category/news if you run in the GraphQL IDE the following query

query GetNodeByUri($uri: String!) {
  nodeByUri(uri: $uri) {
    __typename
  }
}

Using the full path results into a Post (specifically the latest post of the category)

{
  "uri": "http://localhost:3000/category/news"
}

Meanwhile, if you run it with this variable results in the category

{
  "uri": "/category/news/"
}

Another temporary fix is to change the Component.variables into this:

Component.variables = ({ uri }) => {
  return {
    ...
    uri: new URL(uri).pathname,
    ...
  };
};

Not sure which side should fix it, FaustJS or wp-graphql

blakewilson commented 1 year ago

This could also be related to this comment: https://github.com/wp-graphql/wp-graphql/pull/2341#issuecomment-1128719980

jasonbahl commented 1 year ago

@josephfusco and I are working on reproducing right now. 👀

josephfusco commented 1 year ago

Hey @idmahbub can you please provide the Faust.js WordPress Plugin version you are using and which settings you have toggled on? Thank you!

idmahbub commented 1 year ago

This is caused by the new update of the wp-graphql v1.17.0 downgrading to the v1.16.0 solves the issue, the problem is that the URI that is passed by parameter is now the full path http://localhost:3000/category/news/ when previously was /category/news if you run in the GraphQL IDE the following query

query GetNodeByUri($uri: String!) {
  nodeByUri(uri: $uri) {
    __typename
  }
}

Using the full path results into a Post (specifically the latest post of the category)

{
  "uri": "http://localhost:3000/category/news"
}

Meanwhile, if you run it with this variable results in the category

{
  "uri": "/category/news/"
}

Another temporary fix is to change the Component.variables into this:

Component.variables = ({ uri }) => {
  return {
    ...
    uri: new URL(uri).pathname,
    ...
  };
};

Not sure which side should fix it, FaustJS or wp-graphql

can be run by changing the uri value uri: new URL(uri).pathname , but coming 2 warning in console now

Screen Shot 2023-10-18 at 12 42 58
idmahbub commented 1 year ago

Hey @idmahbub can you please provide the Faust.js WordPress Plugin version you are using and which settings you have toggled on? Thank you!

faust plugin versin in Version 6.3.2, an this the setup for me

Screen Shot 2023-10-18 at 12 47 41
jasonbahl commented 1 year ago

Faust De-activated

With the Faust WordPress plugin de-activated, the term uri is relative:

CleanShot 2023-10-18 at 08 15 54

Faust Active

With the Faust WordPress plugin active, the term uri is absolute:

CleanShot 2023-10-18 at 08 16 50


I think Faust needs to fix their term link filter that rewrites the links.

jasonbahl commented 1 year ago

@idmahbub is your "Settings > General > Site Address" setting the same as your "Settings > Faust > Front-end site URL"?

If I have both settings the same, things work.

If I change the settings to be different values, then I see the issue where Faust is making the uri an absolute link, and then nodeByUri fails because it thinks you're trying to resolve an external URI (a path to a resource to a domain that's not the site_url() or home_url())

jasonbahl commented 1 year ago

I think this callback that replaces term links needs to be updated: https://github.com/wpengine/faustjs/blob/canary/plugins/faustwp/includes/replacement/callbacks.php#L213-L227

The post_link callback, right above it, has the following qualifier:

if (
        ! is_rewrites_enabled()
        || ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
    ) {

I believe the term_link callback also needs the same qualifier.

If it is a graphql request, return the link as-is instead of making it an absolute link.

jasonbahl commented 1 year ago

I've opened this PR #1616 which I believe should resolve this issue.

josephfusco commented 10 months ago

Reopening this as a fix has not yet been merged https://github.com/wpengine/faustjs/pull/1653