Skip to content

Instantly share code, notes, and snippets.

@izzygld
Created August 17, 2020 14:34
Show Gist options
  • Save izzygld/5a424641659c00fca4b7c37f58c20b55 to your computer and use it in GitHub Desktop.
Save izzygld/5a424641659c00fca4b7c37f58c20b55 to your computer and use it in GitHub Desktop.
Replace REST API with @wpgraphql with @apollographql client.
<?php
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( 'sfwd-courses' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'course';
$args['graphql_plural_name'] = 'courses';
}
if ( 'sfwd-lessons' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'lesson';
$args['graphql_plural_name'] = 'lessons';
}
if ( 'sfwd-quiz' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'quiz';
$args['graphql_plural_name'] = 'quizs';
}
if ( 'sfwd-topic' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'topic';
$args['graphql_plural_name'] = 'topics';
}
return $args;
}, 10, 2 );
add_filter( 'register_taxonomy_args', function( $args, $taxonomy ) {
if ( 'doc_tag' === $taxonomy ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'documentTag';
$args['graphql_plural_name'] = 'documentTags';
}
return $args;
}, 10, 2 );
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Courses', 'featured_media', [
'type' => 'String',
'description' => __( 'The featured_media of the course', 'learndash' ),
'resolve' => function( $post ) {
$featured_media = get_post_meta( $post->ID, 'featured_media', true );
return ! empty( $featured_media ) ? $featured_media : 'null';
}
] );
} );
add_action( 'graphql_register_types', function() {
$post_types = WPGraphQL::$allowed_post_types;
if ( ! empty( $post_types ) && is_array( $post_types ) ) {
foreach ( $post_types as $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ($post_type === 'sfwd-courses') {
register_graphql_field( $post_type_object->graphql_single_name, 'blurb', [
'type' => 'String',
'description' => __( 'The blurb of the course', 'sfwd-courses' ),
'resolve' => function( $post ) {
$blurb = get_post_meta( $post->ID, 'blurb', true );
return ! empty( $blurb ) ? $blurb : '';
}
]);
}
if ($post_type === 'sfwd-quiz') {
register_graphql_field( $post_type_object->graphql_single_name, 'total', [
'type' => 'String',
'description' => __( 'The total of the quiz', 'sfwd-quiz' ),
'resolve' => function( $post ) {
$price = get_post_meta( $post->ID, 'total', true );
return ! empty( $price ) ? $price : '';
}
]);
}
}
}
});
@izzygld
Copy link
Author

izzygld commented Feb 14, 2022

@DiegoGonzalezCruz Correct, this is an example of how to 'hook up' the wp-graghql to learndash.

(For a single course with all its lessons and topics - query it like a single post:

{
  course( id: "cG9zdDozMzM=" ) {
    id
    title
...lessons
...topics
  }
}

We will be producing a complete plugin later on this year.

@DiegoGonzalezCruz
Copy link

Thanks for your response!

@faniabdo99
Copy link

Outstanding work man, Thanks!

@launchthatbrand
Copy link

@DiegoGonzalezCruz Correct, this is an example of how to 'hook up' the wp-graghql to learndash.

(For a single course with all its lessons and topics - query it like a single post:

{
  course( id: "cG9zdDozMzM=" ) {
    id
    title
...lessons
...topics
  }
}

We will be producing a complete plugin later on this year.

Is this query to get all lessons attached to a course supposed to work with the code provided here? Or does it require extra code to build the fragments?

@izzygld
Copy link
Author

izzygld commented Apr 1, 2024

Yes. Once you have enabled wpGraphql and activated the Learndash (single and plural names)

all data and content can be accessible via graphql

@launchthatbrand
Copy link

launchthatbrand commented Apr 5, 2024

Yes. Once you have enabled wpGraphql and activated the Learndash (single and plural names)

all data and content can be accessible via graphql

The issue I am seeing is that when i register the post types using the code above, it will not fetch related lessons of a course via:

{
...lessons
}

Querying for courses will indeed give me back all courses, but not the attached lessons..

Does more code then above need to be added to make that functionality work?

I tested this on a new template site with just learndash and graphql installed.

@launchthatbrand
Copy link

launchthatbrand commented Apr 5, 2024

I had to add this code below the code above to get all my associated topics of a lesson, and lessons to a course:

add_action( 'graphql_register_types', function() {

    register_graphql_connection( [
        'fromType' => 'Course',
        'toType' => 'Lesson',
        'fromFieldName' => 'siblings',
        'resolve' => function( $page, $args, $context, $info ) {
            $courseId = $page->databaseId;
            
            if ( ! $courseId ) {
                return null;
            }

            $resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $page, $args, $context, $info );
            $resolver->set_query_arg( 'post_type', 'sfwd-lessons' ); // Assuming 'sfwd-lessons' is your Lesson post type
            $resolver->set_query_arg( 'meta_query', array(
                array(
                    'key' => 'course_id',
                    'value' => $courseId,
                    'compare' => '=',
                ),
            ));

            return $resolver->get_connection();
        }
    ]);
	
	register_graphql_connection( [
        'fromType' => 'Lesson',
        'toType' => 'Topic',
        'fromFieldName' => 'siblings',
        'resolve' => function( $page, $args, $context, $info ) {
            $lessonId = $page->databaseId;
            
            if ( ! $lessonId ) {
                return null;
            }

            $resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $page, $args, $context, $info );
			$resolver->set_query_arg( 'post_type', 'sfwd-topic' ); // Assuming 'sfwd-topic' is your Lesson post type
      
            $resolver->set_query_arg( 'meta_query', array(
                array(
                    'key' => 'lesson_id',
                    'value' => $lessonId,
                    'compare' => '=',
                ),
            ));

            return $resolver->get_connection();
        }
    ]);

});
query CoursesAndLessons {
  courses {
    nodes {
      title
      siblings {
        nodes {
          title
          siblings {
            nodes {
              title
            }
          }
        }
      }
    }
  }
}

returns:

{
  "data": {
    "courses": {
      "nodes": [
        {
          "title": "Course 2",
          "siblings": {
            "nodes": [
              {
                "title": "C2 – Lesson 2",
                "siblings": {
                  "nodes": [
                    {
                      "title": "C2 – L2- T2"
                    },
                    {
                      "title": "C2 – L2- T1"
                    }
                  ]
                }
              },
              {
                "title": "C2 – Lesson 1",
                "siblings": {
                  "nodes": []
                }
              }
            ]
          }
        },
        {
          "title": "Course 1",
          "siblings": {
            "nodes": [
              {
                "title": "Lesson 3",
                "siblings": {
                  "nodes": []
                }
              },
              {
                "title": "Lesson 2",
                "siblings": {
                  "nodes": []
                }
              },
              {
                "title": "Lesson 1",
                "siblings": {
                  "nodes": []
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment