wp-graphql / wp-graphql-jwt-authentication

Authentication for WPGraphQL using JWT (JSON Web Tokens)
GNU General Public License v3.0
341 stars 74 forks source link

No X-WP-Total or X-WP-TotalPages headers #89

Closed jake-101 closed 4 years ago

jake-101 commented 4 years ago

I am trying to set up an API integration that requires X-WP-Total but it looks like the WP GraphQL JWT plugin is conflicting somewhere to block it from showing in a response. I have it in my access-control-expose headers but it doesn't return correctly. If I deactivate the plugin it works fine but I use JWT Auth for a feature elsewhere. :( I tried to search for other issues but didn't see a solution. Any help would be super appreciated. Thanks!

jake-101 commented 4 years ago

https://github.com/wp-graphql/wp-graphql-jwt-authentication/blob/d0faaab6cc0dfba47cf111613ad665e7c465e2ff/src/ManageTokens.php#L345

It looks like this line conflicts with what I put into my functions.php file maybe and already has a code comment saying it might conflict elsewhere. i'll comment it out and try it out

jake-101 commented 4 years ago

Well that fixed it!

jasonbahl commented 4 years ago

@jake-101 can you share details on how you are using the X-JWT-Total?

Typically, the total count would be exposed in the query itself, as you can query many resources in a single request.

For example:

{
  posts { 
    nodes {
        id
        title
     }
  }
  pages {
     nodes {
        id
        title
     }
  }
}

A query like this would, I imagine, have trouble showing the total, as you're querying both Posts and Pages.

A common place to show the total is within the query's pageInfo, like so:

  posts { 
    pageInfo {
       totalCount
    }
    nodes {
        id
        title
     }
  }
  pages {
    pageInfo {
       totalCount
    }
     nodes {
        id
        title
     }
  }
}

This way each query can expose the total for the resources being requested.

The core WPGraphQL plugin doesn't support totalCount at the moment, but I've got documentation on how to accomplish this here:

https://github.com/wp-graphql/wp-graphql/blob/develop/src/Data/Connection/AbstractConnectionResolver.php#L632-L659

https://github.com/wp-graphql/wp-graphql/blob/develop/src/Data/Connection/AbstractConnectionResolver.php#L66-L80

tsmith-rv commented 4 years ago

I had this same issue and it was specifically causing me trouble by preventing the "Authors" dropdown in the post editor from populating with all the authors (presumably because it uses the number of total pages to know how many times to query for more authors until the list is complete). I fixed by updating the set_headers (which sets all headers) functions calls in the add_auth_headers_to_rest_response function to use header (which sets a single header), I believe on lines 345 and 360 in ManageTokens.php.

I've actually opened a PR with the fix I implemented locally. #118

rodrigo-arias commented 2 years ago

Thanks @tsmith-rv your fix worked for me. @jasonbahl is there any reason not to merge #118?