diff --git a/www/wp-content/plugins/mygraphql/.instruct/readme.md b/www/wp-content/plugins/mygraphql/.instruct/readme.md index 1b37a53..1c66b3c 100644 --- a/www/wp-content/plugins/mygraphql/.instruct/readme.md +++ b/www/wp-content/plugins/mygraphql/.instruct/readme.md @@ -328,6 +328,70 @@ Example queries for retrieving data: } ``` +### 3. All query pages and posts + +```graphql +{ + pages(first: 10) { + nodes { + id + pageId + date + title + content + featuredImage { + node { + id + sourceUrl + altText + caption + } + } + pageFields { + key + value + } + } + } + posts(first: 10) { + nodes { + id + postId + date + title + content + excerpt + categories { + nodes { + id + categoryId + name + } + } + tags { + nodes { + id + tagId + name + } + } + featuredImage { + node { + id + sourceUrl + altText + caption + } + } + postFields { + key + value + } + } + } +} +``` + ## Configuration ### 1. Type Configuration diff --git a/www/wp-content/plugins/mygraphql/core/AbstractGraphQLType.php b/www/wp-content/plugins/mygraphql/core/AbstractGraphQLType.php index 580720d..c04f7ea 100644 --- a/www/wp-content/plugins/mygraphql/core/AbstractGraphQLType.php +++ b/www/wp-content/plugins/mygraphql/core/AbstractGraphQLType.php @@ -31,18 +31,21 @@ protected function init(): void { } protected function resolveMetaFields($post): array { - $cache_key = "graphql_meta_{$this->typeName}_{$post->ID}"; - $cached = wp_cache_get($cache_key); - - if ($cached !== false) { - return $cached; + if (empty($post->ID)) { + error_log("Post ID is empty in resolveMetaFields"); + return []; } - + + error_log("Resolving meta fields for post ID: " . $post->ID); $meta = get_post_meta($post->ID); + error_log("Raw meta data: " . print_r($meta, true)); + $result = []; foreach ($meta as $key => $values) { + error_log("Processing meta key: {$key}"); if ($this->shouldSkipMetaField($key)) { + error_log("Skipping meta key: {$key}"); continue; } @@ -54,7 +57,7 @@ protected function resolveMetaFields($post): array { } } - wp_cache_set($cache_key, $result, '', $this->config['cache_ttl']); + error_log("Final result: " . print_r($result, true)); return $result; } @@ -95,13 +98,13 @@ protected function getFields(): array { 'type' => 'ID', 'description' => 'The ID of the object' ], - 'title' => [ + /* 'title' => [ 'type' => 'String', 'description' => 'The title of the object', 'resolve' => function($post) { return get_the_title($post->ID); } - ], + ],*/ 'featuredImage' => [ 'type' => 'MediaItem', 'description' => 'Featured image for the object', diff --git a/www/wp-content/plugins/mygraphql/core/Types/PageType.php b/www/wp-content/plugins/mygraphql/core/Types/PageType.php index b06de62..7e9adfd 100644 --- a/www/wp-content/plugins/mygraphql/core/Types/PageType.php +++ b/www/wp-content/plugins/mygraphql/core/Types/PageType.php @@ -10,9 +10,7 @@ protected function getTypeConfig(): array { return [ 'show_in_graphql' => true, 'graphql_single_name' => 'page', - 'graphql_plural_name' => 'pages', - 'allowed_meta_fields' => ['page_template', 'custom_header'], - 'cache_ttl' => 3600 + 'graphql_plural_name' => 'pages' ]; } @@ -20,11 +18,29 @@ protected function registerFields(): void { register_graphql_field($this->typeName, 'pageFields', [ 'type' => ['list_of' => 'PostMetaField'], 'description' => 'All custom fields for this page', - 'resolve' => [$this, 'resolveMetaFields'] + 'resolve' => function($post) { + if (empty($post->ID)) { + return []; + } + + $meta = get_post_meta($post->ID); + $result = []; + + foreach ($meta as $key => $values) { + if (strpos($key, '_') === 0) { + continue; + } + + foreach ($values as $value) { + $result[] = [ + 'key' => $key, + 'value' => maybe_unserialize($value) + ]; + } + } + + return $result; + } ]); } - - protected function resolveCustomField($page) { - return 'custom page value'; - } } \ No newline at end of file diff --git a/www/wp-content/plugins/mygraphql/core/Types/PostType.php b/www/wp-content/plugins/mygraphql/core/Types/PostType.php index 9387a27..bb91cbd 100644 --- a/www/wp-content/plugins/mygraphql/core/Types/PostType.php +++ b/www/wp-content/plugins/mygraphql/core/Types/PostType.php @@ -10,9 +10,7 @@ protected function getTypeConfig(): array { return [ 'show_in_graphql' => true, 'graphql_single_name' => 'post', - 'graphql_plural_name' => 'posts', - 'allowed_meta_fields' => ['author_bio', 'featured_video'], - 'cache_ttl' => 1800 + 'graphql_plural_name' => 'posts' ]; } @@ -20,11 +18,29 @@ protected function registerFields(): void { register_graphql_field($this->typeName, 'postFields', [ 'type' => ['list_of' => 'PostMetaField'], 'description' => 'All custom fields for this post', - 'resolve' => [$this, 'resolveMetaFields'] + 'resolve' => function($post) { + if (empty($post->ID)) { + return []; + } + + $meta = get_post_meta($post->ID); + $result = []; + + foreach ($meta as $key => $values) { + if (strpos($key, '_') === 0) { + continue; + } + + foreach ($values as $value) { + $result[] = [ + 'key' => $key, + 'value' => maybe_unserialize($value) + ]; + } + } + + return $result; + } ]); } - - protected function resolveCustomField($post) { - return 'custom value'; - } }