Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Posts to local json file (Field \"language\" is not defined by type RootQueryToEventConnectionWhereArgs.) #82

Open
ATOMsan opened this issue Mar 20, 2023 · 0 comments

Comments

@ATOMsan
Copy link

ATOMsan commented Mar 20, 2023

I'm trying to save my custom post type posts to local json file but the problem is when i save it with the language attribute a received the error.

{"errors":[{"message":"Field "language" is not defined by type RootQueryToEventConnectionWhereArgs.","extensions":{"category":"graphql"},"locations":[{"line":2,"column":44}]}]}

Maybe you help me to fix my code or maybe you can suggest solution to save posts to local json file.

Also in debug when file trying to save post there is one more error

Deprecated: Function WPGraphQL\Data\Connection\AbstractConnectionResolver::getArgs is deprecated since version 1.11.0! Use WPGraphQL\Data\Connection\PostObjectConnectionResolver::get_args() instead. in /home/localhost.com/public_html/wp-includes/functions.php on line 5383

add_action( 'graphql_register_types', function() { register_graphql_enum_type( 'LanguageCodeFilterEnum', [ 'description' => __( 'Filter by language code', 'domain' ), 'values' => [ 'ALL' => [ 'value' => 'all', 'description' => __( 'All languages', 'domain' ), ], 'CURRENT' => [ 'value' => 'current', 'description' => __( 'Current language', 'domain' ), ], 'EN' => [ 'value' => 'EN', 'description' => __( 'EN language', 'domain' ), ], 'UK' => [ 'value' => 'UK', 'description' => __( 'UK language', 'domain' ), ], ], ] ); } );

`function save_event_data_to_json($post_id) {
$post_type = get_post_type($post_id);
if ($post_type !== 'event') {
return;
}

$query_en = <<<GRAPHQL
query HOME_PAGE($language: LanguageCodeFilterEnum = EN){
events(first: 1000, after: null, where: {language: $language}) {
edges {
node {
title
uri
slug
eventId
featuredImage {
node {
sourceUrl
}
}
eventPost {
previewSection {
dateEnd
dateStart
eventTitle
imgSource
interfaceDate
}
}
eventPeriods {
nodes {
id
name
eventPeriodsTime {
dateEnd
dateStart
}
}
}
eventTypes {
nodes {
id
name
}
}
}
}
}
}
GRAPHQL;

$data_en = graphql(array(
'query' => $query_en,
));

$file_en = ABSPATH . 'wp-content/uploads/events_en.json';
if (WP_Filesystem()) {
global $wp_filesystem;
$wp_filesystem->put_contents($file_en, json_encode($data_en, JSON_UNESCAPED_UNICODE), FS_CHMOD_FILE);
}
}
add_action('save_post', 'save_event_data_to_json');
add_action('edit_post', 'save_event_data_to_json');`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant