How to Add extra arguments into exsisting singular post type #2480
Answered
by
justlevine
mohan-neethu
asked this question in
General
-
I need to add extra Arguments for Post type "Post" (Singular post). Like Id, idType, asPreview I need to add additional new Argumet into Singular Post. How can I add this? |
Beta Was this translation helpful? Give feedback.
Answered by
justlevine
Apr 27, 2022
Replies: 2 comments 1 reply
-
I am curious about this too |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hey ya'll Currently this is a two-step process: First, you need to filter the connection config to add the input args to the schema: add_filter(
'graphql_wp_connection_type_config',
static function( array $config ): array {
// Add an input arg only for RootQuery -> Posts.
if ( 'RootQuery' !== $config['fromType'] || 'Post' !== $config['toType'] ) {
return $config;
}
$config['connectionArgs'] = array_merge(
$config['connectionArgs'],
[
'myCustomInput' => [
'type' => 'String'
'description' => 'What this does to the query',
],
// other new input field configs
]
);
return $config;
},
10
); Then you have to tell the connection resolver what to do with your new arg: add_filter(
'graphql_connection_query_args',
function( $query_args, $connection_resolver ) {
// bail if not a post object resolver.
if ( ! $connection_resolver instanceof \WPGraphQL\Data\Connection\PostObjectConnectionResolver::class ) {
return $query_args;
}
// the input args are stored inside $query_args
if ( isset( $query_args['graphql_args']['where']['myCustomInput'] ) {
// Alter the query args somehow based on the input value.
$query_args....
}
return $query_args;
},
10,
2,
); Let us know if you have any further questions :-) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jasonbahl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey ya'll
Currently this is a two-step process:
First, you need to filter the connection config to add the input args to the schema:
T…