-
Notifications
You must be signed in to change notification settings - Fork 9
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
Not loading shema in IDE #6
Comments
Same here. Considering that it has been several months since this was opened, I wonder if this plugin is still the way to go to expose meta fields to GraphQL. |
@lmartins Unfortunately, it's not. Build your own meta fields using ACF and expose them using WPGraphQL for ACF. |
I've used that and works great. However in this case I was looking to pull meta fields created by WordPress internal APIs that do not go through ACF. |
I just checked my code, because I had the same problem. I ended up registering the meta fields by myself like so: add_action( 'graphql_register_types', function() {
$metaFields = [
'course' => [
'announcement' => 'String',
'duration_info' => 'String',
'level' => 'String',
'price' => 'String',
'sale_price' => 'String',
'sale_price_dates' => 'String',
'sale_price_dates_end' => 'String',
'sale_price_dates_start' => 'String',
'skill_level' => 'String',
'status' => 'String',
'status_dates' => 'String',
'status_dates_end' => 'String',
'status_dates_start' => 'String',
]
];
foreach ( $metaFields as $object_name => $fields ) {
foreach ( $fields as $field_name => $field_type ) {
$field_name_key = $field_name;
$field_api_name = $object_name . '_' . $field_name;
$field_api_name = str_replace( '_', '', ucwords( $field_api_name, '_' ) );
register_graphql_field( $object_name, $field_api_name, [
'type' => $field_type,
'resolve' => function( $post ) use( $field_name_key ) {
return get_post_meta( $post->ID, $field_name_key, true );
}
]);
}
}
} ); This is copied from my past project, it was exposing meta fields of MasterStudy, a theme / plugin collection for WordPress. You can see how the GraphQL fields are registered and a resolver is added that will just resolve the value by calling I have also code written to add ordering (orderby) and filtering (where) for ACF. It's not compatible with the sample above as it's for other fields, but maybe you can convert it so it adds the functionality to your fields, too: // ORDERBY:
add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {
$values['RELEVANCE'] = [
'value' => 'relevance' // add custom meta fields as orderby options
];
return $values;
} );
add_filter( 'graphql_post_object_connection_query_args', function( $query_args, $source, $input ) {
if ( isset( $input['where']['orderby'] ) && is_array( $input['where']['orderby'] ) ) {
foreach( $input['where']['orderby'] as $orderby ) {
if (
$orderby['field'] === 'relevance'
) {
$query_args['meta_key'] = $orderby['field'];
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = $orderby['order'];
}
}
}
return $query_args;
}, 10, 3);
// WHERE:
add_action( 'graphql_register_types', function () {
$root_query_type = 'RootQuery';
$course_category_type = 'CourseCategory';
$course_type = "Course";
register_graphql_field( $course_category_type . 'To' . $course_type . 'ConnectionWhereArgs', 'relevance_gte', [
'type' => 'Number',
] );
register_graphql_field( $root_query_type . 'To' . $course_type . 'ConnectionWhereArgs', 'relevance_gte', [
'type' => 'Number',
] );
register_graphql_field( $root_query_type . 'To' . $course_type . 'ConnectionWhereArgs', 'tags_like', [
'type' => 'String',
] );
} );
add_filter( 'graphql_post_object_connection_query_args', function ( $query_args, $source, $args, $context, $info ) {
$meta_query = [];
if ( isset( $args['where']['relevance_gte'] ) ) {
$meta_query['tags'][] = [
'key' => 'relevance',
'value' => $args['where']['relevance_gte'],
'compare' => '>='
];
}
if ( isset( $args['where']['tags_like'] ) ) {
$tags_filter_arr = explode( ';', $args['where']['tags_like'] );
$meta_query['tags'] = [
'relation' => 'OR'
];
foreach ( $tags_filter_arr as $tags_filter_arr_item ) {
if($tags_filter_arr_item) {
$meta_query['tags'][] = [
'key' => 'tags',
'value' => str_replace( ' ', '', $tags_filter_arr_item ) . ';',
'compare' => 'LIKE'
];
}
}
}
$query_args['meta_query'] = $meta_query;
return $query_args;
}, 10, 5 ); You can see how it's programmed only for two custom field called "relevance" and "tags" and a specific operation "gte" (greater than equals) for the "relevance" as well as a like comparer for the "tags" field. It might give you help to understand in what direction you have to move to get this stuff for your custom fields. You would program a lot of stuff to give your fields everything that is possible, so think about what meta operations you really need. If you don't need order or filter operations on the fields, consider yourself lucky, because you won't need to progress the latter example. Good luck! |
Once I activate the plugin, the wp_graphql IDE won't load its schema.
WordPress 5.6
WP GraphQL 1.0.3
The text was updated successfully, but these errors were encountered: