Ad

Wpgraphql- Make Data Private

- 1 answer

I want to make data private (to return null when query is fetched). I know that there is graphql_data_is_private filter, but I don't know how to implement my query, there's no example in docs.

Docs (https://www.wpgraphql.com/filters/graphql_data_is_private/):

apply_filters( 'graphql_data_is_private', bool $is_private, string $model_name, mixed $data, string $visibility, int $owner, WP_User $current_user );

My query:

query MyQuery {
  users {
    edges {
      node {
        lastName
      }
    }
  }
}
Ad

Answer

It's not a query option, it's a BE definition, you should add a filter (in your plugin or theme code) to modify (adapt to your needs) standard BE behaviour. This filter (hook) (for queried object) is used to check if data should be returned.

add_filter('graphql_data_is_private', 'add_graphql_private_visibility_filter', 10, 6);

function add_graphql_private_visibility_filter($is_private, $model_name, $data, $visibility, $owner, $current_user)
{
  if ('UserObject' === $model_name) {
    // if( is_your_allowed_data_condition ) return false;
    return true;
  }

  // return not changed for other objects
  return $is_private;
}
Ad
source: stackoverflow.com
Ad