Created
          June 15, 2022 17:24 
        
      - 
      
- 
        Save jasonbahl/eafda355aac410dde9aa2ab287a1480c to your computer and use it in GitHub Desktop. 
    This shows how to add an argument to a field in graphql and use it in a resolver.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | add_filter( 'graphql_RootQuery_fields', function( $fields ) { | |
| if ( isset( $fields['posts'] ) ) { | |
| $args = is_array( $fields['posts']['args'] ) ? $fields['posts']['args'] : []; | |
| $args['myCustomArg'] = [ | |
| 'type' => 'String', | |
| 'description' => __( 'This is a field that was added via a filter', 'your-textdomain' ), | |
| ]; | |
| $fields['posts']['args'] = $args; | |
| } | |
| return $fields; | |
| }, 20 ); | |
| add_filter( 'graphql_connection_query_args', function( $args, AbstractConnectionResolver $connection_resolver ) { | |
| // here's the input args provided by the query | |
| $input_args = $connection_resolver->getArgs(); | |
| if ( isset( $input_args['myCustomArg'] ) ) { | |
| $args['s'] = $input_args['myCustomArg']; | |
| } | |
| return $args; | |
| }, 10, 2 ); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment