register_graphql_field returning null values

158 Views Asked by At

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;
    ] );
}


2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

It might just be a typo, but maybe try putting the return statement in the resolver ¯\ (ツ) /¯.

But all kidding aside the issue is likely in the resolver of the register_graphql_object_type() call for your ContactForm type. You can debug the issue by using the GraphQL IDE. The putting wp_send_json( $first_resolver_param ) as the first line is the ContactForm resolver. Then when you run the query in the IDE you'll see what is being returned from your contactForm query.

On top that, what @codmoncai stated as well is likely an issue also. Switch to a list type if expect an array of result from the DB call.