1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<?php
namespace WPGraphQL\Type;
use GraphQL\Type\Definition\ObjectType;
use WPGraphQL\Data\DataSource;
/**
* Class WPObjectType
*
* Object Types should extend this class to take advantage of the helper methods
* and consistent filters.
*
* @package WPGraphQL\Type
* @since 0.0.5
*/
class WPObjectType extends ObjectType {
/**
* Holds the $prepared_fields definition for the PostObjectType
*
* @var $fields
*/
private static $prepared_fields;
/**
* Holds the node_interface definition allowing WPObjectTypes
* to easily define themselves as a node type by implementing
* self::$node_interface
*
* @var $node_interface
* @since 0.0.5
*/
private static $node_interface;
/**
* WPObjectType constructor.
*
* @since 0.0.5
*/
public function __construct( $config ) {
/**
* Filter the config of WPObjectType
*
* @param array $config Array of configuration options passed to the WPObjectType when instantiating a new type
* @param Object $this The instance of the WPObjectType class
*/
$config = apply_filters( 'graphql_wp_object_type_config', $config, $this );
/**
* Set the Types to start with capitals
*/
$config['name'] = ucfirst( $config['name'] );
/**
* Filter the Type config
*/
apply_filters( 'graphql_type_config', $config );
/**
* Run an action when the WPObjectType is instantiating
*
* @param array $config Array of configuration options passed to the WPObjectType when instantiating a new type
* @param Object $this The instance of the WPObjectType class
*/
do_action( 'graphql_wp_object_type', $config, $this );
parent::__construct( $config );
}
/**
* node_interface
*
* This returns the node_interface definition allowing
* WPObjectTypes to easily implement the node_interface
*
* @return array|\WPGraphQL\Data\node_interface
* @since 0.0.5
*/
public static function node_interface() {
if ( null === self::$node_interface ) {
$node_interface = DataSource::get_node_definition();
self::$node_interface = $node_interface['nodeInterface'];
}
return self::$node_interface;
}
/**
* prepare_fields
*
* This function sorts the fields and applies a filter to allow for easily
* extending/modifying the shape of the Schema for the type.
*
* @param array $fields
* @param string $type_name
*
* @return mixed
* @since 0.0.5
*/
public static function prepare_fields( $fields, $type_name ) {
if ( null === self::$prepared_fields ) {
self::$prepared_fields = [];
}
if ( empty( self::$prepared_fields[ $type_name ] ) ) :
/**
* Filter all object fields, passing the $typename as a param
*
* This is useful when several different types need to be easily filtered at once. . .for example,
* if ALL types with a field of a certain name needed to be adjusted, or something to that tune
*
* @param array $fields The array of fields for the object config
* @param string $type_name The name of the object type
*/
$fields = apply_filters( 'graphql_object_fields', $fields, $type_name );
/**
* Filter once with lowercase, once with uppercase for Back Compat.
*/
$lc_type_name = lcfirst( $type_name );
$uc_type_name = ucfirst( $type_name );
/**
* Filter the fields with the typename explicitly in the filter name
*
* This is useful for more targeted filtering, and is applied after the general filter, to allow for
* more specific overrides
*
* @param array $fields The array of fields for the object config
*/
$fields = apply_filters( "graphql_{$lc_type_name}_fields", $fields );
/**
* Filter the fields with the typename explicitly in the filter name
*
* This is useful for more targeted filtering, and is applied after the general filter, to allow for
* more specific overrides
*
* @param array $fields The array of fields for the object config
*/
$fields = apply_filters( "graphql_{$uc_type_name}_fields", $fields );
/**
* This sorts the fields alphabetically by the key, which is super handy for making the schema readable,
* as it ensures it's not output in just random order
*/
ksort( $fields );
self::$prepared_fields[ $type_name ] = $fields;
endif;
return ! empty( self::$prepared_fields[ $type_name ] ) ? self::$prepared_fields[ $type_name ] : null;
}
}