-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-graphql-dad-jokes.php
36 lines (33 loc) · 1.14 KB
/
wp-graphql-dad-jokes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
* Plugin Name: WPGraphQL Dad Jokes
* Plugin URI: http://github.com/wp-graphql/wp-graphql-dad-jokes
* Description: A WPGraphQL Extension that adds a root query to the GraphQL schema that returns a random Dad Joke from icanhazdadjokes.com
* Author: WPGraphQL, Jason Bahl
* Author URI: http://graphql.com
* Text Domain: wp-graphql-dad-jokes
* Domain Path: /languages
* Version: 1.0.0
*
* @package WP_Graphql_Dad_Jokes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'dadJoke', [
'type' => 'String',
'description' => __( 'Returns a random Dad joke', 'wp-graphql' ),
'resolve' => function() {
$get_dad_joke = wp_remote_get('https://icanhazdadjoke.com/', [
'headers' => [
'Accept' => 'application/json',
'User-Agent' => 'WPGraphQL Dad Jokes (https://github.com/wp-graphql/wp-graphql-dad-jokes)',
],
] );
$body = ! empty( $get_dad_joke['body'] ) ? json_decode( $get_dad_joke['body'] ) : null;
$joke = ! empty( $body->joke ) ? $body->joke : null;
return $joke;
},
]);
} );