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
Answers
By changing the 'type' in the register_graphql_field function to
Fixed my issue and allowed me to get all rows in the query
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 yourContactForm
type. You can debug the issue by using the GraphQL IDE. The puttingwp_send_json( $first_resolver_param )
as the first line is theContactForm
resolver. Then when you run the query in the IDE you’ll see what is being returned from yourcontactForm
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.