Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created November 8, 2018 21:39
Show Gist options
  • Save jasonbahl/352b133150286a0c226f8e79b0549224 to your computer and use it in GitHub Desktop.
Save jasonbahl/352b133150286a0c226f8e79b0549224 to your computer and use it in GitHub Desktop.
Adding complex meta support to WPGraphQL. See: https://wp-graphql.slack.com/archives/C3NM1M291/p1541709817254200
add_action( 'init', function () {
register_post_type( 'app_audio_track', [
'label' => 'App Audio Track',
'public' => true,
'show_in_graphql' => true,
'graphql_single_name' => 'AppAudioTrack',
'graphql_plural_name' => 'AppAudioTracks',
] );
} );
add_action( 'graphql_register_types', function () {
register_graphql_enum_type( 'TrackTypeEnum', [
'description' => __( 'The type of track', 'your-textdomain' ),
'values' => [
'KIDS' => [
'value' => 'kids',
],
'ADULTS' => [
'value' => 'adults',
],
'VISUAL' => [
'value' => 'visual',
],
],
] );
register_graphql_object_type( 'MP3', [
'description' => __( 'An MP3 file', 'your-textdomain' ),
'fields' => [
'language' => [
'type' => 'String',
'description' => __( 'The language of the video', 'your-textdomain' ),
],
'title' => [
'type' => 'String',
'description' => __( 'your description...', 'your-textdomain' ),
],
'transcript' => [
'type' => 'String',
'description' => __( 'your description...', 'your-textdomain' ),
],
'description' => [
'type' => 'String',
'description' => __( 'your description...', 'your-textdomain' ),
],
'audioFile' => [
'type' => 'Int',
'description' => __( 'your description...', 'your-textdomain' ),
'resolve' => function ( $video ) {
return ! empty( $video['audio_file'] ) ? absint( $video['audio_file'] ) : null;
}
],
'duration' => [
'type' => 'String',
'description' => __( 'your description...', 'your-textdomain' ),
],
],
] );
register_graphql_field( 'AppAudioTrack', 'tracks', [
'type' => [
'list_of' => 'MP3',
],
'description' => __( 'A List of Tracks by category', 'your-textdomain' ),
'args' => [
'trackType' => [
'type' => 'TrackTypeEnum',
'description' => __( 'The type of track to filter', 'your-textdomain' ),
]
],
'resolve' => function ( $audio_track_post, $args ) {
// default to kids if no argument is set
$type = isset( $args['type'] ) ? $args['type'] : 'kids';
// replace this with:
// $meta = get_post_meta( $audio_track_post->ID, 'audio_object', true );
$meta = [
'kids' => [
[
'language' => 'english',
'title' => 'kids english title',
'transcript' => 'kids english transcript',
'audio_file' => 123,
'duration' => '31:58'
],
[
'language' => 'english',
'title' => 'kids english title',
'transcript' => 'kids english transcript',
'audio_file' => 456,
'duration' => '31:58'
],
[
'language' => 'english',
'title' => 'kids english title',
'transcript' => 'kids english transcript',
'audio_file' => 789,
'duration' => '31:58'
]
],
'adults' => [
[
'language' => 'english',
'title' => 'adult english title',
'transcript' => 'adult english transcript',
'audio_file' => 123,
'duration' => '31:58'
],
[
'language' => 'english',
'title' => 'adult english title',
'transcript' => 'adult english transcript',
'audio_file' => 456,
'duration' => '31:58'
],
[
'language' => 'english',
'title' => 'adult english title',
'transcript' => 'adult english transcript',
'audio_file' => 789,
'duration' => '31:58'
]
],
'visual' => [
[
'language' => 'english',
'title' => 'visual english title',
'transcript' => 'visual english transcript',
'audio_file' => 123,
'duration' => '31:58'
],
[
'language' => 'english',
'title' => 'visual english title',
'transcript' => 'visual english transcript',
'audio_file' => 456,
'duration' => '31:58'
],
[
'language' => 'english',
'title' => 'visual english title',
'transcript' => 'visual english transcript',
'audio_file' => 789,
'duration' => '31:58'
],
],
];
// Return the type of tracks based on the argument
return ! empty( $meta[ $type ] ) && is_array( $meta[ $type ] ) ? $meta[ $type ] : null;
}
] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment