• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/Type/WPObjectType.php
1
<?php
2

3
namespace WPGraphQL\Type;
4

5
use GraphQL\Type\Definition\ObjectType;
6
use WPGraphQL\Data\DataSource;
7
use WPGraphQL\Registry\TypeRegistry;
8

9
/**
10
 * Class WPObjectType
11
 *
12
 * @phpstan-import-type ObjectConfig from \GraphQL\Type\Definition\ObjectType
13
 *
14
 * Object Types should extend this class to take advantage of the helper methods
15
 * and consistent filters.
16
 *
17
 * @package WPGraphQL\Type
18
 * @since   0.0.5
19
 */
20
class WPObjectType extends ObjectType {
21

22
        use WPInterfaceTrait;
23

24
        /**
25
         * Holds the node_interface definition allowing WPObjectTypes
26
         * to easily define themselves as a node type by implementing
27
         * self::$node_interface
28
         *
29
         * @var array<string,mixed>|\WPGraphQL\Type\InterfaceType\Node $node_interface
30
         * @since 0.0.5
31
         */
32
        private static $node_interface;
33

34
        /**
35
         * Instance of the Type Registry
36
         *
37
         * @var \WPGraphQL\Registry\TypeRegistry
38
         */
39
        public $type_registry;
40

41
        /**
42
         * @var array<string, array<string, mixed>>
43
         */
44
        public $fields;
45

46
        /**
47
         * @var array<\GraphQL\Type\Definition\InterfaceType>
48
         */
49
        public $interfaces = [];
50

51
        /**
52
         * WPObjectType constructor.
53
         *
54
         * Sets up a WPObjectType instance with the given configuration and type registry.
55
         *
56
         * @param array<string, mixed>             $config The configuration array for setting up the WPObjectType.
57
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The type registry to associate with the WPObjectType.
58
         *
59
         * @phpstan-param ObjectConfig $config
60
         *
61
         * @throws \Exception If there is an error during instantiation.
62
         * @since 0.0.5
63
         */
64
        public function __construct( array $config, TypeRegistry $type_registry ) {
604✔
65

66
                /**
67
                 * Get the Type Registry
68
                 */
69
                $this->type_registry = $type_registry;
604✔
70

71
                /**
72
                 * Filter the config of WPObjectType
73
                 *
74
                 * @param ObjectConfig          $config         Array of configuration options passed to the WPObjectType when instantiating a new type
75
                 * @param \WPGraphQL\Type\WPObjectType $wp_object_type The instance of the WPObjectType class
76
                 */
77
                $config = apply_filters( 'graphql_wp_object_type_config', $config, $this );
604✔
78

79
                $this->config = $config;
604✔
80

81
                /**
82
                 * Set the Types to start with capitals
83
                 */
84
                $name           = isset( $config['name'] ) ? ucfirst( $config['name'] ) : $this->inferName();
604✔
85
                $config['name'] = apply_filters( 'graphql_type_name', $name, $config, $this );
604✔
86

87
                /**
88
                 * Set up the fields
89
                 *
90
                 * @return array<string, array<string, mixed>> $fields
91
                 */
92
                $config['fields'] = function () use ( $config ) {
604✔
93
                        return ! empty( $this->fields ) ? $this->fields : $this->get_fields( $config, $this->type_registry );
604✔
94
                };
604✔
95

96
                /**
97
                 * Set up the Interfaces
98
                 */
99
                $config['interfaces'] = $this->getInterfaces();
604✔
100

101
                /**
102
                 * Run an action when the WPObjectType is instantiating
103
                 *
104
                 * @param array<string,mixed>          $config         Array of configuration options passed to the WPObjectType when instantiating a new type
105
                 * @param \WPGraphQL\Type\WPObjectType $wp_object_type The instance of the WPObjectType class
106
                 */
107
                do_action( 'graphql_wp_object_type', $config, $this );
604✔
108

109
                parent::__construct( $config );
604✔
110
        }
111

112
        /**
113
         * Get the interfaces implemented by the ObjectType
114
         *
115
         * @return \GraphQL\Type\Definition\InterfaceType[]
116
         */
117
        public function getInterfaces(): array {
712✔
118
                if ( ! empty( $this->interfaces ) ) {
712✔
119
                        return $this->interfaces;
675✔
120
                }
121
                $this->interfaces = $this->get_implemented_interfaces();
604✔
122
                return $this->interfaces;
604✔
123
        }
124

125
        /**
126
         * Node_interface
127
         *
128
         * This returns the node_interface definition allowing
129
         * WPObjectTypes to easily implement the node_interface
130
         *
131
         * @return array<string,mixed>|\WPGraphQL\Type\InterfaceType\Node
132
         * @since 0.0.5
133
         */
134
        public static function node_interface() {
×
135
                if ( null === self::$node_interface ) {
×
136
                        $node_interface       = DataSource::get_node_definition();
×
137
                        self::$node_interface = $node_interface['Node'];
×
138
                }
139

140
                return self::$node_interface;
×
141
        }
142

143
        /**
144
         * This function sorts the fields and applies a filter to allow for easily
145
         * extending/modifying the shape of the Schema for the type.
146
         *
147
         * @param array<string,mixed> $fields    The array of fields for the object config
148
         * @param string              $type_name The name of the type to prepare fields for
149
         * @param array<string,mixed> $config    The config for the Object Type
150
         *
151
         * @return array<string,mixed>
152
         * @since 0.0.5
153
         */
154
        public function prepare_fields( $fields, $type_name, $config ) {
604✔
155

156
                /**
157
                 * Filter all object fields, passing the $typename as a param
158
                 *
159
                 * This is useful when several different types need to be easily filtered at once. . .for example,
160
                 * if ALL types with a field of a certain name needed to be adjusted, or something to that tune
161
                 *
162
                 * @param array<string,mixed>              $fields         The array of fields for the object config
163
                 * @param string                           $type_name      The name of the object type
164
                 * @param \WPGraphQL\Type\WPObjectType     $wp_object_type The WPObjectType Class
165
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry  The Type Registry
166
                 */
167
                $fields = apply_filters( 'graphql_object_fields', $fields, $type_name, $this, $this->type_registry );
604✔
168

169
                /**
170
                 * Filter once with lowercase, once with uppercase for Back Compat.
171
                 */
172
                $lc_type_name = lcfirst( $type_name );
604✔
173
                $uc_type_name = ucfirst( $type_name );
604✔
174

175
                /**
176
                 * Filter the fields with the typename explicitly in the filter name
177
                 *
178
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
179
                 * more specific overrides
180
                 *
181
                 * @param array<string,mixed>              $fields         The array of fields for the object config
182
                 * @param \WPGraphQL\Type\WPObjectType     $wp_object_type The WPObjectType Class
183
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry  The Type Registry
184
                 */
185
                $fields = apply_filters( "graphql_{$lc_type_name}_fields", $fields, $this, $this->type_registry );
604✔
186

187
                /**
188
                 * Filter the fields with the typename explicitly in the filter name
189
                 *
190
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
191
                 * more specific overrides
192
                 *
193
                 * @param array<string,mixed>              $fields         The array of fields for the object config
194
                 * @param \WPGraphQL\Type\WPObjectType     $wp_object_type The WPObjectType Class
195
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry  The Type Registry
196
                 */
197
                $fields = apply_filters( "graphql_{$uc_type_name}_fields", $fields, $this, $this->type_registry );
604✔
198

199
                /**
200
                 * This sorts the fields alphabetically by the key, which is super handy for making the schema readable,
201
                 * as it ensures it's not output in just random order
202
                 */
203
                ksort( $fields );
604✔
204

205
                return $fields;
604✔
206
        }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc