Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add_action( 'graphql_register_types', 'register_dog_type' );
function register_dog_type() {
register_graphql_object_type( 'Dog', [
'description' => __( "Man's best friend", 'your-textdomain' ),
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the dog', 'your-textdomain' ),
],
'breed' => [
'type' => 'String',
'description' => __( 'The Breed of the dog', 'your-textdomain' ),
],
'age' => [
'type' => 'Integer',
'description' => __( 'The age, in years, of the dog', 'your-textdomain' ),
],
],
] );
}
add_action( 'graphql_register_types', 'register_dog_field' );
function register_dog_field() {
register_graphql_field( 'RootQuery', 'getDog', [
'description' => __( 'Get a dog', 'your-textdomain' ),
'type' => 'Dog',
'resolve' => function() {
// Here you need to return data that matches the shape of the "Dog" type. You could get
// the data from the WP Database, an external API, or static values. For example sake,
// we will just return a hard-coded array.
return [
'name' => 'Sparky',
'breed' => 'Golden Retriever',
'age' => 8
];
}
] );
}
add_action( 'graphql_register_types', 'register_weather_enum_type' );
function register_weather_enum_type() {
register_graphql_enum_type( 'WeatherEnum', [
'description' => __( 'Condition of weather', 'your-textdomain' ),
'values' => [
'SUNNY' => [
'value' => 'sunny'
],
'CLOUDY' => [
'value' => 'cloudy'
],
'RAINY' => [
'value' => 'rainy'
],
],
] );
}
add_action( 'graphql_register_types', 'register_weather_field' );
function register_weather_field() {
register_graphql_field( 'RootQuery', 'currentWeather', [
'type' => 'WeatherEnum',
'description' => __( 'Get the weather', 'your-textdomain' ),
'resolve' => function() {
// Here you could fetch data from a database, an external API, or whatever you like.
// In this case, we'll just return static data. It has to return one of the values
// defined by the enum to fulfill the contract of the Schema.
return 'rainy'; //sunny, cloudy
},
] );
}
add_action( 'graphql_register_types', 'register_pet_types' );
function register_pet_types() {
register_graphql_object_type( 'Dog', [
'description' => __( "Man's best friend", 'your-textdomain' ),
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the dog', 'your-textdomain' ),
],
'breed' => [
'type' => 'String',
'description' => __( 'The Breed of the dog', 'your-textdomain' ),
],
'age' => [
'type' => 'Integer',
'description' => __( 'The age, in years, of the dog', 'your-textdomain' ),
],
],
] );
register_graphql_object_type( 'Cat', [
'description' => __( "Not man's best friend...", 'your-textdomain' ),
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the cat', 'your-textdomain' ),
],
'age' => [
'type' => 'Integer',
'description' => __( 'The age, in years, of the cat', 'your-textdomain' ),
],
'isHighOnCatnip' => [
'type' => 'Boolean',
'description' => __( 'Whether the cat is high on Catnip.', 'your-textdomain' ),
]
],
] );
register_graphql_union_type( 'PetUnion', [
'types' => [
\WPGraphQL\TypeRegistry::get_type( 'Dog' ),
\WPGraphQL\TypeRegistry::get_type( 'Cat' )
],
'resolveType' => function( $pet ) {
$type = null;
switch ( $pet['type'] ) {
case 'dog':
$type = \WPGraphQL\TypeRegistry::get_type( 'Dog' );
break;
case 'cat':
$type = \WPGraphQL\TypeRegistry::get_type( 'Cat' );
break;
}
return $type;
}
] );
register_graphql_field( 'RootQuery', 'myPets', [
'description' => __( 'My pets (could be dogs or cats)', 'your-textdomain' ),
'type' => [ 'list_of' => 'PetUnion' ],
'resolve' => function() {
// You would probably get data from a database or remote API here, but we're
// just going to return hard coded data
$pets = [
[
'type' => 'dog',
'name' => 'Bender',
'age' => 3,
'breed' => 'Golden Retriever',
],
[
'type' => 'cat',
'name' => 'Speckles',
'age' => 2,
'isHighOnCatnip' => true,
],
];
return $pets;
}
] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment