简体   繁体   中英

register_graphql_field returning null values

I am trying to return values from a query that is only returning null values or is giving me errors based upon everything that I have tried. I have created a WP Plugin to put this code. I have pasted my code below

I have edited this code to what is currently working, but it is only giving me that last entry in the DB table. How would I get them all to display


function register_contact_form_fields() {
    
    register_graphql_field( 'RootQuery', 'contactForm', [
      'description' => __( 'Get a user submission', 'codmoncai-contact' ),
      'type' => 'ContactForm',
      'resolve' => function( $root, $args, $context, $info ) {
        global $wpdb;
        $combined_data = [];
        $results = $wpdb->get_results("SELECT * FROM wpxd_contact_us");
        
      }
      return $data;
    ] );
}


Uncomment this code

foreach( $results as $result) {
    $data = [
        'name' => $result['name'],
        'email' => $result['email'],
        'comments' => $result['comments']
    ];
}

and return the $data array as the result of the GraphQL field,

return $data;

By changing the 'type' in the register_graphql_field function to

'type' => [ 'list_of' => 'ContactForm' ],

Fixed my issue and allowed me to get all rows in the query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM