-
Notifications
You must be signed in to change notification settings - Fork 449
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
graphql_pre_resolve_uri
does not receive extra query vars
#2515
Comments
CC: #2366 |
In the interim as a workaround, you can add your necessary E.g. (written from my phone, double check for typos ). add_filter( 'request',
function add_my_custom_query_vars( array $query_vars ) : array {
if ( ! is_graphql_request() ) {
return $query_vars;
}
if( 'my_cpt' ===$query_vars['post_type' ] ) {
$query_vars = array_merge( $query_vars,
[
// my custom query vars here.
]
);
}
return $query_vars;
},
10, 2 ); |
@maurocolella I actually ran into this the other day as well! I think the filter is placed too early. The top of the function is (mostly) setting up context, then starting at line 352 most of the resolution begins and the function starts to return things. I think we could move the filter down lower, right above line 352 (after all the context is established), but before the resolvers begin. Either that or introduce a new filter in that location. |
@jasonbahl I think that sounds nice. Thanks for taking the time! |
This filter, during development, appears to have been lower in the function, but moved to the top for specific reasons. |
@markkelnar good find! This might be a good argument to introduce a second filter lower then. One that happens after all the query args are determined, but before all (or at least most of) the returns are determined? |
I'm unclear why the best solution isnt what @maurocolella proposed initially (adding |
Hi, my company is trying to use nextjs - faustwp - wp-graphql - WPML for an existing wordpress website. WPML is installed and some custom post types are using the same slug in french / english. It seems that WPML is overriding parse_query habitually to use the good slug for the current language, there is also another hacky patch to rewrite the query use by get_page_by_path in some content parsing. My current solution, is to use graphql_pre_resolve_uri, check if the uri doesn't contains a "/", and do a query on wp_posts and wp_icl_translations for the slug in the current language. I've added a condition to reject post_types page, revision, nav_menu_item, attachment. It seems to work for now, but it could create conflict with some special uri / taxonomies ? With extra_query_vars in the filter, I would be a lot more easy to know if the request is using the "slug" and check for the post_type since name/post_type are sent in extra_query_vars. |
closed by #2582 |
What problem does this address?
I am trying to write custom resolution logic for single post queries based on slug.
Spec: https://github.com/wp-graphql/wp-graphql/blob/develop/src/Data/NodeResolver.php#L75
However I want to preserve all of the logic, but in our situation we have a meta field, per object (posts including custom post types and users) that specifies a geographic region, and we want each user to only have access to content for their specific region.
But the hook doesn't receive
$extra_query_vars
and as a result, it is not aware of which content type is being passed; hence much of the resolution logic defaults to post or page, and fails for custom post types.What is your proposed solution?
Ideally, if this hook is to substitute the original resolution, it should also receive the original arguments passed to the resolver function.
What alternatives have you considered?
I have considered:
register_post_object_fields
and routing them to my own resolution function, but this defeats the point.And suggesting other ways/hooks to break page resolution down.
For example: a filter or function that "proxies"
get_page_by_path
(WP Core) and enables more granular implementations of custom singles resolution.Depending on feasibility, this last suggestion might be the best long term option, but in the interim, I think simply passing $extra_query_vars through could do the job.
Additional Context
No response
The text was updated successfully, but these errors were encountered: