Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
alexy-os committed Nov 14, 2024
1 parent bd605b0 commit 5599424
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 25 deletions.
64 changes: 64 additions & 0 deletions www/wp-content/plugins/mygraphql/.instruct/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions www/wp-content/plugins/mygraphql/core/AbstractGraphQLType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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',
Expand Down
32 changes: 24 additions & 8 deletions www/wp-content/plugins/mygraphql/core/Types/PageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,37 @@ 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'
];
}

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';
}
}
32 changes: 24 additions & 8 deletions www/wp-content/plugins/mygraphql/core/Types/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,37 @@ 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'
];
}

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';
}
}

0 comments on commit 5599424

Please sign in to comment.